我正在尝试将一个表单添加到我的结帐页面,因此当用户单击“免税”复选框时,将弹出一个文本框并询问用户免税标识号是多少。
我把所有这些工作都很好,我甚至将update_totals_on_change
类添加到我的表单字段中,以便更新总计。
我的下一步是在方法上添加一个动作/过滤器,所以当update_totals_on_change
执行时,我可以将税率设置为0然后它将完成计算总数。
有谁知道我可以挂钩的功能?
看看checkout.js
中的WooCommerce
文件,他们为woocommerce_update_order_review
操作设置了ajax
的动作。
我尝试过这样,但很快就迷路了。
我想我可以通过挂钩到woocommerce_checkout_update_order_review
添加一些帖子数据
然后挂钩到woocommerce_before_calculate_totals
修改税务,但我不知道我需要修改什么。
我甚至走在正确的道路上吗?
好吧,我终于想出来以防万一有兴趣。
在我的插件中,我通过勾选到此函数在订单注释后创建了一个表单:'woocommerce_before_order_notes'
add_action('woocommerce_before_order_notes', array(&$this, 'taxexempt_before_order_notes') );
我的'taxexempt_before_order_notes'功能包含:
function taxexempt_before_order_notes( $checkout ) {
echo '<div style="clear: both"></div>
<h3>Tax Exempt Details</h3>';
woocommerce_form_field( 'tax_exempt_checkbox', array(
'type' => 'checkbox',
'class' => array('tiri taxexempt'),array( 'form-row-wide', 'address-field' ),
'label' => __('Tax Exempt'),
), $checkout->get_value( 'tax_exempt_checkbox' ));
woocommerce_form_field( 'tax_exempt_name', array(
'type' => 'text',
'class' => array('form-row-first', 'tiri', 'taxexempt', 'textbox', 'hidden'),
'label' => __('Tax Exempt Name'),
), $checkout->get_value( 'tax_exempt_name' ));
woocommerce_form_field( 'tax_exempt_id', array(
'type' => 'text',
'class' => array('form-row-last', 'tiri', 'taxexempt', 'textbox', 'hidden', 'update_totals_on_change'),
'label' => __('Tax Exempt Id'),
), $checkout->get_value( 'tax_exempt_id' ));
}
然后最重要的woocommerce功能是:'woocommerce_checkout_update_order_review'
add_action( 'woocommerce_checkout_update_order_review', array(&$this, 'taxexempt_checkout_update_order_review' ));
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($tax_exempt_checkbox) && isset($tax_exempt_id) && $tax_exempt_checkbox == '1' && !empty($tax_exempt_id))
$woocommerce->customer->set_is_vat_exempt(true);
}
我只是解析了$ post_data,这是来自woocommerce中checkout.js文件的序列化表单数据,并检查我的部分表单是否填写正确。
如果是,那么我会为用户设置免税。
已接受的解决方案对我不起作用,但我修改它以使用以下内容:
//=============================================================================
// ADD TAX EXEMPT CHECKMARK
// =============================================================================
add_action( 'woocommerce_after_order_notes', 'qd_tax_exempt');
function qd_tax_exempt( $checkout ) {
echo '<div id="qd-tax-exempt"><h3>'.__('Tax Exempt').'</h3>';
woocommerce_form_field( 'shipping_method_tax_exempt', array(
'type' => 'checkbox',
'class' => array(),
'label' => __('My organization is tax exempt.'),
'required' => false,
), $checkout->get_value( 'shipping_method_tax_exempt' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_review', 'taxexempt_checkout_update_order_review');
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($shipping_method_tax_exempt) && $shipping_method_tax_exempt == '1')
$woocommerce->customer->set_is_vat_exempt(true);
}
这里的关键是要理解名称以shipping_method
开头的任何字段都将继承此更新顺序功能(这是对我不起作用的部分)。我在http://www.affectivia.com/blog/have-a-checkbox-on-the-checkout-page-which-updates-the-order-totals/找到了这个答案
经过长时间的搜索,我发现有一个名为remove_taxes()的cart对象的方法。因此,在为免税用户设置用户元数据后,这将取消税收总额。
function remove_tax_for_exempt( $cart ) {
global $current_user;
$ok_taxexp = get_the_author_meta( 'granted_taxexempt', $current_user->ID );
if ($ok_taxexp){ // now 0 the tax if user is tax exempt
$cart->remove_taxes();
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
因为$cart->remove_taxes();
已被弃用。这就是我用来代替的。
我在前端没有表单,但是具有免税的用户角色。这是我的解决方案。
另外值得注意的是,set_is_vat_exempt(true)
也在美国工作,以免税。
/**
* Set customer as tax exempt if user is a wholesale customer
*/
function remove_tax_for_exempt( $cart ) {
global $woocommerce;
if ( is_user_logged_in() && current_user_can( 'wholesale_customer' ) ) {
$woocommerce->customer->set_is_vat_exempt(true);
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
由于这个答案仍然在谷歌上弹出,我想我会分享设置客户作为免税只在结账时有效,如果您需要在后端编辑后端的订单并使用“重新计算”按钮,税收仍将出现。幸运的是,这也有一个钩子:
function remove_tax_for_exempt($exempt, $order){
return $exempt || user_can($order->get_user_id(), 'wholesale_customer');
}
add_filter('woocommerce_order_is_vat_exempt', 'remove_tax_for_exempt', 10, 2);