Search code examples
phpwordpresswoocommercecountryproduct-variations

Check variation items for specific string in the product name and allowed countries in WooCommerce


How can I add a list of allowed countries, and target products that only have a specific word in their title, as opposed to a variation id. For example, the word "framed" in "Framed A3 Poster" product title.

This would mean that I would avoid having to target each individual variation id, as I have +280 products actually.

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

Solution

  • Using strpos() PHP function will allow to find a string in cart item product name as follows (the code handle also multiple allowed country codes):

    add_action( 'woocommerce_check_cart_items', 'check_cart_items_for_shipping' );
    function check_cart_items_for_shipping() {
        $string_to_find    = 'framed';    // The string to find in product variation name (in lower case)
        $allowed_countries = array('IE'); // The allowed shipping country codes
        $shipping_country  = WC()->customer->get_shipping_country(); // Customer shipping country
    
        // Loop through cart items
        foreach(WC()->cart->get_cart() as $item ) {
            $product_name = $item['data']->get_name(); // Get the product name
            
            // Check cart item for a defined string in the product name and allowed country codes
            if( ! in_array( $shipping_country, $allowed_countries ) 
            && strpos( strtolower($product_name), $string_to_find ) === false ) {
                
                wc_clear_notices(); // Clear other existing notices
                
                $countries = WC()->countries->get_countries(); // Load WooCommerce countries
    
                // Avoid checkout displaying an error notice
                wc_add_notice( sprintf( __('The product "%s" can not be shipped to %s.'),
                    $product_name,
                    $countries[$shipping_country]
                ), 'error' );
                break; // stop the loop
            }
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.