[英]Woocommerce trigger the "order review" update in checkout page
I'm using the Woocommerce Advanced Shipping plugin by Jeroen Sormani for the shipping methods and the WooCommerce Pay for Payment plugin by Karolína Vyskočilová to add a €5 fixed fee to the "cash on delivery" payment method.我使用 Jeroen Sormani 的Woocommerce Advanced Shipping 插件作为运输方式,使用 Karolína Vyskočilová 的WooCommerce Pay for Payment 插件为“货到付款”支付方式增加 5 欧元的固定费用。
Using the Advanced Shipping Plugin, I also created a general rule called "Local pickup" to be always visible to the customers.使用 Advanced Shipping Plugin,我还创建了一个名为“Local pickup”的通用规则,以始终对客户可见。 Then I needed to hide the "cash on delivery" payment method when the "local pickup" shipping option is selected.
然后我需要在选择“本地取货”送货选项时隐藏“货到付款”付款方式。 To achieve this I had to add some code to my functions.php file in order to identify the specific local pickup rule inside the advanced shipping rules:
为此,我必须在我的 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' );
Here is my problem: In the checkout page, when I switch to "local pickup", the "cash on delivery" option disappears as said before, but the €5 fee in the order review table stays there.这是我的问题:在结帐页面,当我切换到“本地取货”时,“货到付款”选项如前所述消失,但订单审核表中的 5 欧元费用保留在那里。 I have to manually switch the payment method again (ie from bank transfer to credit card) in order to make the fee disappear.
我必须再次手动切换付款方式(即从银行转帐到信用卡)以使费用消失。
I need to find some solution to trigger the order review update/refresh when the local pickup is selected.我需要找到一些解决方案来在选择本地取件时触发订单审核更新/刷新。
I tried inserting the following script in the checkout page with no success我尝试在结帐页面中插入以下脚本但没有成功
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' );
You can add the follows code snippet - 您可以添加以下代码段-
function refresh_checkout_on_payment_methods_change(){
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'change', 'input[name^="shipping_method"]', function() {
$('body').trigger('update_checkout');
});
})(jQuery);
</script>
<?php
}
add_action( 'woocommerce_review_order_before_payment', 'refresh_checkout_on_payment_methods_change' );
I managed to solve my problem. 我设法解决了我的问题。 The
$('body').trigger('update_checkout');
$('body').trigger('update_checkout');
function suggested by itzmekhokan is not working in order to remove the extra fee from the updated order review. itzmekhokan建议的功能无法正常运行,以便从更新的订单审核中删除额外费用。 I had to simulate the click on one of the payment methods radio buttons to trigger the checkout update.
我必须模拟对其中一种付款方式单选按钮的点击才能触发结帐更新。
For the checkout page: 对于结帐页面:
function woocommerce_add_update_checkout() {
// Only on checkout page
if( ! is_checkout() ) return;
?>
<script type="text/javascript">
(function($){
$('form.checkout').on( 'change', 'input[name^="shipping_method"]', function() {
document.getElementById("payment_method_bacs").checked = true;
});
})(jQuery);
</script>
<?php
}
add_action( 'wp_footer', 'woocommerce_add_update_checkout' );
For the cart page summary: 对于购物车页面摘要:
function reset_payment_method() {
// Only on cart page
if( ! is_cart() ) return;
$payment_method = WC()->session->get( 'chosen_payment_method' );
if ( $payment_method = 'cod' ) :
WC()->session->set( 'chosen_payment_method', 'bacs' );
endif;
}
add_filter( 'woocommerce_before_shipping_calculator', 'reset_payment_method' );
update "order review" does not work if in template "checkout/review-order.php" has no wrapper with css class "shop_table woocommerce-checkout-review-order-table"如果在模板“checkout/review-order.php”中没有带有 css class“shop_table woocommerce-checkout-review-order-table”的包装器,则更新“订单审查”不起作用
example例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.