WC Marketplace Sales Report

2019-08-17 06:10发布

问题:

I have a page with the following code in it, this code is duplicated from the dashboard. This means this particular widget now shows in two places. With help I managed to increase the number of days this report shows to 60 however I only want the dashboard version to show 60 days and I would like this version to show 365 days.

<?php

/*
 * The template for displaying vendor pending shipping table dashboard widget
 * Override this template by copying it to yourtheme/dc-product-vendor/vendor-dashboard/dashboard-widgets/wcmp_vendor_product_sales_report.php
 *
 * @author  WC Marketplace
 * @package     WCMp/Templates
 * @version   3.0.0
 */
if (!defined('ABSPATH')) {
    // Exit if accessed directly
    exit;
}
global $WCMp;
$product_sales_report_table_headers = apply_filters('wcmp_datatable_widget_product_sales_report_table_headers', array(
    'product'      => array('label' => __( 'Product', 'dc-woocommerce-multi-vendor' )),
    // 'revenue'    => array('label' => __( 'Revenue', 'dc-woocommerce-multi-vendor' )),
    'unique_purchase'=> array('label' => __( 'Unique Purchases', 'dc-woocommerce-multi-vendor' )),
), get_current_user_id());
?>
<p>This page shows all your designs that have sold over the past year.</p>
<table id="widget_product_sales_report" class="table table-striped product_sold_last_week table-bordered wcmp-widget-dt" width="100%">
    <thead>
        <tr>
        <?php 
            if($product_sales_report_table_headers) :
                foreach ($product_sales_report_table_headers as $key => $header) { ?>
            <th class="<?php if(isset($header['class'])) echo $header['class']; ?>"><?php if(isset($header['label'])) echo $header['label']; ?></th>         
                <?php }
            endif;
        ?>
            <!--th><?php _e('Product', 'dc-woocommerce-multi-vendor'); ?></th>
            <th><?php _e('Revenue', 'dc-woocommerce-multi-vendor'); ?></th>
            <th><?php _e('Unique Purchases', 'dc-woocommerce-multi-vendor'); ?></th-->
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<script>
jQuery(document).ready(function($) {
    var product_sales_report_wgt;
    var columns = [];
    <?php if($product_sales_report_table_headers) {
     foreach ($product_sales_report_table_headers as $key => $header) { ?>
        obj = {};
        obj['data'] = '<?php echo esc_js($key); ?>';
        obj['className'] = '<?php if(isset($header['class'])) echo esc_js($header['class']); ?>';
        columns.push(obj);
     <?php }
        } ?>
    product_sales_report_wgt = $('#widget_product_sales_report').DataTable({
        ordering  : true,
        paging: true,
        info: true,
        searching  : true,
        processing: false,
        serverSide: true,
        responsive: true,
        language: {
            "emptyTable": "<?php echo trim(__('Not enough data.','dc-woocommerce-multi-vendor')); ?>",
            "zeroRecords": "<?php echo trim(__('Not enough data.','dc-woocommerce-multi-vendor')); ?>",

        },
        ajax:{
            url : '<?php echo add_query_arg( 'action', 'wcmp_widget_vendor_product_sales_report', $WCMp->ajax_url() ); ?>', 
            type: "post",
            error: function(xhr, status, error) {
                $("#widget_product_sales_report tbody").append('<tr class="odd"><td valign="top" colspan="<?php if(is_array($product_sales_report_table_headers)) count($product_sales_report_table_headers); ?>" class="dataTables_empty" style="text-align:center;">'+error+' - <a href="javascript:window.location.reload();"><?php _e('Reload', 'dc-woocommerce-multi-vendor'); ?></a></td></tr>');
                $("#widget_product_sales_report").css("display","none");
            }
        },
        columns: columns
    });
    new $.fn.dataTable.FixedHeader( product_sales_report_wgt );
});
</script>

Using this code

// To change the default value of `$days_range` from 7 days to 60 days
function lh_wcmp_vendor_custom_sales_report( $days_range ) {
    $days_range = 60; // you can adjust days here as you needed
    return $days_range;
}
add_filter( 'wcmp_widget_vendor_product_sales_report_days_range', 'lh_wcmp_vendor_custom_sales_report', 10 );

I was able to change the default 7 days to 60. But because I am using the same widget code both the dashboard and the report show the same amount of days.

So, to summerise - what I would like is to show 60 days on the dashboard (using the code above) and then show 365 days on the report page.

Can anyone help?

回答1:

You need to update the code in \basel-child\dc-product-vendor\vendor-dashboard\vendor-orders.php and \basel-child\functions.php files.

We are going to add a new query string while making the AJAX request to wcmp_widget_vendor_product_sales_report function from vendor-orders endpoint. It will allow us to detect whether the request is coming from Dashboard or Vendor Orders endpoint.

Following code will be added in the vendor-orders.php file (see line 67-79):

<?php
/* Add 'lh-endpoint' => 'lh-vendor-orders' to the AJAX URL, so we can use it later
 * to change the '$days_range' value conditionally. It will change the ajax url from
 * /wp-admin/admin-ajax.php?action=wcmp_widget_vendor_product_sales_report to 
 * /wp-admin/admin-ajax.php?action=wcmp_widget_vendor_product_sales_report&lh-endpoint=lh-vendor-orders
 */
$args = array(
    'action'      => 'wcmp_widget_vendor_product_sales_report',
    'lh-endpoint' => 'lh-vendor-orders',
);
?>
url : '<?php echo add_query_arg( $args, $WCMp->ajax_url() ); ?>',

Then we will modify our old lh_wcmp_vendor_custom_sales_report() function in functions.php file as:

// get the value of 'lh-endpoint' from URL
$lh_orders_endpoint = isset( $_GET['lh-endpoint'] ) && !empty( $_GET['lh-endpoint'] ) ? $_GET['lh-endpoint'] : '';

// check if 'lh-endpoint' value is 'lh-vendor-orders' or not
if ( 'lh-vendor-orders' !== $lh_orders_endpoint ) {
    $days_range = 60; // if 'lh-endpoint' is not 'lh-vendor-orders', then show last 2 months orders
} else {
    $days_range = 365; // if 'lh-endpoint' is 'lh-vendor-orders', then show orders from 1 year
}

Here is the complete code for both files:

  1. Remove the existing code from vendor-orders.php file and put this code:
<?php
/*
 * The template for displaying vendor orders
 * Override this template by copying it to yourtheme/dc-product-vendor/vendor-dashboard/vendor-orders.php
 *
 * @author  WC Marketplace
 * @package     WCMp/Templates
 * @version   3.0.0
 */

if (!defined('ABSPATH')) {
    // Exit if accessed directly
    exit;
}
global $WCMp;
$product_sales_report_table_headers = apply_filters('wcmp_datatable_widget_product_sales_report_table_headers', array(
    'product'      => array('label' => __( 'Product', 'dc-woocommerce-multi-vendor' )),
    // 'revenue'    => array('label' => __( 'Revenue', 'dc-woocommerce-multi-vendor' )),
    'unique_purchase'=> array('label' => __( 'Unique Purchases', 'dc-woocommerce-multi-vendor' )),
), get_current_user_id());
?>
<p>This page shows all your designs that have sold over the past year.</p>
<table id="widget_product_sales_report" class="table table-striped product_sold_last_week table-bordered wcmp-widget-dt" width="100%">
    <thead>
        <tr>
        <?php 
            if($product_sales_report_table_headers) :
                foreach ($product_sales_report_table_headers as $key => $header) { ?>
            <th class="<?php if(isset($header['class'])) echo $header['class']; ?>"><?php if(isset($header['label'])) echo $header['label']; ?></th>         
                <?php }
            endif;
        ?>
            <!--th><?php _e('Product', 'dc-woocommerce-multi-vendor'); ?></th>
            <th><?php _e('Revenue', 'dc-woocommerce-multi-vendor'); ?></th>
            <th><?php _e('Unique Purchases', 'dc-woocommerce-multi-vendor'); ?></th-->
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<script>
jQuery(document).ready(function($) {
    var product_sales_report_wgt;
    var columns = [];
    <?php if($product_sales_report_table_headers) {
     foreach ($product_sales_report_table_headers as $key => $header) { ?>
        obj = {};
        obj['data'] = '<?php echo esc_js($key); ?>';
        obj['className'] = '<?php if(isset($header['class'])) echo esc_js($header['class']); ?>';
        columns.push(obj);
     <?php }
        } ?>
    product_sales_report_wgt = $('#widget_product_sales_report').DataTable({
        ordering  : true,
        paging: true,
        info: true,
        searching  : true,
        processing: true,
        serverSide: true,
        responsive: true,
        language: {
            "emptyTable": "<?php echo trim(__('Not enough data.','dc-woocommerce-multi-vendor')); ?>",
            "zeroRecords": "<?php echo trim(__('Not enough data.','dc-woocommerce-multi-vendor')); ?>",

        },
        ajax:{
            <?php
            /* Add 'lh-endpoint' => 'lh-vendor-orders' to the AJAX URL, so we can use it later
             * to change the '$days_range' value conditionally. It will change the ajax url from
             * /wp-admin/admin-ajax.php?action=wcmp_widget_vendor_product_sales_report to 
             * /wp-admin/admin-ajax.php?action=wcmp_widget_vendor_product_sales_report&lh-endpoint=vendor-orders
             */
            $args = array(
                'action'      => 'wcmp_widget_vendor_product_sales_report',
                'lh-endpoint' => 'lh-vendor-orders',
            );
            ?>
            //url : '<?php //echo add_query_arg( 'action', 'wcmp_widget_vendor_product_sales_report', $WCMp->ajax_url() ); ?>', 
            url : '<?php echo add_query_arg( $args, $WCMp->ajax_url() ); ?>', 
            type: "post",
            error: function(xhr, status, error) {
                $("#widget_product_sales_report tbody").append('<tr class="odd"><td valign="top" colspan="<?php if(is_array($product_sales_report_table_headers)) count($product_sales_report_table_headers); ?>" class="dataTables_empty" style="text-align:center;">'+error+' - <a href="javascript:window.location.reload();"><?php _e('Reload', 'dc-woocommerce-multi-vendor'); ?></a></td></tr>');
                $("#widget_product_sales_report").css("display","none");
            }
        },
        columns: columns
    });
    new $.fn.dataTable.FixedHeader( product_sales_report_wgt );
});
</script>
  1. In your functions.php file, replace the old lh_wcmp_vendor_custom_sales_report() function with the new one:
// Conditionally change the default value of `$days_range` from 7 days to 60 or 365 days
function lh_wcmp_vendor_custom_sales_report( $days_range ) {

    // get the value of 'lh-endpoint' from URL
    $lh_orders_endpoint = isset( $_GET['lh-endpoint'] ) && !empty( $_GET['lh-endpoint'] ) ? $_GET['lh-endpoint'] : '';

    // check if 'lh-endpoint' value is 'lh-vendor-orders' or not
    if ( 'lh-vendor-orders' !== $lh_orders_endpoint ) {
        $days_range = 60; // if 'lh-endpoint' is not 'lh-vendor-orders', then show last 2 months orders
    } else {
        $days_range = 365; // if 'lh-endpoint' is 'lh-vendor-orders', then show orders from 1 year
    }
    return $days_range;
}
add_filter( 'wcmp_widget_vendor_product_sales_report_days_range', 'lh_wcmp_vendor_custom_sales_report' );

See the screenshots of locally hacked version of WC Marketplace plugin:

wcmp_widget_vendor_product_sales_report call from Vendor Dashboard

wcmp_widget_vendor_product_sales_report call from Vendor Orders Tab

Tested and working on:

  • WordPress 5.0.3
  • Twentyninteen 1.2
  • WooCommerce 3.5.4
  • WC Marketplace 3.3.1
  • Localhost (XAMPP for Windows 5.6.15)


标签: wordpress