我正在尝试 Build 一个woocommerce商店,以便具有批发商或设计师角色的用户将自动免税,只需从购物车/结账中消税 . 我使用动态定价插件为不同的角色提供不同的价格,但税收变化没有选项 .
有人发布了这段代码:
// Place the following code in your theme's functions.php file and replace tax_exempt_role with the name of the role to apply to
add_action( 'init', 'woocommerce_customer_tax_exempt' );
function woocommerce_customer_tax_exempt() {
global $woocommerce;
if ( is_user_logged_in() ) {
$tax_exempt = current_user_can( 'tax_exempt_role');
$woocommerce->customer->set_is_vat_exempt( $tax_exempt );
}
}
这似乎是在前端工作但打破了后端 . 将它添加到functions.php后,当我回到管理区域并看到:http://i.imgur.com/nNHMSAZ.png(这只是新的Chrome错误页面?)
我无法弄清楚的另一件事是如何添加2个角色而不只是一个角色 .
谢谢
2 回答
以下为我的用户角色“批发商”工作 . 添加到functions.php .
要添加多个用户角色,只需添加到
current_user_can();
函数即可 . 我认为这可行:我注意到在使用 'woocommerce_before_checkout_billing_form' 时,您必须先更新或刷新结帐页面,然后您必须刷新购物车页面才能使其生效 .
使用这些操作挂钩 'woocommerce_before_cart_contents' 和 'woocommerce_before_shipping_calculator' 免税,无需先刷新结帐页面即可生效 .
注意:使用与上面相同的回调函数代码 .