Woocommerce 在结帐页面触发“订单审核”更新

我正在使用Jeroen Sormani的Woocommerce Advanced Shipping 插件作为运输方式,并使用Karolína Vyskočilová的WooCommerce Pay for Payment 插件向“货到付款”付款方式添加 5 欧元的固定费用。


使用 Advanced Shipping Plugin,我还创建了一个称为“本地取货”的一般规则,以便客户始终可以看到。Then I needed to hide the "cash on delivery" payment method when the "local pickup" shipping option is selected. 为此,我必须在我的functions.php 文件中添加一些代码,以便在高级运输规则中识别特定的本地取货规则:


function my_custom_available_payment_gateways( $gateways ) {

    $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods' );

    // When 'local pickup' (rule id 21828) has been chosen as shipping rate

    if ( in_array( '21828', $chosen_shipping_rates ) ) :

        // Unset 'cash on delivery'

        unset( $gateways['cod'] );

    endif;

return $gateways;

}

add_filter( 'woocommerce_available_payment_gateways', 'my_custom_available_payment_gateways' );

这是我的问题:在结帐页面中,当我切换到“本地取货”时,“货到付款”选项如前所述消失了,但订单审核表中的 5 欧元费用保留在那里。我必须再次手动切换付款方式(即从银行转账到信用卡)才能使费用消失。


我需要找到一些解决方案来在选择本地取货时触发订单审核更新/刷新。


我尝试在结帐页面中插入以下脚本但没有成功


function woocommerce_add_update_cart() {

    // Only on checkout page

    if( ! is_checkout() ) return;

    ?>

    <script type="text/javascript">

    jQuery(document.body).trigger("update_checkout");

    </script>

    <?php

}

add_action( 'wp_footer', 'woocommerce_add_update_cart' );


慕勒3428872
浏览 144回答 2
2回答

浮云间

您可以添加以下代码片段 -function refresh_checkout_on_payment_methods_change(){&nbsp; &nbsp; ?>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; (function($){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( 'form.checkout' ).on( 'change', 'input[name^="shipping_method"]', function() {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('body').trigger('update_checkout');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; })(jQuery);&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php}add_action( 'woocommerce_review_order_before_payment', 'refresh_checkout_on_payment_methods_change' );

ibeautiful

我设法解决了我的问题。$('body').trigger('update_checkout');itzmekhokan 建议的功能不起作用,以便从更新的订单审查中删除额外费用。我必须模拟点击其中一种付款方式单选按钮来触发结帐更新。对于结帐页面:function woocommerce_add_update_checkout() {&nbsp; &nbsp; // Only on checkout page&nbsp; &nbsp; if( ! is_checkout() ) return;&nbsp; &nbsp; ?>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; (function($){&nbsp; &nbsp; &nbsp; &nbsp; $('form.checkout').on( 'change', 'input[name^="shipping_method"]', function() {&nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("payment_method_bacs").checked = true;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; })(jQuery);&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php}add_action( 'wp_footer', 'woocommerce_add_update_checkout' );对于购物车页面摘要:function reset_payment_method() {&nbsp; &nbsp; // Only on cart page&nbsp; &nbsp; if( ! is_cart() ) return;&nbsp; &nbsp; $payment_method = WC()->session->get( 'chosen_payment_method' );&nbsp; &nbsp; if ( $payment_method = 'cod' ) :&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->set( 'chosen_payment_method', 'bacs' );&nbsp; &nbsp; endif;}add_filter( 'woocommerce_before_shipping_calculator', 'reset_payment_method' );
打开App,查看更多内容
随时随地看视频慕课网APP