购物车中的WooCommerce过滤器woocommerce_adjust_non_base_location_prices

时间:2020-03-30 10:12:19

标签: php wordpress woocommerce hook-woocommerce

我尝试显示基于客户所在国家/地区的价格(价格包括基于国家/地区的税费)。这很好。无论他住哪里,客户始终要支付119.00欧元。只有税收在调整。

这仅适用于以下代码:

add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );

现在我面临以下挑战:有些国家的税率为0%,因为它们在自己的国家/地区(例如挪威)纳税。如果税率为0%,我想收取不含税的价格(我们商店的标准税率为19%),在挪威,则仅为100.00欧元。

所以我尝试了以下代码:

function disable_woo_vat_adjustment(){

    $total_tax = WC()->cart->get_subtotal_tax();

    if( isset( $total_tax ) && $total_tax > 0 ){
        add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
    }

}

do_action( 'after_setup_theme', 'disable_woo_vat_adjustment' );

此代码仍适用于挪威,价格为100.00欧元,这是正确的。但是现在其他所有国家/地区的价格都是错误的,因为价格并不总是相同(119.00欧元),而是100.00欧元+税(奥地利20.00%=> 120.00欧元,而不是119.00欧元)。

我试图在if子句中添加第二个检查:

function disable_woo_vat_adjustment(){

    $total_tax = WC()->cart->get_subtotal_tax();

    if( isset( $total_tax ) && !empty( $total_tax ) && $total_tax > 0 ){
        add_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
    }else{
        remove_filter( 'woocommerce_adjust_non_base_location_prices', '__return_false' );
    }

}

do_action( 'after_setup_theme', 'disable_woo_vat_adjustment' );

我也尝试过不删除该功能,而是在else条件下将过滤器设置为__return_true,但在所有其他国家/地区(例如奥地利)的效果都不理想。

我想念什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

未测试,我的意思是,您可以通过这种方式使用此过滤器,而无需after_setup_theme

也许这会对您有所帮助?

function filter_woocommerce_adjust_non_base_location_prices( $passed ) {
    // $passed: DEFAULT = true
    $total_tax = WC()->cart->get_subtotal_tax();

    if( isset( $total_tax ) && $total_tax > 0 ){
        $passed = false;
    }       

    return $passed; 
}
add_filter( 'woocommerce_adjust_non_base_location_prices', 'filter_woocommerce_adjust_non_base_location_prices', 10, 1 );