This snippet allows you to enable shipping only for selected products, providing greater control over shipping policies.
add_filter('woocommerce_cart_shipping_method_full_label', 'restrict_shipping_to_specific_products', 10, 2);
function restrict_shipping_to_specific_products($label, $method) {
if (is_cart() || is_checkout()) {
$allowed_products = array(123, 456); // Replace with your product IDs
foreach (WC()->cart->get_cart() as $cart_item) {
if (!in_array($cart_item['product_id'], $allowed_products)) {
return ''; // Remove shipping method if product not allowed
}
}
}
return $label;
}