【问题标题】:Customizing Woocommerce reports time interval自定义 Woocommerce 报告时间间隔
【发布时间】:2015-11-22 01:55:39
【问题描述】:

我需要在报告选项卡中插入 last4Days 选项。 我已经通过更改原始文件来做到这一点,但 WooCommerce 会在一段时间后不断更改为原始文件。 所以我试图找到一个过滤器来这样做,但我没有找到它。

【问题讨论】:

    标签: filter woocommerce report hook


    【解决方案1】:

    不幸的是,Woo 团队没有做用户友好的事情,并在报告长度参数上包含一个过滤器。缺少一些元编程魔法(比如在加载之前动态覆盖 .php 文件文本中的参数,非常危险),我能看到的唯一方法是将实例化阶梯上升到生成报告的位置,并且然后让它根据您正在使用的类调用自定义类。幸运的是,WooCommerce 确实为报告文件的路径提供了一个过滤器,它位于 wc-class-admin-reports.php 中,并且挂钩名为 wc_admin_reports_path。如果我们扩展优惠券使用报告,它看起来有点像:

    add_filter('wc_admin_reports_path', 'redirect_coupon_report_class_path', 10, 2);
    function redirect_coupon_report_class_path($path, $report_name, $class) {
      if($report_name == 'coupon-usage') {
        $path = 'path/to/my/custom/class.php';
      }
      return $path;
    }
    

    您需要做的是复制您要更改的报告的类,并将其粘贴到您的主题或插件中的某个位置。然后,添加一个过滤器调用一个函数,该函数将检查传入的报告是否是您要修改的报告,并将其重定向到您的自定义类。在您的自定义类中,您几乎可以做任何您想做的事情,但是与任何覆盖一样,您会希望看到每次更新时它们在该文件中所做的更改。您可以像这样扩展类:

    class WC_Report_Coupon_Usage_Custom extends WC_Report_Coupon_Usage {
    
       // Call the parent constructor
       function __construct() {
         parent::__construct();
       }
    
       // Add a method that you would have previously overwritten directly in the plugin file
       public function output_report() {
    
            $ranges = array(
                'year'         => __( 'Year', 'woocommerce' ),
                'last_month'   => __( 'Last Month', 'woocommerce' ),
                'month'        => __( 'This Month', 'woocommerce' ),
                '7day'         => __( 'Last 7 Days', 'woocommerce' ),
                '4day'         => __( 'Last 4 Days', 'woocommerce' )
            );
    
            $this->chart_colours = array(
                'discount_amount' => '#3498db',
                'coupon_count'    => '#d4d9dc',
            );
    
            $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : '7day';
    
            if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', 'month', '7day' ) ) ) {
                $current_range = '7day';
            }
    
            $this->calculate_current_range( $current_range );
    
            include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php');
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      相关资源
      最近更新 更多