Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. add_action( 'woocommerce_check_cart_items', 'check_cart_items_for_shipping' );
  2. function check_cart_items_for_shipping() {
  3. $allowed_variation_id = '513'; // Here defined the allowed Variation ID
  4. $allowed_country = 'US'; // Here define the allowed shipping country for all variations
  5.  
  6. $shipping_country = WC()->customer->get_shipping_country();
  7. $countries = WC()->countries->get_countries();
  8.  
  9. // Loop through cart items
  10. foreach(WC()->cart->get_cart() as $cart_item ) {
  11. // Check cart item for defined product Ids and applied coupon
  12. if( $shipping_country !== $allowed_country && $cart_item['variation_id'] !== $allowed_variation_id ) {
  13. wc_clear_notices(); // Clear all other notices
  14.  
  15. // Avoid checkout displaying an error notice
  16. wc_add_notice( sprintf( __('The product "%s" can not be shipped to %s.'),
  17. $cart_item['data']->get_name(),
  18. $countries[$shipping_country]
  19. ), 'error' );
  20. break; // stop the loop
  21. }
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement