WC_Germanized_Meta_Box_Product_Data

WC_Germanized_Meta_Box_Product_Data.

Defined (1)

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

/includes/admin/meta-boxes/class-wc-gzd-meta-box-product-data.php  
  1. class WC_Germanized_Meta_Box_Product_Data { 
  2.  
  3. protected static $_instance = null; 
  4.  
  5. public static function instance() { 
  6.  
  7. if ( is_null( self::$_instance ) ) 
  8. self::$_instance = new self(); 
  9.  
  10. return self::$_instance; 
  11.  
  12. private function __construct() { 
  13.  
  14. if ( is_admin() ) { 
  15. add_action( 'woocommerce_product_options_general_product_data', array( __CLASS__, 'output' ) ); 
  16. add_action( 'woocommerce_product_options_shipping', array( __CLASS__, 'output_shipping' ) ); 
  17. add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'save' ), 20, 2 ); 
  18. add_filter( 'product_type_options', array( __CLASS__, 'service_type' ), 10, 1 ); 
  19.  
  20. /** 
  21. * Listen to product updates to actually transform term meta data to term relationships e.g. for product delivery time. 
  22. */ 
  23. add_action( 'woocommerce_update_product', array( __CLASS__, 'update_terms' ), 10, 1 ); 
  24. add_action( 'woocommerce_create_product', array( __CLASS__, 'update_terms' ), 10, 1 ); 
  25.  
  26. add_action( 'woocommerce_update_product_variation', array( __CLASS__, 'update_terms' ), 10, 1 ); 
  27. add_action( 'woocommerce_create_product_variation', array( __CLASS__, 'update_terms' ), 10, 1 ); 
  28.  
  29. /** 
  30. * Manipulating WooCommerce CRUD objects through REST API (saving) doesn't work 
  31. * because we need to use filters which do only receive the product id as a parameter and not the actual 
  32. * manipulated instance. That's why we need to temporarily store term data as product meta. 
  33. * After saving the product this hook checks whether term relationships need to be updated or deleted. 
  34. * @param $product_id 
  35. */ 
  36. public static function update_terms( $product_id ) { 
  37.  
  38. if ( ! wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) 
  39. return; 
  40.  
  41. $product = wc_get_product( $product_id ); 
  42.  
  43. if ( $product->get_id() > 0 ) { 
  44. $taxonomies = array( 'product_delivery_time' ); 
  45.  
  46. foreach( $taxonomies as $taxonomy ) { 
  47.  
  48. $term_data = $product->get_meta( '_' . $taxonomy, true ); 
  49.  
  50. if ( $term_data ) { 
  51. $term_data = ( is_numeric( $term_data ) ? absint( $term_data ) : $term_data ); 
  52. wp_set_object_terms( $product->get_id(), $term_data, $taxonomy ); 
  53. delete_post_meta( $product->get_id(), '_' . $taxonomy ); 
  54. } elseif ( $product->get_meta( '_delete_' . $taxonomy, true ) ) { 
  55. wp_delete_object_term_relationships( $product->get_id(), $taxonomy ); 
  56. delete_post_meta( $product->get_id(), '_delete_' . $taxonomy ); 
  57.  
  58. public static function service_type( $types ) { 
  59.  
  60. $types[ 'service' ] = array( 
  61. 'id' => '_service',  
  62. 'wrapper_class' => 'show_if_simple',  
  63. 'label' => __( 'Service', 'woocommerce-germanized' ),  
  64. 'description' => __( 'Service products do not sell physical products.', 'woocommerce-germanized' ),  
  65. 'default' => 'no' 
  66. ); 
  67.  
  68. return $types; 
  69.  
  70. public static function output() { 
  71.  
  72. global $post, $thepostid; 
  73. $thepostid = $post->ID; 
  74. $_product = wc_get_product( $thepostid ); 
  75.  
  76. echo '<div class="options_group show_if_simple show_if_external show_if_variable">'; 
  77.  
  78. woocommerce_wp_select( array( 'id' => '_sale_price_label', 'label' => __( 'Sale Label', 'woocommerce-germanized' ), 'options' => array_merge( array( "-1" => __( 'Select Price Label', 'woocommerce-germanized' ) ), WC_germanized()->price_labels->get_labels() ), 'desc_tip' => true, 'description' => __( 'If the product is on sale you may want to show a price label right before outputting the old price to inform the customer.', 'woocommerce-germanized' ) ) ); 
  79. woocommerce_wp_select( array( 'id' => '_sale_price_regular_label', 'label' => __( 'Sale Regular Label', 'woocommerce-germanized' ), 'options' => array_merge( array( "-1" => __( 'Select Price Label', 'woocommerce-germanized' ) ), WC_germanized()->price_labels->get_labels() ), 'desc_tip' => true, 'description' => __( 'If the product is on sale you may want to show a price label right before outputting the new price to inform the customer.', 'woocommerce-germanized' ) ) ); 
  80.  
  81. woocommerce_wp_select( array( 'id' => '_unit', 'label' => __( 'Unit', 'woocommerce-germanized' ), 'options' => array_merge( array( "-1" => __( 'Select unit', 'woocommerce-germanized' ) ), WC_germanized()->units->get_units() ), 'desc_tip' => true, 'description' => __( 'Needed if selling on a per unit basis', 'woocommerce-germanized' ) ) ); 
  82. woocommerce_wp_text_input( array( 'id' => '_unit_product', 'label' => __( 'Product Units', 'woocommerce-germanized' ), 'data_type' => 'decimal', 'desc_tip' => true, 'description' => __( 'Number of units included per default product price. Example: 1000 ml.', 'woocommerce-germanized' ) ) ); 
  83. woocommerce_wp_text_input( array( 'id' => '_unit_base', 'label' => __( 'Base Price Units', 'woocommerce-germanized' ), 'data_type' => 'decimal', 'desc_tip' => true, 'description' => __( 'Base price units. Example base price: 0, 99 */ 100 ml. Insert 100 as base price unit amount.', 'woocommerce-germanized' ) ) ); 
  84.  
  85. echo '</div>'; 
  86.  
  87. if ( $_product->is_virtual() ) { 
  88.  
  89. // Show delivery time selection fallback if is virtual but delivery time should be visible on product 
  90. $types = get_option( 'woocommerce_gzd_display_delivery_time_hidden_types', array() ); 
  91.  
  92. if ( ! in_array( 'virtual', $types ) ) { 
  93.  
  94. // Remove default delivery time selection - otherwise input would exist 2 times 
  95. remove_action( 'woocommerce_product_options_shipping', array( __CLASS__, 'output_shipping' ), 10 ); 
  96. self::output_shipping(); 
  97.  
  98.  
  99. echo '<div class="options_group show_if_simple show_if_external">'; 
  100.  
  101. woocommerce_wp_checkbox( array( 'id' => '_unit_price_auto', 'label' => __( 'Calculation', 'woocommerce-germanized' ), 'description' => '<span class="wc-gzd-premium-desc">' . __( 'Calculate base prices automatically.', 'woocommerce-germanized' ) . '</span> <a href="https://vendidero.de/woocommerce-germanized#buy" target="_blank" class="wc-gzd-pro">pro</a>' ) ); 
  102.  
  103. woocommerce_wp_text_input( array( 'id' => '_unit_price_regular', 'label' => __( 'Regular Base Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price' ) ); 
  104. woocommerce_wp_text_input( array( 'id' => '_unit_price_sale', 'label' => __( 'Sale Base Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price' ) ); 
  105.  
  106. echo '</div>'; 
  107.  
  108.  
  109. public static function output_delivery_time_select2( $args = array() ) { 
  110.  
  111. $args = wp_parse_args( $args, array( 
  112. 'name' => 'delivery_time',  
  113. 'placeholder' => __( 'Search for a delivery time…', 'woocommerce-germanized' ),  
  114. 'term' => false,  
  115. 'id' => '',  
  116. 'style' => 'width: 50%',  
  117. ) ); 
  118.  
  119. $args[ 'id' ] = empty( $args[ 'id' ] ) ? $args[ 'name' ] : $args[ 'id' ]; 
  120.  
  121. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() ) { 
  122. ?> 
  123. <select class="wc-product-search wc-gzd-delivery-time-search" style="<?php echo $args[ 'style' ]; ?>" id="<?php echo $args[ 'id' ]; ?>" name="<?php echo $args[ 'name' ]; ?>" data-minimum_input_length="1" data-allow_clear="true" data-placeholder="<?php echo $args[ 'placeholder' ]; ?>" data-action="woocommerce_gzd_json_search_delivery_time" data-multiple="false"> 
  124. <?php 
  125.  
  126. if ( $args[ 'term' ] ) { 
  127. echo '<option value="' . esc_attr( $args[ 'term' ]->term_id ) . '"' . selected( true, true, false ) . '>' . $args[ 'term' ]->name . '</option>'; 
  128.  
  129. ?> 
  130. </select> 
  131. <?php 
  132. } else { 
  133. ?> 
  134. <input type="hidden" class="wc-product-search wc-gzd-delivery-time-search" style="<?php echo $args[ 'style' ]; ?>" id="<?php echo $args[ 'id' ]; ?>" name="<?php echo $args[ 'name' ]; ?>" data-minimum_input_length="1" data-allow_clear="true" data-placeholder="<?php echo $args[ 'placeholder' ]; ?>" data-action="woocommerce_gzd_json_search_delivery_time" data-multiple="false" data-selected="<?php echo ( $args[ 'term' ] ? $args[ 'term' ]->name : '' ); ?>" value="<?php echo ( $args[ 'term' ] ? $args[ 'term' ]->term_id : '' ); ?>" /> 
  135. <?php 
  136.  
  137. public static function output_shipping() { 
  138.  
  139. global $post, $thepostid; 
  140.  
  141. $thepostid = $post->ID; 
  142. $_product = wc_get_product( $thepostid ); 
  143.  
  144. $delivery_time = wc_gzd_get_gzd_product( $_product )->delivery_time; 
  145.  
  146. ?>  
  147.  
  148. <p class="form-field"> 
  149. <label for="delivery_time"><?php _e( 'Delivery Time', 'woocommerce-germanized' ); ?></label> 
  150. <?php 
  151. self::output_delivery_time_select2( array( 
  152. 'name' => 'delivery_time',  
  153. 'placeholder' => __( 'Search for a delivery time…', 'woocommerce-germanized' ),  
  154. 'term' => $delivery_time,  
  155. ) ); 
  156. ?> 
  157. </p> 
  158.  
  159. <?php 
  160.  
  161. // Free shipping 
  162. woocommerce_wp_checkbox( array( 'id' => '_free_shipping', 'label' => __( 'Free shipping?', 'woocommerce-germanized' ), 'description' => __( 'This option disables the "plus shipping costs" notice on product page', 'woocommerce-germanized' ) ) ); 
  163.  
  164.  
  165. public static function get_fields() { 
  166. return array( 
  167. 'product-type' => '',  
  168. '_unit' => '',  
  169. '_unit_base' => '',  
  170. '_unit_product' => '',  
  171. '_unit_price_auto' => '',  
  172. '_unit_price_regular' => '',  
  173. '_unit_price_sale' => '',  
  174. '_sale_price_label' => '',  
  175. '_sale_price_regular_label' => '',  
  176. '_mini_desc' => '',  
  177. 'delivery_time' => '',  
  178. '_sale_price_dates_from' => '',  
  179. '_sale_price_dates_to' => '',  
  180. '_sale_price' => '',  
  181. '_free_shipping' => '',  
  182. '_service' => '',  
  183. ); 
  184.  
  185. public static function save( $post_id ) { 
  186.  
  187. $product = wc_get_product( $post_id ); 
  188.  
  189. $data = self::get_fields(); 
  190.  
  191. foreach ( $data as $k => $v ) { 
  192. $data[ $k ] = ( isset( $_POST[ $k ] ) ? $_POST[ $k ] : null ); 
  193.  
  194. $product = self::save_product_data( $product, $data ); 
  195.  
  196. public static function save_unit_price( $product, $data, $is_variation = false ) { 
  197.  
  198. $data = wp_parse_args( $data, array( 
  199. 'save' => true,  
  200. ) ); 
  201.  
  202. $product_type = ( ! isset( $data['product-type'] ) || empty( $data['product-type'] ) ) ? 'simple' : sanitize_title( stripslashes( $data['product-type'] ) ); 
  203.  
  204. if ( isset( $data['_unit'] ) ) { 
  205.  
  206. if ( empty( $data['_unit'] ) || in_array( $data['_unit'], array( 'none', '-1' ) ) ) 
  207. $product = wc_gzd_unset_crud_meta_data( $product, '_unit' ); 
  208. else 
  209. $product = wc_gzd_set_crud_meta_data( $product, '_unit', sanitize_text_field( $data['_unit'] ) ); 
  210.  
  211.  
  212. if ( isset( $data['_unit_base'] ) ) { 
  213. $product = wc_gzd_set_crud_meta_data( $product, '_unit_base', ( $data['_unit_base'] === '' ) ? '' : wc_format_decimal( $data['_unit_base'] ) ); 
  214.  
  215. if ( isset( $data['_unit_product'] ) ) { 
  216. $product = wc_gzd_set_crud_meta_data( $product, '_unit_product', ( $data['_unit_product'] === '' ) ? '' : wc_format_decimal( $data['_unit_product'] ) ); 
  217.  
  218. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price_auto', ( isset( $data['_unit_price_auto'] ) ) ? 'yes' : '' ); 
  219.  
  220. if ( isset( $data['_unit_price_regular'] ) ) { 
  221. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price_regular', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  222. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  223.  
  224. if ( isset( $data['_unit_price_sale'] ) ) { 
  225.  
  226. // Unset unit price sale if no product sale price has been defined 
  227. if ( ! isset( $data['_sale_price'] ) || $data['_sale_price'] === '' ) 
  228. $data['_unit_price_sale'] = ''; 
  229.  
  230. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price_sale', ( $data['_unit_price_sale'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_sale'] ) ); 
  231.  
  232. // Ignore variable data 
  233. if ( in_array( $product_type, array( 'variable', 'grouped' ) ) && ! $is_variation ) { 
  234.  
  235. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price_regular', '' ); 
  236. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price_sale', '' ); 
  237. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price', '' ); 
  238. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price_auto', '' ); 
  239.  
  240. } else { 
  241.  
  242. $date_from = isset( $data['_sale_price_dates_from'] ) ? wc_clean( $data['_sale_price_dates_from'] ) : ''; 
  243. $date_to = isset( $data['_sale_price_dates_to'] ) ? wc_clean( $data['_sale_price_dates_to'] ) : ''; 
  244.  
  245. // Update price if on sale 
  246. if ( isset( $data['_unit_price_sale'] ) ) { 
  247.  
  248. if ( '' !== $data['_unit_price_sale'] && '' == $date_to && '' == $date_from ) { 
  249. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price', wc_format_decimal( $data['_unit_price_sale'] ) ); 
  250. } else { 
  251. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  252.  
  253. if ( '' !== $data['_unit_price_sale'] && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) { 
  254. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price', wc_format_decimal( $data['_unit_price_sale'] ) ); 
  255.  
  256. if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) 
  257. $product = wc_gzd_set_crud_meta_data( $product, '_unit_price', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  258.  
  259. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() && $data[ 'save' ] ) { 
  260. $product->save(); 
  261.  
  262. return $product; 
  263.  
  264. public static function save_product_data( $product, $data, $is_variation = false ) { 
  265.  
  266. if ( is_numeric( $product ) ) 
  267. $product = wc_get_product( $product ); 
  268.  
  269. $data = apply_filters( 'woocommerce_gzd_product_saveable_data', $data, $product ); 
  270. $data = wp_parse_args( $data, array( 
  271. 'save' => true,  
  272. ) ); 
  273.  
  274. $unit_data = $data; 
  275. $unit_data[ 'save' ] = false; 
  276. $product = self::save_unit_price( $product, $unit_data, $is_variation ); 
  277.  
  278. $product_type = ( ! isset( $data['product-type'] ) || empty( $data['product-type'] ) ) ? 'simple' : sanitize_title( stripslashes( $data['product-type'] ) ); 
  279.  
  280. $sale_price_labels = array( '_sale_price_label', '_sale_price_regular_label' ); 
  281.  
  282. foreach ( $sale_price_labels as $label ) { 
  283.  
  284. if ( isset( $data[$label] ) ) { 
  285.  
  286. if ( empty( $data[$label] ) || in_array( $data[$label], array( 'none', '-1' ) ) ) 
  287. $product = wc_gzd_unset_crud_meta_data( $product, $label ); 
  288. else 
  289. $product = wc_gzd_set_crud_meta_data( $product, $label, sanitize_text_field( $data[$label] ) ); 
  290.  
  291. if ( isset( $data[ '_mini_desc' ] ) ) { 
  292. $product = wc_gzd_set_crud_meta_data( $product, '_mini_desc', ( $data[ '_mini_desc' ] === '' ? '' : esc_html( $data[ '_mini_desc' ] ) ) ); 
  293.  
  294. if ( isset( $data[ 'delivery_time' ] ) && ! empty( $data[ 'delivery_time' ] ) ) { 
  295. $product = wc_gzd_set_crud_term_data( $product, $data[ 'delivery_time' ], 'product_delivery_time' ); 
  296. } else { 
  297. $product = wc_gzd_unset_crud_term_data( $product, 'product_delivery_time' ); 
  298.  
  299. // Free shipping 
  300. $product = wc_gzd_set_crud_meta_data( $product, '_free_shipping', ( isset( $data['_free_shipping'] ) ) ? 'yes' : '' ); 
  301.  
  302. // Free shipping 
  303. $product = wc_gzd_set_crud_meta_data( $product, '_service', ( isset( $data['_service'] ) ) ? 'yes' : '' ); 
  304.  
  305. // Ignore variable data 
  306. if ( in_array( $product_type, array( 'variable', 'grouped' ) ) && ! $is_variation ) { 
  307. $product = wc_gzd_set_crud_meta_data( $product, '_mini_desc', '' ); 
  308.  
  309. if ( wc_gzd_get_dependencies()->woocommerce_version_supports_crud() && $data[ 'save' ] ) { 
  310. $product->save(); 
  311.  
  312. return $product; 
  313.