Never store the shipping information
-
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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Never store the shipping information’ is closed to new replies.