php - 根据 Woocommerce 中选择的付款方式禁用运输方式
问题描述
如果用户选择“货到付款”付款,我需要禁用特定的运输方式。问题是以下代码仅在我每次重置 WooCommerce 瞬态并刷新时才有效。它不适用于来回用户选择。
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;
}
我确实有这段代码应该触发,当我点击选项时,我会在控制台中看到输出。
jQuery(document.body).on('change', 'input[name="payment_method"]', function() {
console.log('Payment method changed');
jQuery('body').trigger('update_checkout');
});
这个我试过了,不行
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);
而且我还尝试了调用 PHP 函数的自定义 AJAX 调用,并且在此过滤器中,没有结果
add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );
接下来我应该尝试什么?
解决方案
2019 年 3 月更新
对于 COD 支付网关,您只需在其设置中添加您想要为其启用的“统一费率”运输方式,例如:
对于 Cod 和其他方法或其他支付网关,这是为特定支付网关禁用特定运输方式的完整工作方式。
您必须在第一个函数中设置您希望隐藏的运输方式 ID。
编码:
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( 'change 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 文件中。测试和工作。
为了能够获得正确的运输方式 ID,您可以使用浏览器检查器,这样:
在测试此代码之前,您可能需要清空购物车。
推荐阅读
- javascript - 将数据存储在数组数组的变量中,以便在 Reactjs 中创建平面数组
- matlab - 运行代码计算 ODE 的欧拉方法
- node.js - 使用 Greenlock-Express 的通配符域的 SNI 错误
- javascript - 使用jQuery单击按钮时无法显示输入中的文本
- intellij-idea - 如何在 IntelliJ 中设置多行注释的格式
- node.js - 当您点击 URL 时,nodejs 中的请求和响应
- javascript - Neataptic XOR 仅使用 3 个节点求解
- vue.js - Vue + Axios Post 请求响应数据返回 undefined
- json - 在 golang 中动态创建嵌套地图
- c - 无法理解在小端机器中用 c 进行类型转换后产生的输出