AeliaWCEU_VAT_AssistantProducts_Integration

Implements an integration with WooCommerce Tax Settings to allow additional information to be recorded an maintained.

Defined (1)

The class is defined in the following location(s).

/src/lib/classes/integration/admin/products_integration.php  
  1. class Products_Integration extends \Aelia\WC\Base_Class { 
  2. public static $instance; 
  3.  
  4. public static function init() { 
  5. self::$instance = new self(); 
  6.  
  7. public function __construct() { 
  8. parent::__construct(); 
  9. $this->set_hooks(); 
  10.  
  11. protected function EUVA() { 
  12. return WC_Aelia_EU_VAT_Assistant::instance(); 
  13.  
  14. /** 
  15. * Adds localisation data for the JavaScript that extend the Tax Settings pages. 
  16. * @param array $admin_scripts_params The array of parameters to extend. 
  17. * @return array The array of parameters with additional data. 
  18. */ 
  19. public static function localize_admin_scripts(array $admin_scripts_params) { 
  20. $euva = WC_Aelia_EU_VAT_Assistant::instance(); 
  21. $text_domain = WC_Aelia_EU_VAT_Assistant::$text_domain; 
  22.  
  23. $admin_scripts_params['product_settings'] = array( 
  24. ); 
  25. return $admin_scripts_params; 
  26.  
  27. protected function set_hooks() { 
  28. // Product Edit UI 
  29. add_action('woocommerce_product_write_panel_tabs', array($this, 'woocommerce_product_write_panel_tabs')); 
  30. add_action('woocommerce_product_data_panels', array($this, 'woocommerce_product_data_panels')); 
  31.  
  32. // Variable products 
  33. add_action('woocommerce_product_after_variable_attributes', array($this, 'woocommerce_product_after_variable_attributes'), 10, 3); 
  34.  
  35. // Product srttings 
  36. add_action('save_post', array($this, 'save_post'), 1, 2); 
  37. // WooCommerce 2.4+ 
  38. // @since 1.7.4.170330 
  39. add_action('woocommerce_ajax_save_product_variations', array($this, 'woocommerce_ajax_save_product_variations')); 
  40.  
  41. /** 
  42. * Adds a tab on the product edit page, which will allow to display the "prices 
  43. * by country" section. 
  44. */ 
  45. public function woocommerce_product_write_panel_tabs() { 
  46. ?> 
  47. <li class="euva_tab hide_if_variable show_if_subscription show_if_simple"> 
  48. <a href="#euva_data" class="wcicon-globe"><?php 
  49. echo __('EU VAT', Definitions::TEXT_DOMAIN); 
  50. ?></a> 
  51. </li> 
  52. <?php 
  53.  
  54. /** 
  55. * Renders additional panels in the Product Edit page. 
  56. */ 
  57. public function woocommerce_product_data_panels() { 
  58. global $post; 
  59.  
  60. // Prepare the data for the views 
  61. $product_id = $post->ID; 
  62.  
  63. // Debug 
  64. //var_dump( 
  65. // $product_euva,  
  66. // $product_euva_fields 
  67. //);die(); 
  68.  
  69. // VIES - Define is the product is a service 
  70. $vies_product_is_service = get_post_meta($product_id, Definitions::FIELD_VIES_PRODUCT_IS_SERVICE, true); 
  71.  
  72. include($this->EUVA()->path('views') . '/admin/product_euva_panel.php'); 
  73.  
  74. /** 
  75. * Renders the fields to allow entering prices by country for product variations. 
  76. * @param int variation_index The variation inde (a progressive number within 
  77. * the loop used to render the UI for the variations). 
  78. * @param array variation_data The variation data. 
  79. * @param WP_Post variation The variation object. 
  80. */ 
  81. public function woocommerce_product_after_variable_attributes($variation_index, $variation_data, $variation) { 
  82. // The variation is a WP_Post object, we can take its ID directly 
  83. $product_id = $variation->ID; 
  84. $variation_id = $variation->ID; 
  85.  
  86. // VIES - Define is the product is a service 
  87. $vies_product_is_service = get_post_meta($product_id, Definitions::FIELD_VIES_PRODUCT_IS_SERVICE, true); 
  88.  
  89. // Debug 
  90. //var_dump( 
  91. // $product_prices_by_country 
  92. //);//die(); 
  93.  
  94. include($this->EUVA()->path('views') . '/admin/product_euva_variation.php'); 
  95.  
  96. /** 
  97. * Adds a checkbox to the product admin page to indicate that the product is a 
  98. * service, for VIES purposes. 
  99. * @param array $product_type_options 
  100. * @return array $product_type_options 
  101. */ 
  102. public function easy_booking_add_product_option_pricing($product_type_options) { 
  103. $product_type_options['booking_option'] = array( 
  104. 'id' => '_vies_service',  
  105. 'wrapper_class' => 'show_if_simple show_if_variable',  
  106. 'label' => __('', Definitions::TEXT_DOMAIN),  
  107. 'description' => __( 'Bookable products can be rent or booked on a daily schedule', 'easy_booking' ),  
  108. 'default' => 'no' 
  109. ); 
  110.  
  111. return $product_type_options; 
  112.  
  113. /** 
  114. * Processes the meta for a product being saved, adding the settings for the 
  115. * EU VAT Assistant. 
  116. * @param string product_id The ID of the product being saved. 
  117. * @param mixed post The post being saved. 
  118. * @since 1.3.21.150405 
  119. */ 
  120. public function save_post($product_id, $post = null) { 
  121. if(!isset($_POST['variable_post_id'])) { 
  122. // Simple or external product 
  123.  
  124. // VIES Settings 
  125. // Define if product is a service for VIES purposes 
  126. if(isset($_POST[Definitions::FIELD_VIES_PRODUCT_IS_SERVICE]) && 
  127. isset($_POST[Definitions::FIELD_VIES_PRODUCT_IS_SERVICE][$product_id])) { 
  128. update_post_meta($product_id, Definitions::FIELD_VIES_PRODUCT_IS_SERVICE, $_POST[Definitions::FIELD_VIES_PRODUCT_IS_SERVICE][$product_id]); 
  129. else { 
  130. // Variable product 
  131.  
  132. // Get all variation IDs. They will be used to retrieve the settings for 
  133. // each variation 
  134. $all_variation_ids = $_POST['variable_post_id']; 
  135.  
  136. // VIES Settings 
  137. // Define if product is a service for VIES purposes 
  138. if(!empty($_POST[Definitions::FIELD_VIES_PRODUCT_IS_SERVICE])) { 
  139. $this->save_variations_attribute($all_variation_ids, Definitions::FIELD_VIES_PRODUCT_IS_SERVICE); 
  140.  
  141. /** 
  142. * Handles the saving of variations data using the new logic introduced in 
  143. * WooCommerce 2.4. 
  144. * @param int product_id The ID of the variable product whose variations are 
  145. * being saved. 
  146. * @since 1.7.4.170330 
  147. */ 
  148. public function woocommerce_ajax_save_product_variations($product_id) { 
  149. $this->save_post($product_id); 
  150.  
  151. /** 
  152. * Saves the attributes POSTed with a request against the variations to which 
  153. * they belong. 
  154. * @param array variation_ids An array of variation IDs. 
  155. * @param string field_name The name of the POSTed field from which the values 
  156. * should be retrieved. 
  157. * @since 1.3.21.150405 
  158. */ 
  159. protected function save_variations_attribute(array $variation_ids, $field_name) { 
  160. foreach($variation_ids as $variation_idx => $variation_id) { 
  161. if(!empty($_POST[$field_name][$variation_id])) { 
  162. update_post_meta($variation_id, $field_name, $_POST[$field_name][$variation_id]);