Php 在自定义插件文件中为WooCommerce shipping添加过滤器问题

Php 在自定义插件文件中为WooCommerce shipping添加过滤器问题,php,wordpress,plugins,woocommerce,shipping,Php,Wordpress,Plugins,Woocommerce,Shipping,以下是我的自定义插件代码: class Class_name { public function __construct() { if ( is_admin() ) { //Custom admin menu add_action( 'admin_menu', array( $this,'custom_submenu_page')); add_filter( 'woocommerce_p

以下是我的自定义插件代码:

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();
我想用这个过滤器,但他不工作。前端什么也没发生。我不知道哪里出错了


我不应该使用过滤器?

更新与装运方法的税务计算相关的

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

add_filter( 'woocommerce_package_rates', array( $this , 'conditional_shipping', 10, 2 ) );
……这应该是:

add_filter( 'woocommerce_package_rates', array( $this , 'conditional_shipping' ), 10, 2 );
$GLOBALS['class_name'] = new Class_name();
或者在代码末尾:

new Class_name();
……这应该是:

add_filter( 'woocommerce_package_rates', array( $this , 'conditional_shipping' ), 10, 2 );
$GLOBALS['class_name'] = new Class_name();
因此,您应该尝试以下完整代码(在之前清空购物车):

这段代码放在一个插件文件中。它已经过测试,可以工作了

您可能需要刷新发货方法

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


嗨@LoicTheAztec!谢谢你的回答,我会一个接一个地尝试每一个更改,你是对的,问题是由“)”引起的。也感谢其他人的更正,但我不太明白,我使用了您以前问题的答案,这次税收计算不起作用。@Efbi已更新并测试。它也适用于税收。试试看。