首页 文章

在woocommerce结帐时添加免税表格

提问于
浏览
3

我正在尝试将一个表单添加到我的结帐页面,因此当用户单击“免税”复选框时,将弹出一个文本框并询问用户免税标识号是多少 .

我把所有这些工作都很好,我甚至将'update_totals_on_change'类添加到我的表单字段中,以便更新总计 .

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

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

查看WooCommerce中的checkout.js文件,他们将操作设置为'woocommerce_update_order_review'以进行ajax操作 .

我试着跟着它,但很快就迷路了 .

我想我可以通过挂钩'woocommerce_checkout_update_order_review'来添加一些帖子数据

然后挂钩到'woocommerce_before_calculate_totals'修改税收,但我不知道我需要修改什么 .

我甚至走在正确的道路上吗?

提前致谢!

4 回答

  • 5

    接受的解决方案对我不起作用,但我修改它以使用以下内容:

    //=============================================================================
    // 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/找到了这个答案

  • 0

    因为 $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' );
    
  • 2

    好吧,我终于想出来以防万一有兴趣 .

    在我的插件中,我通过勾选到此函数在订单注释后创建了一个表单:'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文件的序列化表单数据,并检查我的部分表单是否填写正确 .

    如果是,那么我会为用户设置免税 .

  • 3

    经过长时间的搜索后,我发现有一个名为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' );
    

相关问题