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 = wp_get_post_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. <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> 
  86. <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 ) ); ?>" /> 
  87. </p> 
  88. <p class="form-row form-row-last _unit_price_auto_field"> 
  89. <label for="variable_unit_price_auto_<?php echo $loop; ?>"><?php echo __( 'Calculation', 'woocommerce-germanized' ); ?></label> 
  90. <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' ] );?> /> 
  91. <span class="description"> 
  92. <span class="wc-gzd-premium-desc"><?php echo __( 'Calculate unit prices automatically', 'woocommerce-germanized' ); ?></span> 
  93. <a href="https://vendidero.de/woocommerce-germanized#buy" target="_blank" class="wc-gzd-pro">pro</a> 
  94. </span> 
  95. </p> 
  96. <p class="form-row form-row-first"> 
  97. <label for="variable_unit_price_regular"><?php echo __( 'Regular Unit Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')'; ?></label> 
  98. <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="" /> 
  99. </p> 
  100. <p class="form-row form-row-last"> 
  101. <label for="variable_unit_price_sale"><?php echo __( 'Sale Unit Price', 'woocommerce-germanized' ) . ' (' . get_woocommerce_currency_symbol() . ')'; ?></label> 
  102. <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="" /> 
  103. </p> 
  104. </div> 
  105. <div class="variable_shipping_time hide_if_variation_virtual"> 
  106. <p class="form-row form-row-first"> 
  107. <label for="delivery_time"><?php _e( 'Delivery Time', 'woocommerce-germanized' ); ?></label> 
  108. <input type="hidden" class="wc-product-search wc-gzd-delivery-time-search" style="width: 100%" id="variable_delivery_time_<?php echo $loop; ?>" name="variable_delivery_time[<?php echo $loop; ?>]" data-minimum_input_length="1" data-allow_clear="true" data-placeholder="<?php _e( 'Same as parent', '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 : '' ); ?>" /> 
  109. </p> 
  110. </div> 
  111. <div class="variable_cart_mini_desc"> 
  112. <p class="form-row form-row-full"> 
  113. <label for="variable_mini_desc"><?php echo __( 'Optional Mini Description', 'woocommerce-germanized' ); ?></label> 
  114. <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> 
  115. <?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 ) ); ?> 
  116. </p> 
  117. </div> 
  118. <?php 
  119.  
  120. public static function save( $variation_id, $i ) { 
  121.  
  122. $data = array( 
  123. '_unit_product' => '',  
  124. '_unit_price_auto' => '',  
  125. '_unit_price_regular' => '',  
  126. '_sale_price_label' => '',  
  127. '_sale_price_regular_label' => '',  
  128. '_unit_price_sale' => '',  
  129. '_mini_desc' => '',  
  130. '_service' => '',  
  131. 'delivery_time' => '',  
  132. ); 
  133.  
  134. foreach ( $data as $k => $v ) { 
  135.  
  136. $data_k = 'variable' . ( substr( $k, 0, 1) === '_' ? '' : '_' ) . $k; 
  137. $data[ $k ] = ( isset( $_POST[ $data_k ][$i] ) ? $_POST[ $data_k ][$i] : null ); 
  138.  
  139.  
  140. $product = wc_get_product( $variation_id ); 
  141. $parent = wc_gzd_get_gzd_product( $product->parent ); 
  142.  
  143. // Check if parent has unit_base + unit otherwise ignore data 
  144. if ( empty( $parent->unit ) || empty( $parent->unit_base ) ) { 
  145. $data[ '_unit_price_auto' ] = ''; 
  146. $data[ '_unit_price_regular' ] = ''; 
  147. $data[ '_unit_price_sale' ] = ''; 
  148.  
  149. // If parent has no unit, delete unit_product as well 
  150. if ( empty( $parent->unit ) ) { 
  151. $data[ '_unit_product' ] = ''; 
  152.  
  153. $data[ 'product-type' ] = ( isset( $product->parent ) ? $product->parent->product_type : $product->type ); 
  154. $data[ '_sale_price_dates_from' ] = $_POST['variable_sale_price_dates_from'][$i]; 
  155. $data[ '_sale_price_dates_to' ] = $_POST['variable_sale_price_dates_to'][$i]; 
  156. $data[ '_sale_price' ] = $_POST['variable_sale_price'][$i]; 
  157.  
  158. WC_Germanized_Meta_Box_Product_Data::save_product_data( $variation_id, $data, true ); 
  159.  
  160.