嘿,我添加了两种付款方式,COD 和卡付款,并且有两种送货方式,一种用于 COD,一种用于卡,在 Woocommerce COD 设置中,我禁用了用于卡付款的送货方式,但我的卡设置没有该选项,所以当选择卡付款时,我使用代码隐藏 COD 运输,但是当我这样做时,我无法再更改付款方式或运输方式。
add_action( 'woocommerce_package_rates','show_hide_shipping_methods', 10, 2 );
function show_hide_shipping_methods( $rates, $package ) {
$payment_method = 'monri';
$payment_method1 = 'bacs';
$chosen_payment_method = WC()->session->get('chosen_payment_method');
if( $payment_method == $chosen_payment_method || $payment_method1 == $chosen_payment_method ){
unset($rates['flexible_shipping_single:10']);
}
return $rates;
}
我想要的只是在结帐时同时拥有 COD 和卡付款按钮,并且当选择它们时,会选择正确的运输方式
这就是我解决问题的方法,不是最干净的方法,但它有效。
add_filter('woocommerce_available_payment_gateways', 'filter_payment_gateways_based_on_shipping', 10, 1);
function filter_payment_gateways_based_on_shipping($available_gateways) {
// Check if specific shipping method is selected
if (is_checkout()) {
$chosen_shipping_method = WC()->session->get('chosen_shipping_methods')[0];
// List of shipping methods for which you want to disable payment methods
$shipping_methods_to_check = array('flexible_shipping_single:4'); // Adjust with your shipping method IDs, you can get these in many way, easiest, inspect the checkbox on payment page
// List of payment methods to be disabled
$payment_methods_to_disable = array('cod'); // Adjust with your payment method IDs
if (in_array($chosen_shipping_method, $shipping_methods_to_check)) {
foreach ($payment_methods_to_disable as $method_id) {
if (isset($available_gateways[$method_id])) {
unset($available_gateways[$method_id]);
}
}
}
// List of shipping methods for which you want to disable payment methods
$shipping_methods_to_check = array('flexible_shipping_single:13'); // Adjust with your shipping method IDs, you can get these in many way, easiest, inspect the checkbox on payment page
// List of payment methods to be disabled
$payment_methods_to_disable = array('monri', 'bacs'); // Adjust with your payment method IDs
if (in_array($chosen_shipping_method, $shipping_methods_to_check)) {
foreach ($payment_methods_to_disable as $method_id) {
if (isset($available_gateways[$method_id])) {
unset($available_gateways[$method_id]);
}
}
}
}
return $available_gateways;
}