I am looking to remove the shipping from few variable products in WooCommerce. I need that few products from cart directly go to PayPal.
I added the following code in my theme s functions.php file:
add_filter( woocommerce_checkout_fields , disable_address_validation_for_specific_products );
function disable_address_validation_for_specific_products( $fields ) {
// Enter the product IDs of the variable products for which you want to disable address validation
$product_ids = array(12, 34, 56);
if (!empty(WC()->cart)) {
foreach (WC()->cart->get_cart() as $cart_item) {
$product_id = $cart_item[ product_id ];
if (in_array($product_id, $product_ids)) {
unset($fields[ billing ][ billing_address_1 ][ validate ]);
unset($fields[ billing ][ billing_city ][ validate ]);
unset($fields[ billing ][ billing_postcode ][ validate ]);
unset($fields[ billing ][ billing_country ][ validate ]);
unset($fields[ billing ][ billing_state ][ validate ]);
break; // Stop the loop after finding the first matching product
}
}
}
return $fields;
}
add_filter( woocommerce_cart_needs_shipping_address , __return_false );
Now, for all products, shipping is disabled, what I don t want.
Also, I get those errors on the checkout page:
- Error# 1 No shipping method has been selected. Please double-check your address, or contact us if you need any help.
- Error # 2 Please enter an address to continue.
What I am doing wrong? How can I fix this?