Advertisement
businessdad

EU VAT Assistant - Set VAT number required for some category

Dec 21st, 2017
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. /**
  2.  * Aelia EU VAT Assistant for WooCommerce - Make VAT number required when one
  3.  * or more product from specific categories are in the cart.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * Add the code to the bottom of your theme's functions.php file (see https://www.skyverge.com/blog/add-custom-code-to-wordpress/).
  7.  * The code will intercept the checkout process automatically and set the VAT number
  8.  * field as "required", depending on the content of the cart.
  9.  *
  10.  * GPL DISCLAIMER
  11.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  12.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  13.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  14.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  15.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  16.  *
  17.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  18.  */
  19.  
  20. /**
  21.  * Returns the categories assigned to a product.
  22.  *
  23.  * @param WC_Product product
  24.  * @return array
  25.  */
  26. function aelia_get_product_categories($product) {
  27.   $result = array();
  28.   // Get all the categories to which a product is assigned
  29.   $categories = wp_get_post_terms($product->get_id(), 'product_cat');
  30.  
  31.   // The $categories array contains a list of objects. Most likely, we would
  32.   // like to have categorys slug as a keys, and their names as values. The
  33.   // wp_list_pluck() function is perfect for this
  34.   $categories = wp_list_pluck($categories, 'name', 'slug');
  35.   return $categories;
  36. }
  37.  
  38. /**
  39.  * Makes the VAT number required when products from specific categories are
  40.  * added to the cart.
  41.  *
  42.  * @param bool vat_number_required
  43.  * @param string customer_country
  44.  * @return bool
  45.  */
  46. add_filter('wc_aelia_euva_order_is_eu_vat_number_required', function($vat_number_required, $customer_country) {
  47.   // If the VAT number is already required, just return the flag as it is
  48.   if($vat_number_required) {
  49.     return $vat_number_required;
  50.   }
  51.  
  52.   // An array of category IDs for which the VAT number is required
  53.   $categories_requiring_vat_number = array(
  54.     'category1',
  55.     'category2',
  56.   );
  57.  
  58.   foreach(WC()->cart->get_cart() as $item) {
  59.     // $item['data'] contains a product instance
  60.     if(!empty($item['data'])) {
  61.       // The keys of the returned array are the category IDs
  62.       $product_categories = array_keys(aelia_get_product_categories($item['data']));
  63.       $matching_categories = array_intersect($categories_requiring_vat_number, $product_categories);
  64.      
  65.       // As soon as we find one product from one of the "VAT number required"
  66.       // categories, we can stop
  67.       if(!empty($matching_categories)) {
  68.         $vat_number_required = true;
  69.       }
  70.     }
  71.   }
  72.  
  73.   return $vat_number_required;
  74. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement