如何在woocommerce中实现特定产品的免费送货并设置默认送货

问题描述

function reset_default_shipping_method( $method,$available_methods ) 
{
   foreach( WC()->cart->get_cart() as $cart_item ) {
     $product_in_cart = $cart_item['product_id'];
     if($product_in_cart == '12101' ){
        $default_method = 'free_shipping:13'; 
     } else{
        unset( $available_methods['free_shipping:13'] );
     }
   }          
  return $default_method;   
}

add_filter('woocommerce_shipping_chosen_method','reset_default_shipping_method',10,2);

解决方法

function reset_default_shipping_method( $method,$available_methods ) {
  foreach( WC()->cart->get_cart() as $cart_item ) {
    $product_in_cart = $cart_item['product_id'];
    if($product_in_cart == '12101' ){
      $default_method = 'free_shipping:13'; 
    }else{
      unset( $available_methods['free_shipping:13'] );
    }
  }          
  return $default_method;   
    
} add_filter('woocommerce_shipping_chosen_method','reset_default_shipping_method',10,2);