【发布时间】:2018-10-28 22:59:01
【问题描述】:
如果用户选择“货到付款”付款,我需要禁用特定的送货方式。问题是以下代码仅在我重置时才有效 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);
我还尝试了自定义 AJAX 调用,它调用了一个 PHP 函数,在这个过滤器中,没有结果
add_filter( 'woocommerce_package_rates', 'alter_shipping_methods', 100 );
接下来我应该尝试什么?
【问题讨论】:
标签: php jquery ajax woocommerce shipping