2013-05-31 61 views
2

我想一个表单,以便当用户点击了“免税”复选框添加到我的结帐页面,文本框会弹出并问清免税的身份证号码是用户。结账加入免税形式woocommerce

我得到了一切工作的伟大,我甚至增加了“update_totals_on_change”类我的表单字段,以便其将更新汇总。

我的下一步是在一个方法上添加一个动作/过滤器,所以当'update_totals_on_change'执行时,我可以将税率设置为0,然后它将完成总计算。

有谁知道我可以挂钩哪些功能?

综观WooCommerce的checkout.js文件,他们将操作设置为AJAX操作“woocommerce_update_order_review”。

我试过了,但很快就迷路了。

我想我可以通过为“woocommerce_checkout_update_order_review”

挂钩,然后钩到“woocommerce_before_calculate_totals”修改税的东西添加一些数据后,但我不知道我需要修改。

我是否在正确的道路上?

感谢先进!

回答

5

好吧,我终于想通了,以防万一有人感兴趣。

在我的插件,我通过挂钩到这个功能订单的注释后,做了一个表格:“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文件的序列化表单数据,并检查我的表单部分是否填写正确。

如果是,那么我将设置免税的用户。

2

经过长时间的搜寻后,我发现,对于车对象调用remove_taxes()的方法。 因此,在为免税用户设置用户元后,会取消税收总额。

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'); 
+0

感谢您发布该答案。我已经添加了一个自定义用户配置文件字段,并跟踪用户是否免税,但不知道如何为这些用户删除税款。这是票! – cce1911

+0

remove_taxes()自3.2开始已弃用 - 将用户设置为is_tax_exempt(true)是执行此操作的新方法(根据代码中的注释)。 – richplane

2

接受的解决方案并没有为我工作,但我修改它使用以下命令:

//============================================================================= 
// 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/

+1

谢谢!救生员 –

+1

'class'是要应用于该字段的CSS类的列表,因此它需要是空数组而不是空字符串。 – richplane

+0

@richplane,我根据你的评论更新了我的答案。谢谢。 –