【问题标题】:add_filter issue in custom plugin file for WooCommerce shippingWooCommerce 运输的自定义插件文件中的 add_filter 问题
【发布时间】:2018-06-20 05:28:24
【问题描述】:

这是我的自定义插件代码:

class Class_name
{
    public function __construct()
    {
        if ( is_admin() ) {

            //Custom admin menu
            add_action( 'admin_menu', array( $this,'custom_submenu_page'));


            add_filter( 'woocommerce_package_rates', array($this , 'conditional_shipping', 10, 2 ));

        }
    }
function conditional_shipping( $rates, $packages ) {
    if ( isset( $rates['flat_rate:32'] ) ) {
    // Set the cost to $100
    $rates['flat_rate:32']->cost = 100;
}
    return $rates;
}



}


new Class_name();

我正在尝试使用此过滤器,但他不起作用。前端什么都没有发生。我不知道错误在哪里。

我不是应该使用过滤器吗?

【问题讨论】:

    标签: php wordpress plugins woocommerce shipping


    【解决方案1】:

    更新与运输方式的税收计算有关。

    您的代码中有一些错误,例如:

    add_filter( 'woocommerce_package_rates', array( $this , 'conditional_shipping', 10, 2 ) );
    

    ……应该改为:

    add_filter( 'woocommerce_package_rates', array( $this , 'conditional_shipping' ), 10, 2 );
    

    或者在你的代码末尾:

    new Class_name();
    

    ……应该改为:

    $GLOBALS['class_name'] = new Class_name();
    

    所以你应该试试这样的完整代码(之前清空购物车)

    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    if ( ! class_exists( 'Class_Name' ) ) {
    
        class Class_Name {
    
            public function __construct() {
                //Custom admin menu
                add_action( 'admin_menu', array( $this,'custom_submenu_page'));
                
                add_filter( 'woocommerce_package_rates', array( $this , 'conditional_shipping' ), 10, 2 );
            }
    
            public function conditional_shipping( $rates, $package ) {
                // HERE Defined Method rate ID
                $rate_key = 'flat_rate:32';
                
                // HERE Define New cost
                $new_cost = 100;
                
                ## -- 1. Set the rate cost -- ##
    
                if ( isset( $rates[$rate_key] ) ){
                    // Get original cost
                    $original_cost = $rates[$rate_key]->cost;
                    // Set the new cost
                    $rates[$rate_key]->cost = number_format($new_cost, 2);
                    // calculate the conversion rate (for taxes)
                    $conversion_rate = $new_cost / $original_cost;
                }
                
                ## -- 2. Taxes rate cost calculation (if enabled) -- ##
                
                $taxes = array();
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // set the new line tax cost in the array
                        $taxes[$key] = number_format($tax * $conversion_rate, 2);
                    }
                }
                // Set the new taxes costs
                $rates[$rate_key]->taxes = $taxes;
                
                ## -- Return new costs -- ##
                return $rates;
            }
        }
        $GLOBALS['class_name'] = new Class_Name();
    }
    

    此代码位于插件文件中。它已经过测试并且可以工作。

    您可能需要刷新送货方式

    转到 WooCommerce 运输设置并在您的“统一费率”的相关运输区域中,禁用/保存并重新启用/保存相应的“统一费率”。

    【讨论】:

    • 嗨@LoicTheAztec!感谢您的回答,我会一一尝试进行更改以确保您是对的,问题是由“)”引起的。也感谢其他人的指正
    • 但是我一点儿也不懂,我用你之前的问题link的回答,这次税收计算不起作用。
    • @Efbi 已更新和测试。它也适用于税收。试试看。
    猜你喜欢
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2017-05-28
    • 2011-09-16
    • 1970-01-01
    • 2014-09-03
    相关资源
    最近更新 更多