Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

php - How to implement the free shipping on specific product in woocommerce and set the default shipping

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);
question from:https://stackoverflow.com/questions/65917356/how-to-implement-the-free-shipping-on-specific-product-in-woocommerce-and-set-th

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...