ds3464
2018-05-18 13:25
浏览 84
已采纳

根据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);

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?

图片转代码服务由CSDN问答提供 功能建议

如果用户选择付款“货到付款”,我需要禁用特定的送货方式。 问题是,只有每次重置 WooCommerce瞬态并刷新时,以下代码才有效。 它不适用于来回的用户选择。

  add_filter('woocommerce_package_rates','alter_shipping_methods',100); 
 
ntort alter_shipping_methods($ rates){
 
 $ chosen_gateway = WC() - > session-  > chosen_payment_method; 
 
 //如果付款是货到付款,请删除特定的送货
如果($ chosen_gateway =='cod'){
 
 foreach($ rate as $ rate_id => $ rate){  
 if($ rate-> label ==='Hrvatskapošta'){
 unset($ rates [$ rate_id]); 
} 
} 
 
} 
 
返回$ rate;  
 
} 
   
 
 

我确实有这个代码可以触发,当我点击选项时,我会在控制台中看到输出。 \ n

  jQuery(document.body).on('change','input [name =“payment_method”]',function(){
 console.log('Payment method changed'); 
  jQuery('body')。触发器('update_checkout'); 
}); 
   
 
 

我试过这个,它不起作用

  function action_woocommerce_checkout_update_order_review($ array,$ int){\  n 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  ); 
   
 
 

我接下来应该尝试什么?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题