WC_Germanized_Meta_Box_Product_Data_Variable

Adds unit price and delivery time to variable Product metabox.

Defined (1)

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

/includes/admin/meta-boxes/class-wc-gzd-meta-box-product-data-variable.php  
  1. class WC_Germanized_Meta_Box_Product_Data_Variable { 
  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_after_variable_attributes', array( __CLASS__, 'output' ), 20, 3 ); 
  14. add_action( 'woocommerce_save_product_variation', array( __CLASS__, 'save' ) , 0, 2 ); 
  15. add_action( 'woocommerce_variation_options', array( __CLASS__, 'service' ), 0, 3 ); 
  16.  
  17. public static function service( $loop, $variation_data, $variation ) { 
  18.  
  19. $_product = wc_get_product( $variation ); 
  20. $variation_id = $_product->variation_id; 
  21. $is_service = get_post_meta( $variation_id, '_service', true ); 
  22.  
  23. ?> 
  24. <label><input type="checkbox" class="checkbox variable_service" name="variable_service[<?php echo $loop; ?>]" <?php checked( $is_service !== '' ? $is_service : '', 'yes' ); ?> /> <?php _e( 'Service', 'woocommerce-germanized' ); ?> <?php echo wc_gzd_help_tip( __( 'Service products do not sell physical products.', 'woocommerce-germanized' ) ); ?></label> 
  25. <?php 
  26.  
  27. public static function output( $loop, $variation_data, $variation ) { 
  28.  
  29. $_product = wc_get_product( $variation ); 
  30. $_parent = wc_get_product( $_product->parent ); 
  31. $variation_id = $_product->variation_id; 
  32.  
  33. $variation_meta = get_post_meta( $variation_id ); 
  34. $variation_data = array(); 
  35.  
  36. $variation_fields = array( 
  37. '_unit' => '',  
  38. '_unit_base' => '',  
  39. '_unit_product' => '',  
  40. '_unit_price_auto' => '',  
  41. '_unit_price_regular' => '',  
  42. '_unit_price_auto' => '',  
  43. '_unit_price_sale' => '',  
  44. '_sale_price_label' => '',  
  45. '_sale_price_regular_label' => '',  
  46. '_mini_desc' => '' 
  47. ); 
  48.  
  49. foreach ( $variation_fields as $field => $value ) { 
  50. $variation_data[ $field ] = isset( $variation_meta[ $field ][0] ) ? maybe_unserialize( $variation_meta[ $field ][0] ) : $value; 
  51.  
  52. $delivery_time = get_the_terms( $variation_id, 'product_delivery_time' ); 
  53.  
  54. if ( $delivery_time && ! empty( $delivery_time ) && is_array( $delivery_time ) ) 
  55. $delivery_time = $delivery_time[0]; 
  56.  
  57. ?> 
  58.  
  59. <div class="variable_pricing_labels"> 
  60.  
  61. <p class="form-row form-row-first"> 
  62. <label><?php _e( 'Sale Label', 'woocommerce-germanized' ); ?></label> 
  63. <select name="variable_sale_price_label[<?php echo $loop; ?>]"> 
  64. <option value="" <?php selected( empty( $variation_data[ '_sale_price_label' ] ) , true ); ?>><?php _e( 'Same as Parent', 'woocommerce-germanized' ); ?></option> 
  65. <?php foreach ( WC_germanized()->price_labels->get_labels() as $key => $value ) : ?> 
  66. <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key === $variation_data[ '_sale_price_label' ], true ); ?>><?php echo esc_html( $value ); ?></option> 
  67. <?php endforeach; ?> 
  68. </select> 
  69. </p> 
  70.  
  71. <p class="form-row form-row-last"> 
  72. <label><?php _e( 'Sale Regular Label', 'woocommerce-germanized' ); ?></label> 
  73. <select name="variable_sale_price_regular_label[<?php echo $loop; ?>]"> 
  74. <option value="" <?php selected( empty( $variation_data[ '_sale_price_regular_label' ] ), true ); ?>><?php _e( 'Same as Parent', 'woocommerce-germanized' ); ?></option> 
  75. <?php foreach ( WC_germanized()->price_labels->get_labels() as $key => $value ) : ?> 
  76. <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $key === $variation_data[ '_sale_price_regular_label' ], true ); ?>><?php echo esc_html( $value ); ?></option> 
  77. <?php endforeach; ?> 
  78. </select> 
  79. </p> 
  80.  
  81. </div> 
  82.  
  83. <div class="variable_pricing_unit"> 
  84. <p class="form-row form-row-first"> 
  85.  
  86. <input type="hidden" name="variable_parent_unit_product[<?php echo $loop; ?>]" class="wc-gzd-parent-unit_product" value="" /> 
  87. <input type="hidden" name="variable_parent_unit[<?php echo $loop; ?>]" class="wc-gzd-parent-unit" value="" /> 
  88. <input type="hidden" name="variable_parent_unit_base[<?php echo $loop; ?>]" class="wc-gzd-parent-unit_base" value="" /> 
  89.  
  90. <label for="variable_unit_product"><?php echo __( 'Product Units', 'woocommerce-germanized' );?> <?php echo wc_gzd_help_tip( __( 'Number of units included per default product price. Example: 1000 ml. Leave blank to use parent value.', 'woocommerce-germanized' ) ); ?></label> 
  91. <input class="input-text wc_input_decimal" size="6" type="text" name="variable_unit_product[<?php echo $loop; ?>]" value="<?php echo ( ! empty( $variation_data[ '_unit_product' ] ) ? esc_attr( wc_format_localized_decimal( $variation_data[ '_unit_product' ] ) ) : '' );?>" placeholder="<?php echo esc_attr( wc_format_localized_decimal( wc_gzd_get_gzd_product( $_parent )->unit_product ) ); ?>" /> 
  92. </p> 
  93. <p class="form-row form-row-last _unit_price_auto_field"> 
  94. <label for="variable_unit_price_auto_<?php echo $loop; ?>"><?php echo __( 'Calculation', 'woocommerce-germanized' ); ?></label> 
  95. <input class="input-text wc_input_price" id="variable_unit_price_auto_<?php echo $loop; ?>" type="checkbox" name="variable_unit_price_auto[<?php echo $loop; ?>]" value="yes" <?php checked( 'yes', $variation_data[ '_unit_price_auto' ] );?> /> 
  96. <span class="description"> 
  97. <span class="wc-gzd-premium-desc"><?php echo __( 'Calculate unit prices automatically', 'woocommerce-germanized' ); ?></span> 
  98. <a href="https://vendidero.de/woocommerce-germanized#buy" target="_blank" class="wc-gzd-pro">pro</a> 
  99. </span> 
  100. </p> 
  101. <p class="form-row form-row-first"> 
  102. <label for="variable_unit_price_regular"><?php echo __( 'Regular Unit Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')'; ?></label> 
  103. <input class="input-text wc_input_price" size="5" type="text" name="variable_unit_price_regular[<?php echo $loop; ?>]" value="<?php echo ( ! empty( $variation_data[ '_unit_price_regular' ] ) ? esc_attr( wc_format_localized_price( $variation_data[ '_unit_price_regular' ] ) ) : '' );?>" placeholder="" /> 
  104. </p> 
  105. <p class="form-row form-row-last"> 
  106. <label for="variable_unit_price_sale"><?php echo __( 'Sale Unit Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')'; ?></label> 
  107. <input class="input-text wc_input_price" size="5" type="text" name="variable_unit_price_sale[<?php echo $loop; ?>]" value="<?php echo ( ! empty( $variation_data[ '_unit_price_sale' ] ) ? esc_attr( wc_format_localized_price( $variation_data[ '_unit_price_sale' ] ) ) : '' );?>" placeholder="" /> 
  108. </p> 
  109. </div> 
  110. <div class="variable_shipping_time hide_if_variation_virtual"> 
  111. <p class="form-row form-row-first"> 
  112. <label for="delivery_time"><?php _e( 'Delivery Time', 'woocommerce-germanized' ); ?></label> 
  113.  
  114. <?php 
  115. WC_Germanized_Meta_Box_Product_Data::output_delivery_time_select2( array( 
  116. 'name' => 'variable_delivery_time[' . $loop . ']',  
  117. 'id' => 'variable_delivery_time_' . $loop,  
  118. 'placeholder' => __( 'Same as parent', 'woocommerce-germanized' ),  
  119. 'term' => $delivery_time,  
  120. 'style' => 'width: 100%',  
  121. ) ); 
  122. ?> 
  123. </p> 
  124. </div> 
  125. <div class="variable_cart_mini_desc"> 
  126. <p class="form-row form-row-full"> 
  127. <label for="variable_mini_desc"><?php echo __( 'Optional Mini Description', 'woocommerce-germanized' ); ?></label> 
  128. <textarea rows="3" style="width: 100%" name="variable_mini_desc[<?php echo $loop;?>]" id="variable_mini_desc_<?php echo $loop;?>" class="variable_mini_desc"><?php echo htmlspecialchars_decode( $variation_data[ '_mini_desc' ] ); ?></textarea> 
  129. <?php // wp_editor( htmlspecialchars_decode( wc_gzd_get_gzd_product( $_product )->mini_desc ), 'wc_gzd_product_mini_desc_' . $loop, array( 'textarea_name' => 'variable_mini_desc[' . $loop . ']', 'textarea_rows' => 5, 'media_buttons' => false, 'teeny' => true ) ); ?> 
  130. </p> 
  131. </div> 
  132. <?php 
  133.  
  134. public static function save( $variation_id, $i ) { 
  135.  
  136. $data = array( 
  137. '_unit_product' => '',  
  138. '_unit_price_auto' => '',  
  139. '_unit_price_regular' => '',  
  140. '_sale_price_label' => '',  
  141. '_sale_price_regular_label' => '',  
  142. '_unit_price_sale' => '',  
  143. '_parent_unit_product' => '',  
  144. '_parent_unit' => '',  
  145. '_parent_unit_base' => '',  
  146. '_mini_desc' => '',  
  147. '_service' => '',  
  148. 'delivery_time' => '',  
  149. ); 
  150.  
  151. foreach ( $data as $k => $v ) { 
  152.  
  153. $data_k = 'variable' . ( substr( $k, 0, 1) === '_' ? '' : '_' ) . $k; 
  154. $data[ $k ] = ( isset( $_POST[ $data_k ][$i] ) ? $_POST[ $data_k ][$i] : null ); 
  155.  
  156.  
  157. $product = wc_get_product( $variation_id ); 
  158. $product_parent = wc_get_product( wc_gzd_get_crud_data( $product, 'parent' ) ); 
  159.  
  160. // Check if parent has unit_base + unit otherwise ignore data 
  161. if ( empty( $data[ '_parent_unit' ] ) || empty( $data[ '_parent_unit_base' ] ) ) { 
  162.  
  163. $data[ '_unit_price_auto' ] = ''; 
  164. $data[ '_unit_price_regular' ] = ''; 
  165. $data[ '_unit_price_sale' ] = ''; 
  166.  
  167. // If parent has no unit, delete unit_product as well 
  168. if ( empty( $data[ '_parent_unit' ] ) ) { 
  169. $data[ '_unit_product' ] = ''; 
  170.  
  171. $data[ 'product-type' ] = $product_parent->get_type(); 
  172. $data[ '_sale_price_dates_from' ] = $_POST['variable_sale_price_dates_from'][$i]; 
  173. $data[ '_sale_price_dates_to' ] = $_POST['variable_sale_price_dates_to'][$i]; 
  174. $data[ '_sale_price' ] = $_POST['variable_sale_price'][$i]; 
  175.  
  176. $product = WC_Germanized_Meta_Box_Product_Data::save_product_data( $product, $data, true );