WC_GZD_Product_Variable

WooCommerce Germanized Product Variable.

Defined (1)

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

/includes/class-wc-gzd-product-variable.php  
  1. class WC_GZD_Product_Variable extends WC_GZD_Product { 
  2.  
  3. /** 
  4. * Get the min or max variation unit regular price. 
  5. * @param string $min_or_max - min or max 
  6. * @param boolean $display Whether the value is going to be displayed 
  7. * @return string 
  8. */ 
  9. public function get_variation_unit_regular_price( $min_or_max = 'min', $display = false ) { 
  10. $prices = $this->get_variation_unit_prices( $display ); 
  11. $price = 'min' === $min_or_max ? current( $prices['regular_price'] ) : end( $prices['regular_price'] ); 
  12. return apply_filters( 'woocommerce_gzd_get_variation_unit_regular_price', $price, $this, $min_or_max, $display ); 
  13.  
  14. /** 
  15. * Get the min or max variation unit sale price. 
  16. * @param string $min_or_max - min or max 
  17. * @param boolean $display Whether the value is going to be displayed 
  18. * @return string 
  19. */ 
  20. public function get_variation_unit_sale_price( $min_or_max = 'min', $display = false ) { 
  21. $prices = $this->get_variation_unit_prices( $display ); 
  22. $price = 'min' === $min_or_max ? current( $prices['sale_price'] ) : end( $prices['sale_price'] ); 
  23. return apply_filters( 'woocommerce_gzd_get_variation_unit_sale_price', $price, $this, $min_or_max, $display ); 
  24.  
  25. /** 
  26. * Get the min or max variation (active) unit price. 
  27. * @param string $min_or_max - min or max 
  28. * @param boolean $display Whether the value is going to be displayed 
  29. * @return string 
  30. */ 
  31. public function get_variation_unit_price( $min_or_max = 'min', $display = false ) { 
  32. $prices = $this->get_variation_unit_prices( $display ); 
  33. $price = 'min' === $min_or_max ? current( $prices['price'] ) : end( $prices['price'] ); 
  34. return apply_filters( 'woocommerce_gzd_get_variation_unit_price', $price, $this, $min_or_max, $display ); 
  35.  
  36. public function is_on_unit_sale() { 
  37. $is_on_sale = false; 
  38. $prices = $this->get_variation_unit_prices(); 
  39. if ( $prices['regular_price'] !== $prices['sale_price'] && $prices['sale_price'] === $prices['price'] ) { 
  40. $is_on_sale = true; 
  41. return apply_filters( 'woocommerce_gzd_product_is_on_unit_sale', $is_on_sale, $this ); 
  42.  
  43. public function has_unit() { 
  44. $prices = $this->get_variation_unit_prices(); 
  45. if ( $this->unit && $prices['regular_price'] && $this->unit_base ) 
  46. return true; 
  47. return false; 
  48.  
  49. /** 
  50. * Returns the price in html format. 
  51. * @access public 
  52. * @param string $price (default: '') 
  53. * @return string 
  54. */ 
  55. public function get_unit_html( $price = '' ) { 
  56.  
  57. if ( get_option( 'woocommerce_gzd_unit_price_enable_variable' ) === 'no' ) 
  58. return ''; 
  59.  
  60. $prices = $this->get_variation_unit_prices( true ); 
  61. $text = get_option( 'woocommerce_gzd_unit_price_text' ); 
  62.  
  63. if ( $this->has_unit() ) { 
  64.  
  65. $min_price = current( $prices['price'] ); 
  66. $max_price = end( $prices['price'] ); 
  67. $price = $min_price !== $max_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce-germanized' ), wc_price( $min_price ), wc_price( $max_price ) ) : wc_price( $min_price ); 
  68.  
  69. if ( $this->is_on_sale() ) { 
  70. $min_regular_price = current( $prices['regular_price'] ); 
  71. $max_regular_price = end( $prices['regular_price'] ); 
  72. $regular_price = $min_regular_price !== $max_regular_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce-germanized' ), wc_price( $min_regular_price ), wc_price( $max_regular_price ) ) : wc_price( $min_regular_price ); 
  73. $price = apply_filters( 'woocommerce_gzd_variable_unit_sale_price_html', $this->get_price_html_from_to( $regular_price, $price, false ), $this ); 
  74. } else { 
  75. $price = apply_filters( 'woocommerce_gzd_variable_unit_price_html', $price, $this ); 
  76.  
  77. if ( strpos( $text, '{price}' ) !== false ) { 
  78.  
  79. $price = str_replace( '{price}', $price . apply_filters( 'wc_gzd_unit_price_seperator', ' / ' ) . $this->get_unit_base(), $text ); 
  80.  
  81. } else { 
  82.  
  83. $price = str_replace( array( '{base_price}', '{unit}', '{base}' ), array( $price, '<span class="unit">' . $this->get_unit() . '</span>', ( $this->unit_base != apply_filters( 'wc_gzd_unit_base_min_amount_to_show', 1 ) ? '<span class="unit-base">' . $this->unit_base . '</span>' : '' ) ), $text ); 
  84.  
  85.  
  86.  
  87. return apply_filters( 'woocommerce_gzd_unit_price_html', $price, $this ); 
  88.  
  89. /** 
  90. * Get an array of all sale and regular unit prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale. 
  91. * Can be filtered by plugins which modify costs, but otherwise will include the raw meta costs unlike get_price() which runs costs through the woocommerce_get_price filter. 
  92. * This is to ensure modified prices are not cached, unless intended. 
  93. * @param bool $display Are prices for display? If so, taxes will be calculated. 
  94. * @return array() Array of RAW prices, regular prices, and sale prices with keys set to variation ID. 
  95. */ 
  96. public function get_variation_unit_prices( $display = false ) { 
  97.  
  98. if ( ! $this->is_type( 'variable' ) ) 
  99. return false;  
  100.  
  101. global $wp_filter; 
  102.  
  103. $transient_name = 'wc_gzd_var_unit_prices_' . $this->id; 
  104.  
  105. /** 
  106. * Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters. 
  107. * DEVELOPERS should filter this hash if offering conditonal pricing to keep it unique. 
  108. * @var string 
  109. */ 
  110. if ( $display ) { 
  111. $price_hash = array( get_option( 'woocommerce_tax_display_shop', 'excl' ), WC_Tax::get_rates() ); 
  112. } else { 
  113. $price_hash = array( false ); 
  114.  
  115. $filter_names = array( 'woocommerce_gzd_variation_unit_prices_price', 'woocommerce_gzd_variation_unit_prices_regular_price', 'woocommerce_gzd_variation_unit_prices_sale_price' ); 
  116.  
  117. foreach ( $filter_names as $filter_name ) { 
  118. if ( ! empty( $wp_filter[ $filter_name ] ) ) { 
  119. $price_hash[ $filter_name ] = array(); 
  120.  
  121. foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) { 
  122. $price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) ); 
  123.  
  124. $price_hash = md5( json_encode( apply_filters( 'woocommerce_gzd_get_variation_unit_prices_hash', $price_hash, $this, $display ) ) ); 
  125.  
  126. // If the value has already been generated, we don't need to grab the values again. 
  127. if ( empty( $this->unit_prices_array[ $price_hash ] ) ) { 
  128.  
  129. // Get value of transient 
  130. $this->unit_prices_array = array_filter( (array) json_decode( strval( get_transient( $transient_name ) ), true ) ); 
  131.  
  132. // If the product version has changed, reset cache 
  133. if ( empty( $this->unit_prices_array['version'] ) || $this->unit_prices_array['version'] !== WC_Cache_Helper::get_transient_version( 'product' ) ) { 
  134. $this->unit_prices_array = array( 'version' => WC_Cache_Helper::get_transient_version( 'product' ) ); 
  135.  
  136. // If the prices are not stored for this hash, generate them 
  137. if ( empty( $this->unit_prices_array[ $price_hash ] ) ) { 
  138.  
  139. $prices = array(); 
  140. $regular_prices = array(); 
  141. $sale_prices = array(); 
  142. $variation_ids = $this->get_children( true ); 
  143.  
  144. foreach ( $variation_ids as $variation_id ) { 
  145.  
  146. if ( $variation = $this->get_child( $variation_id ) ) { 
  147.  
  148. $price = apply_filters( 'woocommerce_gzd_variation_unit_prices_price', $variation->unit_price, $variation, $this ); 
  149. $regular_price = apply_filters( 'woocommerce_gzd_variation_unit_prices_regular_price', $variation->unit_price_regular, $variation, $this ); 
  150. $sale_price = apply_filters( 'woocommerce_gzd_variation_unit_prices_sale_price', $variation->unit_price_sale, $variation, $this ); 
  151.  
  152. // If sale price does not equal price, the product is not yet on sale 
  153. if ( $sale_price === $regular_price || $sale_price !== $price ) { 
  154. $sale_price = $regular_price; 
  155.  
  156. // If we are getting prices for display, we need to account for taxes 
  157. if ( $display ) { 
  158. if ( 'incl' === get_option( 'woocommerce_tax_display_shop' ) ) { 
  159. $price = '' === $price ? '' : $variation->get_price_including_tax( 1, $price ); 
  160. $regular_price = '' === $regular_price ? '' : $variation->get_price_including_tax( 1, $regular_price ); 
  161. $sale_price = '' === $sale_price ? '' : $variation->get_price_including_tax( 1, $sale_price ); 
  162. } else { 
  163. $price = '' === $price ? '' : $variation->get_price_excluding_tax( 1, $price ); 
  164. $regular_price = '' === $regular_price ? '' : $variation->get_price_excluding_tax( 1, $regular_price ); 
  165. $sale_price = '' === $sale_price ? '' : $variation->get_price_excluding_tax( 1, $sale_price ); 
  166.  
  167. $prices[ $variation_id ] = $price; 
  168. $regular_prices[ $variation_id ] = $regular_price; 
  169. $sale_prices[ $variation_id ] = $sale_price; 
  170.  
  171. asort( $prices ); 
  172. asort( $regular_prices ); 
  173. asort( $sale_prices ); 
  174.  
  175. $this->unit_prices_array[ $price_hash ] = array( 
  176. 'price' => $prices,  
  177. 'regular_price' => $regular_prices,  
  178. 'sale_price' => $sale_prices 
  179. ); 
  180.  
  181. set_transient( $transient_name, json_encode( $this->unit_prices_array ), DAY_IN_SECONDS * 30 ); 
  182.  
  183.  
  184. $this->unit_prices_array[ $price_hash ] = apply_filters( 'woocommerce_gzd_variation_unit_prices', $this->unit_prices_array[ $price_hash ], $this, $display ); 
  185.  
  186.  
  187. return $this->unit_prices_array[ $price_hash ]; 
  188.  
  189.