• Hello there,

    I’m using a function to block some countries of purchasing some certain items, but i can’t get it to work properly, because Woocommerce stores the shipping information and uses it to validate the checkout, even on non-logged in users.

    What i mean is, if have a product that can only be bought by people who are in Spain, if they make a previous purchase on the store for another country, when they try to buy the product that can only be bought to spain, they can’t get to the checkout form, because a message appears saying that there are errors in their cart.

    Currently, i’m using this function:

    add_action( 'woocommerce_check_cart_items', 'products_not_shipable' );
    function products_not_shipable() {
    
        if( is_checkout() ){
            
            // Set Products
            $products = array(14059, 14051, 14043, 14035, 14017, 13847, 13833, 13823, 13809, 13793, 13771, 13757, 13747, 13733, 13692, 13682, 13658, 13644, 13634, 13620, 13583, 14056, 14048, 14040, 14032, 14014, 13842, 13828, 13820, 13803, 13788, 13766, 13752, 13744, 13727, 13687, 13679, 13653, 13639, 13631, 13631, 13578);
    
            // Get customer country
            $country = WC()->session->get('customer')['shipping_country'];
            if( empty($country) ){
                $country = WC()->session->get('customer')['billing_country'];
            }
    
            // For Countrys
            $pais = array("DE","DK","SI","ES","FI","FR","GR","IT","PL","PT","UK");
    
            if ( !in_array($country,$pais)){
                // Loop through cart items
                foreach( WC()->cart->get_cart() as $item ){
                    // IF product is in cart
                    if( in_array( $item['product_id'], $products ) ){
                        // Avoid checkout and display an error notice
                        wc_add_notice( sprintf( __(" %s can't be shipped to your country.", "woocommerce" ), '"' . $item['data']->get_name() . '"'), 'error' );
                        break; // Stop the loop
                    }
                }
            }
        }
    }

    Is there a way to make it work like i need to?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi there ??

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers! ??

    Thread Starter lpaweb

    (@lpaweb)

    So, by changing my function i was able to achieve what i want.

    add_action( 'woocommerce_review_order_before_submit', 'products_not_shipable' );
    function products_not_shipable() {
        // Only on checkout page (allowing customer to change the country in cart shipping calculator)
        
        if( is_checkout() ){
            // Set your products
            $products = array(14059, 14051, 14043, 14035, 14017, 13847, 13833, 13823, 13809, 13793, 13771, 13757, 13747, 13733, 13692, 13682, 13658, 13644, 13634, 13620, 13583, 14056, 14048, 14040, 14032, 14014, 13842, 13828, 13820, 13803, 13788, 13766, 13752, 13744, 13727, 13687, 13679, 13653, 13639, 13631, 13631, 13578);
    
            // Get customer country
            $country = WC()->session->get('customer')['shipping_country'];
            if( empty($country) ){
                $country = WC()->session->get('customer')['billing_country'];
            }
    
            // For Countrys
            $pais = array("DE","DK","SI","ES","FI","FR","GR","IT","PL","PT","UK");
    
            if ( !in_array($country,$pais)){
                // Loop through cart items
                foreach( WC()->cart->get_cart() as $item ){
                    // IF product is in cart
                    if( in_array( $item['product_id'], $products ) ){
                        // Avoid checkout and display an error notice
                        wc_add_notice( sprintf( __(" %s can't be shipped to your country.", "woocommerce" ), '"' . $item['data']->get_name() . '"'), 'error' );
                        
                            echo '<script language="JavaScript">';
                            echo 'document.getElementById("place_order").disabled = true;';
                            echo '</script>';
                        
                        break; // Stop the loop
                    }
                }
            }
        }
    }

    I made it run on woocommerce_review_order_before_submit instead of woocommerce_check_cart_items and it works. Also, added a javascript code to disable the checkout button in case you don’t meet the requirements.

    But now i have a different problem.
    Every time i click on “ship to different address” and change the country on the shipping address the woocommerce runs a validation to check if all is ok to proceed with checkout and it runs my function, but if the “ship to different address” is not enabled, every time i change the country it doesn’t make any validation and lets the client place the order for countries that don’t meet the requirements.

    So, what i need now, is to know how can i make woocommerce run the checkout validation when the country on billing address changes.

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Never store the shipping information’ is closed to new replies.