购物车中的oocommerce过滤器woocommerce_adjust_non_base_location_prices

问题描述 投票:1回答:1

我尝试显示基于客户所在国家/地区的价格(价格包括基于国家/地区的税金)。这很好。无论他住哪里,客户始终要支付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,但在所有其他国家/地区(例如奥地利)都无法正常运行。

我想念的是什么?

谢谢。

php wordpress woocommerce hook
1个回答
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 ); 
© www.soinside.com 2019 - 2024. All rights reserved.