Shipping restrictions for variable products with specific words in their product titles

Question

this question has been partially answered here: Shipping restrictions with WooCommerce variable products
..but I’d like to go one step further. How can I add a list of allowed countries, and target products that only have the word "framed" in their title (example: "Framed A3 Poster")

The below code is what i’m working with:

add_action( 'woocommerce_check_cart_items', 'check_cart_items_for_shipping' );
function check_cart_items_for_shipping() {
    $allowed_variation_id = '13021'; // Here defined the allowed Variation ID
    $allowed_country      = 'IE'; // Here define the allowed shipping country for all variations

    $shipping_country = WC()->customer->get_shipping_country();
    $countries        = WC()->countries->get_countries();

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        // Check cart item for defined product Ids and applied coupon
        if( $shipping_country !== $allowed_country && $cart_item['variation_id'] !== $allowed_variation_id ) {
            wc_clear_notices(); // Clear all other notices

            // Avoid checkout displaying an error notice
            wc_add_notice( sprintf( __('The product "%s" can not be shipped to %s.'),
                $cart_item['data']->get_name(),
                $countries[$shipping_country]
            ), 'error' );
            break; // stop the loop
        }
    }
}

Thanks in advance,
David

0
David Hayden 3 months 0 Answers 7 views 0

Leave an answer