douzhaishan5462 2019-04-08 02:25
浏览 72

根据所选付款方式更改Woocommerce购物车项目税级

I have tried to implement code found on this site in a number of answers from a particular user with regard to refreshing the checkout based on a change to the payment gateway selected or to some other field change. However, when the JS is included in my function file, my checkout is stuck and I have ajax loading animated circles.

I've already tried to adapt code from:

Trigger ajax update_checkout event on shipping methods change in Woocommerce

Update checkout ajax event when choosing a payment gateway in Woocommerce

On country change ajax update checkout for shipping in Woocommerce

Change Pay button on checkout based on Woocommerce chosen payment method

add_filter( "woocommerce_product_get_tax_class", "woo_diff_rate_for_user", 1, 2 );
add_filter( "woocommerce_product_variation_get_tax_class", "woo_diff_rate_for_user", 1, 2 );
function woo_diff_rate_for_user( $tax_class, $product ) {

// Get the chosen payment gateway (dynamically)
$chosen_payment_method = WC()->session->get('chosen_payment_method');

 if( $chosen_payment_method == 'wdc_woo_credits'){
        $tax_class = "Zero rate";
    } 

<script type="text/javascript">
        (function($){
            $('form.checkout').on( 'change', 'input[name^="payment_method"]', function() {
                var t = { updateTimer: !1,  dirtyInput: !1,
                    reset_update_checkout_timer: function() {
                        clearTimeout(t.updateTimer)
                    },  trigger_update_checkout: function() {
                        t.reset_update_checkout_timer(), t.dirtyInput = !1,
                        $(document.body).trigger("update_checkout")
                    }
                };
                $(document.body).trigger('update_checkout')
            });
        })(jQuery);
    </script>
     return $tax_class;
}

If I don't include the JS/jQuery, my function, which changes the tax class based on payment option works when the shipping method is changed and the page refreshes on change. But I need the checkout to refresh when the payment gateway is changed not when the shipping is changed.

  • 写回答

1条回答 默认 最新

  • duanliaolan6178 2019-04-08 08:56
    关注

    You can't include this kind of jQuery script in a filter hook and there is mistakes in your code. Anyways you are not using the right code even for the tax class change.

    The replacement code:

    add_action( 'woocommerce_before_calculate_totals', 'change_tax_class_based_on_payment_method', 10, 1 );
    function change_tax_class_based_on_payment_method( $cart ) {
        // Only for a specific defined payment meyhod
        if ( WC()->session->get('chosen_payment_method') !== 'wdc_woo_credits' )
            return;
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ){
            // We set "Zero rate" tax class
            $cart_item['data']->set_tax_class("Zero rate");
        }
    }
    
    add_action('wp_footer', 'payment_methods_trigger_update_checkout');
    function payment_methods_trigger_update_checkout() {
        if( is_checkout() && ! is_wc_endpoint_url() ) :
        ?>
        <script type="text/javascript">
            jQuery(function($){
                $( 'form.checkout' ).on('change', 'input[name="payment_method"]', function() {
                    $(document.body).trigger('update_checkout');
                });
            });
        </script>
        <?php
        endif;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    If you use Woo Credits plugin, the correct payment ID is woo_credits, but not wdc_woo_credits.

    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改