0

In my woocommerce site, I list both products that are available for shipping to India as well as International destinations. Am assigning a only-india shipping class to certain products so as to mark them as available for India only.

Now am trying to add validation in the Checkout page so as to prevent users from completing the order, if they have selected an international destination, but having some only-india products in their cart along with the international products.

What I have tried so far is this:

add_action('woocommerce_checkout_order_review', 'abc_validating_international_delivery');
add_action('woocommerce_update_order_review', 'abc_validating_international_delivery');
function abc_validating_international_delivery(){

    $has_local_products = false;
    $post_data = $_POST['post_data'];

    parse_str( $post_data, $abc_array );

    if( isset($abc_array['ship_to_different_address']) )
        $shipping_country = $abc_array['shipping_country'];
    else
        $shipping_country = $abc_array['billing_country'];

    if( !empty( $shipping_country ) )
    {
        foreach ( WC()->cart->cart_contents as $cart_content_product ) {

            $shipping_class = $cart_content_product['data']->get_shipping_class();
            if($shipping_class == 'only-india')
            {
                $has_local_products = true;
            }            
        }

        if( $has_local_products && $shipping_country != 'IN' )  
        {
            wc_add_notice( sprintf( '<strong>You have added some products into your cart, which will not be delivered outside India!<br>Please visit <a href="https://www.example.com/my-account/">My Account</a> page and update the shipping/billing location or <a href="https://www.example.com/cart/">review the products</a> you added in cart!</strong>' ), 'error' );
            return false;
        }
        else
            return true;
    }
}

It works first time when accessing the Checkout page and clicking the Place Order button. I mean suppose I have added 3 international delivery available products and 1 only-india product in my cart, and when I choose a foreign country(say, US), and clicks on the Place Order button, it would show the notice(sometimes it's showing more than 2 duplicate notices). But if I don't make any change and press the Place Order button again, it would simply takes me to the payment gateway page. I mean it doesn't seems to be doing the validation neither stopping me from completing the order!

Any suggestions on improving the code? Please feel free to ask if you need any clarification or explanations.

Thank you

1

Since I do not have a great answer for you after checking logic above (seems okay).. something else I noticed.

add_action('woocommerce_update_order_review'  ...

Are you using the most recent update? I see no reference here docs.woothemes.com/wc-apidocs/hook-docs.html or on github depo for that do_action.

Here is the current layout for hooks/filters on form-checkout.php.. Are you sure you are using the correct add_action? (Woocommerce has abysmal documentation if you noticed). You want it also validated for your additional conditions (shipping class/shipping country) after they submit the "checkout-review", correct? (And reject with message at that point if needed). Meaning: what if they update shipping on the checkout-review page (new submitted shipping values)?

An additional idea would to disallow adding to cart if shipping conditions match. (Remove 'add to cart' button and replace with a short message: "India Only" etc) thus avoiding it get put in cart in first place. Or leave the button and pop the msg when they click it. (obviously either of these options only will work if they have already entered shipping info previously).

Hope this helps a little bit..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.