Woocommerce中update_checkout触发失效问题求助
问题:update_checkout触发函数跨浏览器兼容性异常
我开发的插件中,update_checkout触发函数存在兼容性问题:
- 使用
change事件触发时,完全无法生效; - 换成
blur事件后,Chrome、Opera、IE浏览器能正常触发,但Firefox和Safari完全无效。
业务背景:插件针对bacs支付方式提供5%折扣,需要在计算运费前先算出折扣后的商品小计(商品小计 - 折扣),若该金额低于1000则不提供免费运费;cheque支付方式无需折扣,这部分逻辑运行正常。
运费逻辑代码
add_filter( 'woocommerce_shipping_packages', function( $packages ) { $session_data = WC()->session->get_session_data(); //error_log(print_r($session_data,true)); $cart_totals = maybe_unserialize( $session_data['cart_totals'] ); $payment_method = 'bacs'; $chosen_payment_method = WC()->session->get('chosen_payment_method'); $sub_total_after_item_discount = $cart_totals['subtotal'] + $cart_totals['fee_total']; if( $payment_method == $chosen_payment_method && $sub_total_after_item_discount < 1000){ $free_shipping_id = 'free_shipping:7'; unset($packages[0]['rates'][ $free_shipping_id ]); } elseif ($payment_method == $chosen_payment_method && $sub_total_after_item_discount > 1000) { $flat_rate = 'flat_rate:6'; unset($packages[0]['rates'][ $flat_rate ]); } return $packages; } ); add_filter( 'woocommerce_shipping_packages', function( $packages ) { $session_data = WC()->session->get_session_data(); //error_log(print_r($session_data,true)); $cart_totals = maybe_unserialize( $session_data['cart_totals'] ); $payment_method = 'cheque'; $chosen_payment_method = WC()->session->get('chosen_payment_method'); $sub_total_after_item_discount = $cart_totals['total'] + $cart_totals['fee_total']; if( $payment_method == $chosen_payment_method && $sub_total_after_item_discount < 1000){ $free_shipping_id = 'free_shipping:7'; unset($packages[0]['rates'][ $free_shipping_id ]); } elseif ($payment_method == $chosen_payment_method && $sub_total_after_item_discount > 1000) { $flat_rate = 'flat_rate:6'; unset($packages[0]['rates'][ $flat_rate ]); } return $packages; } );
触发update_checkout的代码
add_action( 'woocommerce_review_order_before_payment', 'osvjezi_metodu_placanja' ); function osvjezi_metodu_placanja(){ // jQuery kod ?> <script type="text/javascript"> (function($){ $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() { $('body').trigger('update_checkout'); //console.log('blur', true);}); }); })(jQuery); </script> <?php }
内容的提问来源于stack exchange,提问作者bilkyroot
相关产品推荐
相关产品推荐

