WC_GZD_Product

WooCommerce Germanized Abstract Product.

Defined (1)

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

/includes/abstracts/abstract-wc-gzd-product.php  
  1. class WC_GZD_Product { 
  2.  
  3. /** 
  4. * The actual Product object (e.g. simple, variable) 
  5. * @var object 
  6. */ 
  7. private $child; 
  8.  
  9. protected $gzd_variation_level_meta = array( 
  10. 'unit_price' => '',  
  11. 'unit_price_regular' => '',  
  12. 'unit_price_sale' => '',  
  13. 'unit_price_auto' => '',  
  14. 'mini_desc' => '',  
  15. 'service' => '',  
  16. 'gzd_product' => NULL,  
  17. ); 
  18.  
  19. protected $gzd_variation_inherited_meta_data = array( 
  20. 'unit',  
  21. 'unit_base',  
  22. 'unit_product',  
  23. 'sale_price_label',  
  24. 'sale_price_regular_label',  
  25. 'free_shipping',  
  26. ); 
  27.  
  28. /** 
  29. * Construct new WC_GZD_Product 
  30. *  
  31. * @param WC_Product $product  
  32. */ 
  33. public function __construct( $product ) { 
  34.  
  35. if ( is_numeric( $product ) ) 
  36. $product = WC()->product_factory->get_product_standalone( get_post( $product ) ); 
  37.  
  38. $this->child = $product; 
  39.  
  40. /** 
  41. * Redirects __get calls to WC_Product Class. 
  42. *  
  43. * @param string $key 
  44. * @return mixed  
  45. */ 
  46. public function __get( $key ) { 
  47.  
  48. if ( $this->child->is_type( 'variation' ) && in_array( $key, array_keys( $this->gzd_variation_level_meta ) ) ) { 
  49.  
  50. $value = get_post_meta( $this->child->variation_id, '_' . $key, true ); 
  51.  
  52. if ( '' === $value ) 
  53. $value = $this->gzd_variation_level_meta[ $key ]; 
  54.  
  55. } else if ( $this->child->is_type( 'variation' ) && in_array( $key, $this->gzd_variation_inherited_meta_data ) ) { 
  56.  
  57. $value = metadata_exists( 'post', $this->child->variation_id, '_' . $key ) ? get_post_meta( $this->child->variation_id, '_' . $key, true ) : get_post_meta( $this->child->parent->id, '_' . $key, true ); 
  58.  
  59. // Handle meta data keys which can be empty at variation level to cause inheritance 
  60. if ( '' === $value ) { 
  61. $value = get_post_meta( $this->child->parent->id, '_' . $key, true ); 
  62.  
  63. } else if ( $key == 'delivery_time' ) { 
  64.  
  65. $value = $this->get_delivery_time(); 
  66.  
  67. } else { 
  68.  
  69. $value = $this->child->$key; 
  70.  
  71.  
  72. return $value; 
  73.  
  74. /** 
  75. * Redirect issets to WC_Product Class 
  76. *  
  77. * @param string $key  
  78. * @return boolean  
  79. */ 
  80. public function __isset( $key ) { 
  81.  
  82. if ( $this->child->is_type( 'variation' ) && in_array( $key, array_keys( $this->gzd_variation_level_meta ) ) ) { 
  83. return metadata_exists( 'post', $this->child->variation_id, '_' . $key ); 
  84. } else if ( $this->child->is_type( 'variation' ) && in_array( $key, array_keys( $this->gzd_variation_inherited_meta_data ) ) ) { 
  85. return metadata_exists( 'post', $this->child->parent->id, '_' . $key ); 
  86. } else { 
  87. return isset( $this->child->$key ); 
  88.  
  89.  
  90. public function __call( $method, $args ) { 
  91. if ( method_exists( $this->child, $method ) ) 
  92. return call_user_func_array( array( $this->child, $method ), $args ); 
  93. return false; 
  94.  
  95. /** 
  96. * Get a product's cart description 
  97. *  
  98. * @return boolean|string 
  99. */ 
  100. public function get_mini_desc() { 
  101. if ( $this->mini_desc && ! empty( $this->mini_desc ) ) 
  102. return wpautop( htmlspecialchars_decode( $this->mini_desc ) ); 
  103. return false; 
  104.  
  105. public function is_service() { 
  106. if ( ! empty( $this->service ) && 'yes' === $this->service ) 
  107. return true; 
  108. return false; 
  109.  
  110. /** 
  111. * Checks whether current product applies for a virtual VAT exception (downloadable or virtual) 
  112. *  
  113. * @return boolean 
  114. */ 
  115. public function is_virtual_vat_exception() { 
  116. return apply_filters( 'woocommerce_gzd_product_virtual_vat_exception', ( ( get_option( 'woocommerce_gzd_enable_virtual_vat' ) === 'yes' ) && ( $this->is_downloadable() || $this->is_virtual() ) ? true : false ), $this ); 
  117.  
  118. public function get_price_html_from_to( $from, $to, $show_labels = true ) { 
  119.  
  120. $sale_label = ( $show_labels ? $this->get_sale_price_label() : '' ); 
  121. $sale_regular_label = ( $show_labels ? $this->get_sale_price_regular_label() : '' ); 
  122.  
  123. $price = ( ! empty( $sale_label ) ? '<span class="wc-gzd-sale-price-label">' . $sale_label . '</span>' : '' ) . ' <del>' . ( ( is_numeric( $from ) ) ? wc_price( $from ) : $from ) . '</del> ' . ( ! empty( $sale_regular_label ) ? '<span class="wc-gzd-sale-price-label wc-gzd-sale-price-regular-label">' . $sale_regular_label . '</span> ' : '' ) . '<ins>' . ( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) . '</ins>'; 
  124.  
  125. return apply_filters( 'woocommerce_germanized_get_price_html_from_to', $price, $from, $to, $this ); 
  126.  
  127. /** 
  128. * Gets a product's tax description (if is taxable) 
  129. *  
  130. * @return mixed string if is taxable else returns false 
  131. */ 
  132. public function get_tax_info() { 
  133.  
  134. $_tax = new WC_Tax(); 
  135. $tax_notice = false; 
  136.  
  137. if ( $this->is_taxable() ) { 
  138.  
  139. $tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); 
  140. $tax_rates = $_tax->get_rates( $this->get_tax_class() ); 
  141.  
  142. if ( ! empty( $tax_rates ) ) { 
  143.  
  144. $tax_rates = array_values( $tax_rates ); 
  145. // If is variable or is virtual vat exception dont show exact tax rate 
  146. if ( $this->is_virtual_vat_exception() || $this->is_type( 'variable' ) ) 
  147. $tax_notice = ( $tax_display_mode == 'incl' && ! WC()->customer->is_vat_exempt() ? __( 'incl. VAT', 'woocommerce-germanized' ) : __( 'excl. VAT', 'woocommerce-germanized' ) ); 
  148. else 
  149. $tax_notice = ( $tax_display_mode == 'incl' && ! WC()->customer->is_vat_exempt() ? sprintf( __( 'incl. %s%% VAT', 'woocommerce-germanized' ), ( wc_gzd_format_tax_rate_percentage( $tax_rates[0][ 'rate' ] ) ) ) : sprintf( __( 'excl. %s%% VAT', 'woocommerce-germanized' ), ( wc_gzd_format_tax_rate_percentage( $tax_rates[0][ 'rate' ] ) ) ) ); 
  150.  
  151. }  
  152.  
  153. return apply_filters( 'woocommerce_gzd_product_tax_info', $tax_notice, $this ); 
  154.  
  155. /** 
  156. * Checks whether current Product has a unit price 
  157. *  
  158. * @return boolean 
  159. */ 
  160. public function has_unit() { 
  161. if ( $this->unit && $this->unit_price_regular && $this->unit_base ) 
  162. return true; 
  163. return false; 
  164.  
  165. /** 
  166. * Returns unit base html 
  167. *  
  168. * @return string 
  169. */ 
  170. public function get_unit_base() { 
  171. return ( $this->unit_base ) ? ( $this->unit_base != apply_filters( 'woocommerce_gzd_unit_base_hide_amount', 1 ) ? '<span class="unit-base">' . $this->unit_base . '</span>' . apply_filters( 'wc_gzd_unit_price_base_seperator', ' ' ) : '' ) . '<span class="unit">' . $this->get_unit() . '</span>' : ''; 
  172.  
  173. /** 
  174. * Returns unit 
  175. *  
  176. * @return string 
  177. */ 
  178. public function get_unit() { 
  179. $unit = $this->unit; 
  180. return WC_germanized()->units->$unit; 
  181.  
  182. /** 
  183. * Returns sale price label 
  184. *  
  185. * @return string 
  186. */ 
  187. public function get_sale_price_label() { 
  188.  
  189. $default = get_option( 'woocommerce_gzd_default_sale_price_label', '' ); 
  190. $label = ( ! empty( $this->sale_price_label ) ? $this->sale_price_label : $default ); 
  191.  
  192. return ( ! empty( $label ) ? WC_germanized()->price_labels->$label : '' ); 
  193.  
  194. /** 
  195. * Returns sale price regular label 
  196. *  
  197. * @return string 
  198. */ 
  199. public function get_sale_price_regular_label() { 
  200.  
  201. $default = get_option( 'woocommerce_gzd_default_sale_price_regular_label', '' ); 
  202. $label = ( ! empty( $this->sale_price_regular_label ) ? $this->sale_price_regular_label : $default ); 
  203.  
  204. return ( ! empty( $label ) ? WC_germanized()->price_labels->$label : '' ); 
  205.  
  206. /** 
  207. * Returns unit regular price 
  208. *  
  209. * @return string the regular price 
  210. */ 
  211. public function get_unit_regular_price() { 
  212. return apply_filters( 'woocommerce_gzd_get_unit_regular_price', $this->unit_price_regular, $this ); 
  213.  
  214. /** 
  215. * Returns unit sale price 
  216. *  
  217. * @return string the sale price  
  218. */ 
  219. public function get_unit_sale_price() { 
  220. return apply_filters( 'woocommerce_gzd_get_unit_sale_price', $this->unit_price_sale, $this ); 
  221.  
  222. /** 
  223. * Returns the unit price (if is sale then return sale price) 
  224. *  
  225. * @param integer $qty  
  226. * @param string $price  
  227. * @return string formatted unit price 
  228. */ 
  229. public function get_unit_price( $qty = 1, $price = '' ) { 
  230. $tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); 
  231. return ( $tax_display_mode == 'incl' ) ? $this->get_unit_price_including_tax( $qty, $price ) : $this->get_unit_price_excluding_tax( $qty, $price ); 
  232.  
  233. /** 
  234. * Returns unit price including tax 
  235. *  
  236. * @param integer $qty  
  237. * @param string $price  
  238. * @return string unit price including tax 
  239. */ 
  240. public function get_unit_price_including_tax( $qty = 1, $price = '' ) { 
  241. $price = ( $price == '' ) ? $this->unit_price : $price; 
  242. return ( $price == '' ) ? '' : $this->get_price_including_tax( $qty, $price ); 
  243.  
  244. /** 
  245. * Returns unit price excluding tax 
  246. *  
  247. * @param integer $qty  
  248. * @param string $price  
  249. * @return string unit price excluding tax 
  250. */ 
  251. public function get_unit_price_excluding_tax( $qty = 1, $price = '' ) { 
  252. $price = ( $price == '' ) ? $this->unit_price : $price; 
  253. return ( $price == '' ) ? '' : $this->get_price_excluding_tax( $qty, $price ); 
  254.  
  255. /** 
  256. * Checks whether unit price is on sale 
  257. *  
  258. * @return boolean  
  259. */ 
  260. public function is_on_unit_sale() { 
  261. return apply_filters( 'woocommerce_gzd_product_is_on_unit_sale', ( $this->get_unit_sale_price() !== $this->get_unit_regular_price() && $this->get_unit_sale_price() == $this->get_unit_price() ), $this ); 
  262.  
  263. /** 
  264. * Returns unit price html output 
  265. *  
  266. * @return string  
  267. */ 
  268. public function get_unit_html( $show_sale = true ) { 
  269.  
  270. if ( apply_filters( 'woocommerce_gzd_hide_unit_text', false, $this ) ) 
  271. return apply_filters( 'woocommerce_germanized_disabled_unit_text', '', $this ); 
  272.  
  273. $display_price = $this->get_unit_price(); 
  274. $display_regular_price = $this->get_unit_price( 1, $this->get_unit_regular_price() ); 
  275. $display_sale_price = $this->get_unit_price( 1, $this->get_unit_sale_price() ); 
  276.  
  277. $price_html = ( ( $this->is_on_unit_sale() && $show_sale ) ? $this->get_price_html_from_to( $display_regular_price, $display_sale_price, false ) : wc_price( $display_price ) ); 
  278. $html = ''; 
  279. $text = get_option( 'woocommerce_gzd_unit_price_text' ); 
  280.  
  281. if ( $this->has_unit() ) { 
  282.  
  283. if ( strpos( $text, '{price}' ) !== false ) { 
  284.  
  285. $html = str_replace( '{price}', $price_html . apply_filters( 'wc_gzd_unit_price_seperator', ' / ' ) . $this->get_unit_base(), $text ); 
  286.  
  287. } else { 
  288.  
  289. $html = str_replace( array( '{base_price}', '{unit}', '{base}' ), array( $price_html, '<span class="unit">' . $this->get_unit() . '</span>', ( $this->unit_base != apply_filters( 'woocommerce_gzd_unit_base_hide_amount', 1 ) ? '<span class="unit-base">' . $this->unit_base . '</span>' : '' ) ), $text ); 
  290.  
  291.  
  292.  
  293. return apply_filters( 'woocommerce_gzd_unit_price_html', $html, $this ); 
  294.  
  295. public function has_product_units() { 
  296. return ( $this->unit_product && ! empty( $this->unit_product ) && $this->get_unit() ); 
  297.  
  298. /** 
  299. * Formats the amount of product units 
  300. *  
  301. * @return string  
  302. */ 
  303. public function get_product_units_html() { 
  304.  
  305. if ( apply_filters( 'woocommerce_gzd_hide_product_units_text', false, $this ) ) 
  306. return apply_filters( 'woocommerce_germanized_disabled_product_units_text', '', $this ); 
  307.  
  308. $html = ''; 
  309. $text = get_option( 'woocommerce_gzd_product_units_text' ); 
  310.  
  311. if ( $this->has_product_units() ) 
  312. $html = str_replace( array( '{product_units}', '{unit}', '{unit_price}' ), array( str_replace( '.', ', ', $this->unit_product ), $this->get_unit(), $this->get_unit_html() ), $text ); 
  313.  
  314. return apply_filters( 'woocommerce_gzd_product_units_html', $html, $this ); 
  315.  
  316.  
  317. /** 
  318. * Returns the current products delivery time term without falling back to default term 
  319. *  
  320. * @return bool|object false returns false if term does not exist otherwise returns term object 
  321. */ 
  322. public function get_delivery_time() { 
  323.  
  324. $terms = wp_get_post_terms( $this->id, 'product_delivery_time' ); 
  325.  
  326. if ( $this->child->is_type( 'variation' ) ) { 
  327. $variation_terms = wp_get_post_terms( $this->child->variation_id , 'product_delivery_time' ); 
  328. if ( ! empty( $variation_terms ) && ! is_wp_error( $variation_terms ) ) 
  329. $terms = $variation_terms; 
  330.  
  331. if ( is_wp_error( $terms ) || empty( $terms ) ) 
  332. return false; 
  333.  
  334. return $terms[ 0 ]; 
  335.  
  336. /** 
  337. * Returns current product's delivery time term. If none has been set and a default delivery time has been set, returns that instead. 
  338. *  
  339. * @return object 
  340. */ 
  341. public function get_delivery_time_term() { 
  342.  
  343. $delivery_time = $this->delivery_time; 
  344.  
  345. if ( empty( $delivery_time ) && get_option( 'woocommerce_gzd_default_delivery_time' ) && ! $this->is_downloadable() ) { 
  346.  
  347. $delivery_time = array( get_term_by( 'id', get_option( 'woocommerce_gzd_default_delivery_time' ), 'product_delivery_time' ) ); 
  348. if ( is_array( $delivery_time ) ) { 
  349. array_values( $delivery_time ); 
  350. $delivery_time = $delivery_time[0]; 
  351.  
  352. return ( ! is_wp_error( $delivery_time ) && ! empty( $delivery_time ) ) ? $delivery_time : false; 
  353.  
  354. /** 
  355. * Returns the delivery time html output 
  356. *  
  357. * @return string  
  358. */ 
  359. public function get_delivery_time_html() { 
  360.  
  361. $html = ''; 
  362.  
  363. if ( apply_filters( 'woocommerce_germanized_hide_delivery_time_text', false, $this ) ) 
  364. return apply_filters( 'woocommerce_germanized_disabled_delivery_time_text', '', $this ); 
  365.  
  366. if ( $this->get_delivery_time_term() ) { 
  367. $html = $this->get_delivery_time_term()->name; 
  368. } else { 
  369. $html = apply_filters( 'woocommerce_germanized_empty_delivery_time_text', '', $this ); 
  370.  
  371. return ( ! empty( $html ) ? apply_filters( 'woocommerce_germanized_delivery_time_html', str_replace( '{delivery_time}', $html, get_option( 'woocommerce_gzd_delivery_time_text' ) ), $html ) : '' ); 
  372.  
  373. public function has_free_shipping() { 
  374. return ( apply_filters( 'woocommerce_germanized_product_has_free_shipping', ( $this->free_shipping === 'yes' ? true : false ), $this ) ); 
  375.  
  376. /** 
  377. * Returns the shipping costs notice html output 
  378. *  
  379. * @return string  
  380. */ 
  381. public function get_shipping_costs_html() { 
  382.  
  383. if ( apply_filters( 'woocommerce_germanized_hide_shipping_costs_text', false, $this ) ) 
  384. return apply_filters( 'woocommerce_germanized_disabled_shipping_text', '', $this ); 
  385.  
  386. $find = array( 
  387. '{link}',  
  388. '{/link}' 
  389. ); 
  390.  
  391. $replace = array( 
  392. '<a href="' . esc_url( get_permalink( wc_get_page_id( 'shipping_costs' ) ) ) . '" target="_blank">',  
  393. '</a>' 
  394. ); 
  395.  
  396. return apply_filters( 'woocommerce_gzd_shipping_costs_text', str_replace( $find, $replace, ( $this->has_free_shipping() ? get_option( 'woocommerce_gzd_free_shipping_text' ) : get_option( 'woocommerce_gzd_shipping_costs_text' ) ) ), $this ); 
  397.