在Woocommerce中基于选择的付款方式禁用送货方式 [英] Disable shipping method based on chosen payment method in Woocommerce
问题描述
如果用户选择付款货到付款",我需要禁用特定的送货方式.问题是以下代码仅在我重置时有效 每次都会刷新WooCommerce并刷新.来回选择用户时不起作用.
I need to disable specific shipping method if user selected payment "Cash on Delivery". The problem is that the following code works only if I reset WooCommerce transients each time and refresh. It doesn't work on user selection back and forth.
add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );
function alter_shipping_methods( $rates ) {
$chosen_gateway = WC()->session->chosen_payment_method;
// If payment is Cash on delivery remove specific shipping
if($chosen_gateway == 'cod') {
foreach ( $rates as $rate_id => $rate ) {
if ( $rate->label === 'Hrvatska pošta' ) {
unset( $rates[ $rate_id ] );
}
}
}
return $rates;
}
我确实具有应触发的代码,当我单击选项时,我会在控制台中看到输出.
I do have this code which should trigger and I see the output in console when I click around options.
jQuery(document.body).on('change', 'input[name="payment_method"]', function() {
console.log('Payment method changed');
jQuery('body').trigger('update_checkout');
});
我已经尝试过了,但是没用
I have tried with this, it doesn't work
function action_woocommerce_checkout_update_order_review($array, $int) {
WC()->cart->calculate_shipping();
return;
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);
我还尝试了自定义AJAX调用,该调用调用了PHP函数,并且在此过滤器内部,没有结果
And I have also tried custom AJAX call which calls a PHP function and inside this filter, no result
add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );
接下来我应该尝试什么?
What should I try next?
推荐答案
已于2019年3月更新
这是使"COD"付款方式禁用特定运输方式的完整工作方式.
Here is the complete working way to make "COD" payment method disable a specific shipping method.
您必须在第一个功能中设置您要隐藏的运输方式ID.
You will have to set in the first function the shipping method Id that you wish to hide.
代码:
add_action( 'woocommerce_package_rates','show_hide_shipping_methods', 10, 2 );
function show_hide_shipping_methods( $rates, $package ) {
// HERE Define your targeted shipping method ID
$payment_method = 'cod';
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method ){
unset($rates['flat_rate:12']);
}
return $rates;
}
add_action( 'woocommerce_review_order_before_payment', 'payment_methods_trigger_update_checkout' );
function payment_methods_trigger_update_checkout(){
// jQuery code
?>
<script type="text/javascript">
(function($){
$( 'form.checkout' ).on( 'blur', 'input[name^="payment_method"]', function() {
setTimeout(function(){
$(document.body).trigger('update_checkout');
}, 250 );
});
})(jQuery);
</script>
<?php
}
add_action( 'woocommerce_checkout_update_order_review', 'refresh_shipping_methods' );
function refresh_shipping_methods( $post_data ){
// HERE Define your targeted shipping method ID
$payment_method = 'cod';
$bool = true;
if ( WC()->session->get('chosen_payment_method') === $payment_method )
$bool = false;
// Mandatory to make it work with shipping methods
foreach ( WC()->cart->get_shipping_packages() as $package_key => $package ){
WC()->session->set( 'shipping_for_package_' . $package_key, $bool );
}
WC()->cart->calculate_shipping();
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
要获取正确的送货方式ID ,您可以使用浏览器检查器,方法是:
To be able to get the correct shipping method ID you can use your browser inspector, this way:
这篇关于在Woocommerce中基于选择的付款方式禁用送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!