skip to Main Content

Search WordPress Code Snippets – Quick Solutions for Developers


Enable Shipping for Specific Products in WooCommerce

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;
}

I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

Back To Top