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. if ( is_null( self::$_instance ) ) 
  7. self::$_instance = new self(); 
  8. return self::$_instance; 
  9.  
  10. private function __construct() { 
  11.  
  12. if ( is_admin() ) { 
  13. add_action( 'woocommerce_product_options_general_product_data', array( __CLASS__, 'output' ) ); 
  14. add_action( 'woocommerce_product_options_shipping', array( __CLASS__, 'output_shipping' ) ); 
  15. add_action( 'woocommerce_product_options_pricing', array( __CLASS__, 'output_pricing' ) ); 
  16. add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'save' ), 20, 2 ); 
  17. add_filter( 'product_type_options', array( __CLASS__, 'service_type' ), 10, 1 ); 
  18.  
  19. public static function service_type( $types ) { 
  20.  
  21. $types[ 'service' ] = array( 
  22. 'id' => '_service',  
  23. 'wrapper_class' => 'show_if_simple',  
  24. 'label' => __( 'Service', 'woocommerce-germanized' ),  
  25. 'description' => __( 'Service products do not sell physical products.', 'woocommerce-germanized' ),  
  26. 'default' => 'no' 
  27. ); 
  28.  
  29. return $types; 
  30.  
  31. public static function output_pricing() { 
  32.  
  33. global $post, $thepostid; 
  34. $thepostid = $post->ID; 
  35. $_product = wc_get_product( $thepostid ); 
  36.  
  37. 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' ) ) ); 
  38. 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' ) ) ); 
  39.  
  40.  
  41. public static function output() { 
  42.  
  43. global $post, $thepostid; 
  44. $thepostid = $post->ID; 
  45. $_product = wc_get_product( $thepostid ); 
  46.  
  47. // Still output sale price labels if is a variable product 
  48. if ( $_product->is_type( 'variable' ) ) 
  49. self::output_pricing(); 
  50.  
  51. 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' ) ) ); 
  52. 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' ) ) ); 
  53. 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' ) ) ); 
  54.  
  55. if ( $_product->is_virtual() ) { 
  56.  
  57. // Show delivery time selection fallback if is virtual but delivery time should be visible on product 
  58. $types = get_option( 'woocommerce_gzd_display_delivery_time_hidden_types', array() ); 
  59.  
  60. if ( ! in_array( 'virtual', $types ) ) { 
  61.  
  62. // Remove default delivery time selection - otherwise input would exist 2 times 
  63. remove_action( 'woocommerce_product_options_shipping', array( __CLASS__, 'output_shipping' ), 10 ); 
  64. self::output_shipping(); 
  65.  
  66.  
  67. echo '<div class="show_if_simple show_if_external">'; 
  68.  
  69. 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>' ) ); 
  70.  
  71. woocommerce_wp_text_input( array( 'id' => '_unit_price_regular', 'label' => __( 'Regular Base Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price' ) ); 
  72. woocommerce_wp_text_input( array( 'id' => '_unit_price_sale', 'label' => __( 'Sale Base Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')', 'data_type' => 'price' ) ); 
  73.  
  74. echo '</div>'; 
  75.  
  76.  
  77. public static function output_shipping() { 
  78.  
  79. global $post, $thepostid; 
  80.  
  81. $thepostid = $post->ID; 
  82. $_product = wc_get_product( $thepostid ); 
  83.  
  84. $delivery_time = wc_gzd_get_gzd_product( $_product )->delivery_time; 
  85.  
  86. ?>  
  87.  
  88. <p class="form-field"> 
  89. <label for="delivery_time"><?php _e( 'Delivery Time', 'woocommerce-germanized' ); ?></label> 
  90. <input type="hidden" class="wc-product-search wc-gzd-delivery-time-search" style="width: 50%" id="delivery_time" name="delivery_time" data-minimum_input_length="1" data-allow_clear="true" data-placeholder="<?php _e( 'Search for a delivery time…', 'woocommerce-germanized' ); ?>" data-action="woocommerce_gzd_json_search_delivery_time" data-multiple="false" data-selected="<?php echo ( $delivery_time ? $delivery_time->name : '' ); ?>" value="<?php echo ( $delivery_time ? $delivery_time->term_id : '' ); ?>" /> 
  91. </p> 
  92.  
  93. <?php 
  94.  
  95. // Free shipping 
  96. 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' ) ) ); 
  97.  
  98.  
  99. public static function get_fields() { 
  100. return array( 
  101. 'product-type' => '',  
  102. '_unit' => '',  
  103. '_unit_base' => '',  
  104. '_unit_product' => '',  
  105. '_unit_price_auto' => '',  
  106. '_unit_price_regular' => '',  
  107. '_unit_price_sale' => '',  
  108. '_sale_price_label' => '',  
  109. '_sale_price_regular_label' => '',  
  110. '_mini_desc' => '',  
  111. 'delivery_time' => '',  
  112. '_sale_price_dates_from' => '',  
  113. '_sale_price_dates_to' => '',  
  114. '_sale_price' => '',  
  115. '_free_shipping' => '',  
  116. '_service' => '',  
  117. ); 
  118.  
  119. public static function save( $post_id ) { 
  120.  
  121. $product = wc_get_product( $post_id ); 
  122.  
  123. $data = self::get_fields(); 
  124.  
  125. foreach ( $data as $k => $v ) { 
  126. $data[ $k ] = ( isset( $_POST[ $k ] ) ? $_POST[ $k ] : null ); 
  127.  
  128. self::save_product_data( $post_id, $data ); 
  129.  
  130.  
  131. public static function save_product_data( $post_id, $data, $is_variation = false ) { 
  132.  
  133. $data = apply_filters( 'woocommerce_gzd_product_saveable_data', $data, $post_id ); 
  134.  
  135. $product_type = ( ! isset( $data['product-type'] ) || empty( $data['product-type'] ) ) ? 'simple' : sanitize_title( stripslashes( $data['product-type'] ) ); 
  136.  
  137. if ( isset( $data['_unit'] ) ) { 
  138.  
  139. if ( empty( $data['_unit'] ) || in_array( $data['_unit'], array( 'none', '-1' ) ) ) 
  140. delete_post_meta( $post_id, '_unit' ); 
  141. else 
  142. update_post_meta( $post_id, '_unit', sanitize_text_field( $data['_unit'] ) ); 
  143.  
  144.  
  145. $sale_price_labels = array( '_sale_price_label', '_sale_price_regular_label' ); 
  146.  
  147. foreach ( $sale_price_labels as $label ) { 
  148.  
  149. if ( isset( $data[$label] ) ) { 
  150.  
  151. if ( empty( $data[$label] ) || in_array( $data[$label], array( 'none', '-1' ) ) ) 
  152. delete_post_meta( $post_id, $label ); 
  153. else 
  154. update_post_meta( $post_id, $label, sanitize_text_field( $data[$label] ) ); 
  155.  
  156.  
  157.  
  158. if ( isset( $data['_unit_base'] ) ) { 
  159. update_post_meta( $post_id, '_unit_base', ( $data['_unit_base'] === '' ) ? '' : wc_format_decimal( $data['_unit_base'] ) ); 
  160.  
  161. if ( isset( $data['_unit_product'] ) ) { 
  162. update_post_meta( $post_id, '_unit_product', ( $data['_unit_product'] === '' ) ? '' : wc_format_decimal( $data['_unit_product'] ) ); 
  163.  
  164. update_post_meta( $post_id, '_unit_price_auto', ( isset( $data['_unit_price_auto'] ) ) ? 'yes' : '' ); 
  165.  
  166. if ( isset( $data['_unit_price_regular'] ) ) { 
  167. update_post_meta( $post_id, '_unit_price_regular', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  168. update_post_meta( $post_id, '_unit_price', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  169.  
  170. if ( isset( $data['_unit_price_sale'] ) ) { 
  171.  
  172. // Unset unit price sale if no product sale price has been defined 
  173. if ( ! isset( $data['_sale_price'] ) || $data['_sale_price'] === '' ) 
  174. $data['_unit_price_sale'] = ''; 
  175.  
  176. update_post_meta( $post_id, '_unit_price_sale', ( $data['_unit_price_sale'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_sale'] ) ); 
  177.  
  178. if ( isset( $data[ '_mini_desc' ] ) ) { 
  179. update_post_meta( $post_id, '_mini_desc', ( $data[ '_mini_desc' ] === '' ? '' : esc_html( $data[ '_mini_desc' ] ) ) ); 
  180.  
  181. if ( isset( $data[ 'delivery_time' ] ) && ! empty( $data[ 'delivery_time' ] ) ) { 
  182. if ( ! is_numeric( $data[ 'delivery_time' ] ) ) 
  183. wp_set_object_terms( $post_id, sanitize_text_field( $data[ 'delivery_time' ] ), 'product_delivery_time' ); 
  184. else 
  185. wp_set_object_terms( $post_id, absint( $data[ 'delivery_time' ] ) , 'product_delivery_time' ); 
  186. } else { 
  187. wp_delete_object_term_relationships( $post_id, 'product_delivery_time' ); 
  188.  
  189. // Free shipping 
  190. update_post_meta( $post_id, '_free_shipping', ( isset( $data['_free_shipping'] ) ) ? 'yes' : '' ); 
  191.  
  192. // Free shipping 
  193. update_post_meta( $post_id, '_service', ( isset( $data['_service'] ) ) ? 'yes' : '' ); 
  194.  
  195. // Ignore variable data 
  196. if ( in_array( $product_type, array( 'variable', 'grouped' ) ) && ! $is_variation ) { 
  197.  
  198. update_post_meta( $post_id, '_unit_price_regular', '' ); 
  199. update_post_meta( $post_id, '_unit_price_sale', '' ); 
  200. update_post_meta( $post_id, '_unit_price', '' ); 
  201. update_post_meta( $post_id, '_unit_price_auto', '' ); 
  202. update_post_meta( $post_id, '_mini_desc', '' ); 
  203.  
  204. } else { 
  205.  
  206. $date_from = isset( $data['_sale_price_dates_from'] ) ? wc_clean( $data['_sale_price_dates_from'] ) : ''; 
  207. $date_to = isset( $data['_sale_price_dates_to'] ) ? wc_clean( $data['_sale_price_dates_to'] ) : ''; 
  208.  
  209. // Update price if on sale 
  210. if ( isset( $data['_unit_price_sale'] ) ) { 
  211.  
  212. if ( '' !== $data['_unit_price_sale'] && '' == $date_to && '' == $date_from ) { 
  213. update_post_meta( $post_id, '_unit_price', wc_format_decimal( $data['_unit_price_sale'] ) ); 
  214. } else { 
  215. update_post_meta( $post_id, '_unit_price', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  216.  
  217. if ( '' !== $data['_unit_price_sale'] && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) { 
  218. update_post_meta( $post_id, '_unit_price', wc_format_decimal( $data['_unit_price_sale'] ) ); 
  219.  
  220. if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) 
  221. update_post_meta( $post_id, '_unit_price', ( $data['_unit_price_regular'] === '' ) ? '' : wc_format_decimal( $data['_unit_price_regular'] ) ); 
  222.  
  223.  
  224.  
  225.