WCPBC_Admin_Product_Data

WCPBC_Admin_Product_Data.

Defined (1)

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

/includes/admin/class-wcpbc-admin-product-data.php  
  1. class WCPBC_Admin_Product_Data { 
  2.  
  3. /** 
  4. * Hook in methods 
  5. */ 
  6. public static function init() { 
  7.  
  8. add_action( 'woocommerce_product_options_general_product_data', array( __CLASS__, 'product_options_countries_prices' ) ); 
  9.  
  10. add_action( 'woocommerce_process_product_meta_simple', array( __CLASS__, 'process_product_simple_countries_prices' ) ) ;  
  11.  
  12. add_action( 'woocommerce_process_product_meta_external', array( __CLASS__, 'process_product_simple_countries_prices' ) ) ;  
  13.  
  14. add_action( 'woocommerce_product_after_variable_attributes', array( __CLASS__, 'product_variable_attributes_countries_prices') , 10, 3 );  
  15.  
  16. add_action( 'woocommerce_save_product_variation', array( __CLASS__, 'save_product_variation_countries_prices' ), 10, 2 ); 
  17.  
  18. add_action( 'woocommerce_variable_product_sync', array( __CLASS__, 'variable_product_sync' ), 10, 2 );  
  19.  
  20.  
  21. /** 
  22. * Add price input to product simple metabox 
  23. */ 
  24. public static function product_options_countries_prices() {  
  25.  
  26. foreach ( WCPBC()->get_regions() as $key => $value ) {  
  27.  
  28. $_id_prefix = '_' . $key; 
  29.  
  30. $_regular_price = get_post_meta( get_the_ID(), $_id_prefix . '_regular_price' , true ); 
  31. $_sale_price = get_post_meta( get_the_ID(), $_id_prefix . '_sale_price' , true ); 
  32.  
  33. $_price_method = empty($_regular_price) ? 'exchange_rate' : 'manual';  
  34. $_display = ( $_price_method == 'exchange_rate' ? 'none' : 'block' ); 
  35.  
  36. ?> 
  37. <div class="options_group pricing show_if_simple show_if_external">  
  38.  
  39. <?php 
  40. woocommerce_wp_radio( 
  41. array( 
  42. 'id' => $_id_prefix . '_price_method',  
  43. 'value' => $_price_method,  
  44. 'class' => 'wcpbc_price_method',  
  45. 'label' => __( 'Price for', 'wc-price-based-country' ) . ' ' . $value['name']. ' (' . get_woocommerce_currency_symbol( $value['currency'] ) . ')',  
  46. 'options' => array( 
  47. 'exchange_rate' => __('Calculate price by applying exchange rate.', 'wc-price-based-country'),  
  48. 'manual' => __('Set price manually.', 'wc-price-based-country') 
  49. ); 
  50. ?>  
  51.  
  52. <div style="display:<?php echo $_display; ?>"> 
  53.  
  54. <?php do_action('wc_price_based_country_before_product_options_pricing', $_id_prefix, $value['currency'] ); ?> 
  55.  
  56. <p class="form-field _regular_price_field"> 
  57. <label><?php echo __( 'Regular Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol($value['currency']) . ')'; ?></label> 
  58. <input type="text" id="<?php echo $_id_prefix . '_regular_price'; ?>" name="<?php echo $_id_prefix . '_regular_price'; ?>" value="<?php echo wc_format_localized_price( $_regular_price ); ?>" class="short wc_input_price" placeholder="" /> 
  59. </p> 
  60.  
  61. <p class="form-field _sale_price_field">  
  62. <label><?php echo __( 'Sale Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol($value['currency']) . ')'; ?></label> 
  63. <input type="text" id="<?php echo $_id_prefix . '_sale_price'; ?>" name="<?php echo $_id_prefix . '_sale_price'; ?>" value="<?php echo wc_format_localized_price( $_sale_price ); ?>" class="short wc_input_price wcpbc_sale_price" /> 
  64. </p> 
  65.  
  66. </div> 
  67.  
  68. </div> 
  69.  
  70. <?php  
  71. }  
  72.  
  73.  
  74. /** 
  75. * Save meta data product simple 
  76. */ 
  77. public static function process_product_simple_countries_prices( $post_id, $i = false, $variable = '' ) {  
  78.  
  79. foreach ( WCPBC()->get_regions() as $key => $value ) { 
  80.  
  81. $key_prefix = '_' . $key . $variable; 
  82. $key_regular_price = $key_prefix . '_regular_price'; 
  83. $key_sale_price = $key_prefix . '_sale_price';  
  84. $key_price = $key_prefix . '_price';  
  85. $key_price_method = $key_prefix . '_price_method'; 
  86.  
  87.  
  88. if ( $i === false ) { 
  89.  
  90. $_regular_price = $_POST[$key_regular_price]; 
  91. $_sale_price = $_POST[$key_sale_price]; 
  92. $_price_method = $_POST[$key_price_method];  
  93.  
  94. } else { 
  95.  
  96. $_regular_price = $_POST[$key_regular_price][$i]; 
  97. $_sale_price = $_POST[$key_sale_price][$i]; 
  98. $_price_method = $_POST[$key_price_method][$i];  
  99.  
  100. $_price_method = ( $_price_method == 'exchange_rate' || empty( $_regular_price ) ) ? 'exchange_rate' : 'manual'; 
  101.  
  102. if ( $_price_method == 'exchange_rate') { 
  103.  
  104. $_regular_price = ''; 
  105. $_sale_price = ''; 
  106. $_price = ''; 
  107.  
  108. } else { 
  109.  
  110. $_regular_price = wc_format_decimal( $_regular_price ); 
  111. $_sale_price = wc_format_decimal( $_sale_price ); 
  112. // Update price if on sale 
  113. $_price = ( '' !== $_sale_price ) ? $_sale_price : $_regular_price;  
  114. }  
  115.  
  116. update_post_meta( $post_id, $key_regular_price, $_regular_price );  
  117. update_post_meta( $post_id, $key_sale_price, $_sale_price );  
  118. update_post_meta( $post_id, $key_price, $_price );  
  119. update_post_meta( $post_id, $key_price_method, $_price_method ); 
  120.  
  121. do_action('wc_price_based_country_process_product_pricing', $key_prefix, $post_id, $_price_method, $i );  
  122.  
  123. }  
  124.  
  125.  
  126. /**  
  127. * Add price input to product variation metabox 
  128. */ 
  129. public static function product_variable_attributes_countries_prices( $loop, $variation_data, $variation ) {  
  130.  
  131. foreach ( WCPBC()->get_regions() as $key => $value) { 
  132.  
  133. $_regular_price = wc_format_localized_price( get_post_meta( $variation->ID, '_' . $key . '_variable_regular_price', true) ); 
  134. $_sale_price = wc_format_localized_price( get_post_meta( $variation->ID, '_' . $key . '_variable_sale_price', true) ); 
  135.  
  136. $_empty_method = empty($_regular_price) ? 'exchange_rate' : 'manual'; 
  137. $_display = $_empty_method == 'exchange_rate' ? 'none' : 'block';  
  138.  
  139. ?> 
  140. <div style="width:100%; overflow:auto; padding-right:10px;border-top:1px solid #eee;"> 
  141.  
  142. <p class="form-row form-row-first"><strong><?php echo __( 'Price for', 'wc-price-based-country' ) . ' ' . $value['name'] . ' (' . get_woocommerce_currency_symbol( $value['currency'] ) . ')'; ?></strong></p> 
  143.  
  144. <div class="form-row form-row-last <?php echo '_' . $key . '_variable_price_method_' . $loop . '_field'; ?>"> 
  145. <ul> 
  146. <li style="padding:0;"> 
  147. <label> 
  148. <input name="<?php echo '_' . $key . '_variable_price_method[' . $loop . ']'; ?>" value="exchange_rate" class="wcpbc_price_method" <?php echo ($_empty_method == 'exchange_rate' ? 'checked="checked"':'');?> type="radio"> 
  149. <?php _e('Calculate price by applying exchange rate.', 'wc-price-based-country') ?> 
  150. </label> 
  151. </li> 
  152. <li style="padding:0;"> 
  153. <label> 
  154. <input name="<?php echo '_' . $key . '_variable_price_method[' . $loop . ']'; ?>" value="manual" class="wcpbc_price_method" <?php echo ($_empty_method != 'exchange_rate' ? 'checked="checked"':'');?>type="radio"> 
  155. <?php _e('Set price manually.', 'wc-price-based-country') ?> 
  156. </label> 
  157. </li> 
  158. </ul> 
  159. </div> 
  160.  
  161. <div style="display:<?php echo $_display;?>"> 
  162.  
  163. <?php do_action('wc_price_based_country_before_product_variable_options_pricing', $key, $value['currency'], $loop, $variation ); ?> 
  164.  
  165. <div class="wpbc_variable_pricing"> 
  166.  
  167. <p class="form-row form-row-first"> 
  168. <label><?php echo __( 'Regular Price:', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol( $value['currency'] ) . ')'; ?></label> 
  169. <input type="text" size="5" id="<?php echo '_' . $key . '_variable_regular_price_' . $loop; ?>" name="<?php echo '_' . $key . '_variable_regular_price[' . $loop. ']'; ?>" value="<?php if ( isset( $_regular_price ) ) echo esc_attr( $_regular_price ); ?>" class="wc_input_price" /> 
  170. </p> 
  171. <p class="form-row form-row-last"> 
  172. <label><?php echo __( 'Sale Price:', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol( $value['currency'] ) . ')'; ?></label> 
  173. <input type="text" size="5" id="<?php echo '_' . $key . '_variable_sale_price_' . $loop; ?>" name="<?php echo '_' . $key . '_variable_sale_price[' . $loop. ']'; ?>" value="<?php if ( isset( $_sale_price ) ) echo esc_attr( $_sale_price ); ?>" class="wc_input_price wcpbc_sale_price" /> 
  174. </p> 
  175.  
  176. <div class="sale_price_dates_fields"></div> 
  177. </div> 
  178.  
  179. </div> 
  180.  
  181. </div> 
  182.  
  183. <?php  
  184. }  
  185.  
  186. /** 
  187. * Save meta data product variation 
  188. */ 
  189. public static function save_product_variation_countries_prices( $variation_id, $i ) {  
  190.  
  191. self::process_product_simple_countries_prices( $variation_id, $i, '_variable');  
  192.  
  193. /** 
  194. * Sync product variation prices with parent 
  195. */ 
  196. public static function variable_product_sync( $product_id, $children ) {  
  197.  
  198. foreach ( WCPBC()->get_regions() as $region_key => $region ) { 
  199.  
  200. // Main active prices 
  201. $min_price = null; 
  202. $max_price = null; 
  203. $min_price_id = null; 
  204. $max_price_id = null; 
  205.  
  206. // Regular prices 
  207. $min_regular_price = null; 
  208. $max_regular_price = null; 
  209. $min_regular_price_id = null; 
  210. $max_regular_price_id = null; 
  211.  
  212. // Sale prices 
  213. $min_sale_price = null; 
  214. $max_sale_price = null; 
  215. $min_sale_price_id = null; 
  216. $max_sale_price_id = null; 
  217.  
  218. foreach ( array( 'price', 'regular_price', 'sale_price' ) as $price_type ) {  
  219.  
  220. foreach ( $children as $child_id ) {  
  221.  
  222. $child_price_method = get_post_meta( $child_id, '_' . $region_key . '_variable_price_method', true ); 
  223.  
  224. if ( $child_price_method !== 'manual' ) { 
  225.  
  226. $child_price = get_post_meta( $child_id, '_' . $price_type, true );  
  227. $child_price = ( !empty( $child_price ) && $region['exchange_rate'] ) ? ( $region['exchange_rate'] * $child_price ) : $child_price;  
  228.  
  229. } else{ 
  230.  
  231. $child_price = get_post_meta( $child_id, '_' . $region_key . '_variable_' . $price_type, true );  
  232. }  
  233.  
  234. // Skip non-priced variations 
  235. if ( $child_price === '' ) { 
  236. continue; 
  237.  
  238. // Skip hidden variations 
  239. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 
  240. $stock = get_post_meta( $child_id, '_stock', true ); 
  241. if ( $stock !== "" && $stock <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { 
  242. continue; 
  243.  
  244. // Find min price 
  245. if ( is_null( ${"min_{$price_type}"} ) || $child_price < ${"min_{$price_type}"} ) { 
  246. ${"min_{$price_type}"} = $child_price; 
  247. ${"min_{$price_type}_id"} = $child_id; 
  248.  
  249. // Find max price 
  250. if ( is_null( ${"max_{$price_type}"} ) || $child_price > ${"max_{$price_type}"} ) { 
  251. ${"max_{$price_type}"} = $child_price; 
  252. ${"max_{$price_type}_id"} = $child_id; 
  253.  
  254. // Store ids 
  255. update_post_meta( $product_id, '_' . $region_key . '_min_' . $price_type . '_variation_id', ${"min_{$price_type}_id"} ); 
  256. update_post_meta( $product_id, '_' . $region_key . '_max_' . $price_type . '_variation_id', ${"max_{$price_type}_id"} ); 
  257. }  
  258. }