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. protected $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. public function get_wc_product() { 
  41. return $this->child; 
  42.  
  43. /** 
  44. * Redirects __get calls to WC_Product Class. 
  45. *  
  46. * @param string $key 
  47. * @return mixed  
  48. */ 
  49. public function __get( $key ) { 
  50.  
  51. if ( $this->child->is_type( 'variation' ) && in_array( $key, array_keys( $this->gzd_variation_level_meta ) ) ) { 
  52.  
  53. $value = wc_gzd_get_crud_data( $this->child, $key ); 
  54.  
  55. if ( '' === $value ) 
  56. $value = $this->gzd_variation_level_meta[ $key ]; 
  57.  
  58. } elseif ( $this->child->is_type( 'variation' ) && in_array( $key, $this->gzd_variation_inherited_meta_data ) ) { 
  59.  
  60. $value = wc_gzd_get_crud_data( $this->child, $key ) ? wc_gzd_get_crud_data( $this->child, $key ) : ''; 
  61.  
  62. // Handle meta data keys which can be empty at variation level to cause inheritance 
  63. if ( ! $value || '' === $value ) { 
  64. $value = wc_gzd_get_crud_data( wc_get_product( wc_gzd_get_crud_data( $this->child, 'parent' ) ), $key ); 
  65.  
  66. } elseif ( $key == 'delivery_time' ) { 
  67.  
  68. $value = $this->get_delivery_time(); 
  69.  
  70. } else { 
  71.  
  72. if ( strpos( '_', $key ) !== true ) 
  73. $key = '_' . $key; 
  74.  
  75. $value = wc_gzd_get_crud_data( $this->child, $key ); 
  76.  
  77. return $value; 
  78.  
  79. /** 
  80. * Redirect issets to WC_Product Class 
  81. *  
  82. * @param string $key  
  83. * @return boolean  
  84. */ 
  85. public function __isset( $key ) { 
  86. if ( $this->child->is_type( 'variation' ) && in_array( $key, array_keys( $this->gzd_variation_level_meta ) ) ) { 
  87. return metadata_exists( 'post', wc_gzd_get_crud_data( $this->child, 'id' ), '_' . $key ); 
  88. } elseif ( $this->child->is_type( 'variation' ) && in_array( $key, array_keys( $this->gzd_variation_inherited_meta_data ) ) ) { 
  89. return metadata_exists( 'post', wc_gzd_get_crud_data( $this->child, 'id' ), '_' . $key ) || metadata_exists( 'post', wc_gzd_get_crud_data( $this->child, 'parent' ), '_' . $key ); 
  90. } else { 
  91. return metadata_exists( 'post', wc_gzd_get_crud_data( $this->child, 'id' ), '_' . $key ); 
  92.  
  93. public function __call( $method, $args ) { 
  94. if ( method_exists( $this->child, $method ) ) 
  95. return call_user_func_array( array( $this->child, $method ), $args ); 
  96. return false; 
  97.  
  98. /** 
  99. * Get a product's cart description 
  100. *  
  101. * @return boolean|string 
  102. */ 
  103. public function get_mini_desc() { 
  104.  
  105. $mini_desc = apply_filters( 'woocommerce_gzd_product_cart_description', $this->mini_desc, $this ); 
  106.  
  107. if ( $mini_desc && ! empty( $mini_desc ) ) 
  108. return wpautop( htmlspecialchars_decode( $mini_desc ) ); 
  109.  
  110. return false; 
  111.  
  112. public function is_service() { 
  113. if ( ! empty( $this->service ) && 'yes' === $this->service ) 
  114. return true; 
  115. return false; 
  116.  
  117. /** 
  118. * Checks whether current product applies for a virtual VAT exception (downloadable or virtual) 
  119. *  
  120. * @return boolean 
  121. */ 
  122. public function is_virtual_vat_exception() { 
  123. 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 ); 
  124.  
  125. public function add_labels_to_price_html( $price_html ) { 
  126.  
  127. $org_price_html = $price_html; 
  128.  
  129. if ( ! $this->child->is_on_sale() ) 
  130. return $price_html; 
  131.  
  132. $sale_label = $this->get_sale_price_label(); 
  133. $sale_regular_label = $this->get_sale_price_regular_label(); 
  134.  
  135. // Do not manipulate if there is no label to be added. 
  136. if ( empty( $sale_label ) && empty( $sale_regular_label ) ) { 
  137. return $price_html; 
  138.  
  139. preg_match( "/<del>(.*?)<\\/del>/si", $price_html, $match_regular ); 
  140. preg_match( "/<ins>(.*?)<\\/ins>/si", $price_html, $match_sale ); 
  141. preg_match( "/<small .*>(.*?)<\\/small>/si", $price_html, $match_suffix ); 
  142.  
  143. if ( empty( $match_sale ) || empty( $match_regular ) ) { 
  144. return $price_html; 
  145.  
  146. $new_price_regular = $match_regular[0]; 
  147. $new_price_sale = $match_sale[0]; 
  148. $new_price_suffix = ( empty( $match_suffix ) ? '' : ' ' . $match_suffix[0] ); 
  149.  
  150. if ( ! empty( $sale_label ) && isset( $match_regular[1] ) ) 
  151. $new_price_regular = '<span class="wc-gzd-sale-price-label">' . $sale_label . '</span> ' . $match_regular[0]; 
  152.  
  153. if ( ! empty( $sale_regular_label ) && isset( $match_sale[1] ) ) 
  154. $new_price_sale = '<span class="wc-gzd-sale-price-label wc-gzd-sale-price-regular-label">' . $sale_regular_label . '</span> ' . $match_sale[0]; 
  155.  
  156. return apply_filters( 'woocommerce_gzd_product_sale_price_with_labels_html', $new_price_regular . ' ' . $new_price_sale . $new_price_suffix, $org_price_html, $this ); 
  157.  
  158. public function get_price_html_from_to( $from, $to, $show_labels = true ) { 
  159.  
  160. $sale_label = ( $show_labels ? $this->get_sale_price_label() : '' ); 
  161. $sale_regular_label = ( $show_labels ? $this->get_sale_price_regular_label() : '' ); 
  162.  
  163. $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>'; 
  164.  
  165. return apply_filters( 'woocommerce_germanized_get_price_html_from_to', $price, $from, $to, $this ); 
  166.  
  167. /** 
  168. * Gets a product's tax description (if is taxable) 
  169. *  
  170. * @return mixed string if is taxable else returns false 
  171. */ 
  172. public function get_tax_info() { 
  173.  
  174. $tax_notice = false; 
  175.  
  176. if ( $this->is_taxable() ) { 
  177.  
  178. $tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); 
  179. $tax_rates = WC_Tax::get_rates( $this->get_tax_class() ); 
  180.  
  181. if ( ! empty( $tax_rates ) ) { 
  182.  
  183. $tax_rates = array_values( $tax_rates ); 
  184. // If is variable or is virtual vat exception dont show exact tax rate 
  185. if ( $this->is_virtual_vat_exception() || $this->is_type( 'variable' ) || get_option( 'woocommerce_gzd_hide_tax_rate_shop' ) === 'yes' ) 
  186. $tax_notice = ( $tax_display_mode == 'incl' && ! WC()->customer->is_vat_exempt() ? __( 'incl. VAT', 'woocommerce-germanized' ) : __( 'excl. VAT', 'woocommerce-germanized' ) ); 
  187. else 
  188. $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' ] ) ) ) ); 
  189.  
  190. }  
  191.  
  192. return apply_filters( 'woocommerce_gzd_product_tax_info', $tax_notice, $this ); 
  193.  
  194. /** 
  195. * Checks whether current Product has a unit price 
  196. *  
  197. * @return boolean 
  198. */ 
  199. public function has_unit() { 
  200. if ( $this->unit && $this->unit_price_regular && $this->unit_base ) 
  201. return true; 
  202. return false; 
  203.  
  204. /** 
  205. * Returns unit base html 
  206. *  
  207. * @return string 
  208. */ 
  209. public function get_unit_base() { 
  210. 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>' : ''; 
  211.  
  212. /** 
  213. * Returns unit 
  214. *  
  215. * @return string 
  216. */ 
  217. public function get_unit() { 
  218. $unit = $this->unit; 
  219. return WC_germanized()->units->$unit; 
  220.  
  221. /** 
  222. * Returns sale price label 
  223. *  
  224. * @return string 
  225. */ 
  226. public function get_sale_price_label() { 
  227.  
  228. $default = get_option( 'woocommerce_gzd_default_sale_price_label', '' ); 
  229. $label = ( ! empty( $this->sale_price_label ) ? $this->sale_price_label : $default ); 
  230.  
  231. return ( ! empty( $label ) ? WC_germanized()->price_labels->$label : '' ); 
  232.  
  233. /** 
  234. * Returns sale price regular label 
  235. *  
  236. * @return string 
  237. */ 
  238. public function get_sale_price_regular_label() { 
  239.  
  240. $default = get_option( 'woocommerce_gzd_default_sale_price_regular_label', '' ); 
  241. $label = ( ! empty( $this->sale_price_regular_label ) ? $this->sale_price_regular_label : $default ); 
  242.  
  243. return ( ! empty( $label ) ? WC_germanized()->price_labels->$label : '' ); 
  244.  
  245. /** 
  246. * Returns unit regular price 
  247. *  
  248. * @return string the regular price 
  249. */ 
  250. public function get_unit_regular_price() { 
  251. return apply_filters( 'woocommerce_gzd_get_unit_regular_price', $this->unit_price_regular, $this ); 
  252.  
  253. /** 
  254. * Returns unit sale price 
  255. *  
  256. * @return string the sale price  
  257. */ 
  258. public function get_unit_sale_price() { 
  259. return apply_filters( 'woocommerce_gzd_get_unit_sale_price', $this->unit_price_sale, $this ); 
  260.  
  261. /** 
  262. * Returns the unit price (if is sale then return sale price) 
  263. *  
  264. * @param integer $qty  
  265. * @param string $price  
  266. * @return string formatted unit price 
  267. */ 
  268. public function get_unit_price( $qty = 1, $price = '' ) { 
  269. $tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); 
  270. return ( $tax_display_mode == 'incl' ) ? $this->get_unit_price_including_tax( $qty, $price ) : $this->get_unit_price_excluding_tax( $qty, $price ); 
  271.  
  272. /** 
  273. * Returns unit price including tax 
  274. *  
  275. * @param integer $qty  
  276. * @param string $price  
  277. * @return string unit price including tax 
  278. */ 
  279. public function get_unit_price_including_tax( $qty = 1, $price = '' ) { 
  280. $price = ( $price == '' ) ? $this->unit_price : $price; 
  281. return ( $price == '' ) ? '' : wc_gzd_get_price_including_tax( $this->child, array( 'price' => $price, 'qty' => $qty ) ); 
  282.  
  283. /** 
  284. * Returns unit price excluding tax 
  285. *  
  286. * @param integer $qty  
  287. * @param string $price  
  288. * @return string unit price excluding tax 
  289. */ 
  290. public function get_unit_price_excluding_tax( $qty = 1, $price = '' ) { 
  291. $price = ( $price == '' ) ? $this->unit_price : $price; 
  292. return ( $price == '' ) ? '' : wc_gzd_get_price_excluding_tax( $this->child, array( 'price' => $price, 'qty' => $qty ) ); 
  293.  
  294. /** 
  295. * Checks whether unit price is on sale 
  296. *  
  297. * @return boolean  
  298. */ 
  299. public function is_on_unit_sale() { 
  300. 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 ); 
  301.  
  302. /** 
  303. * Returns unit price html output 
  304. *  
  305. * @return string  
  306. */ 
  307. public function get_unit_html( $show_sale = true ) { 
  308.  
  309. if ( apply_filters( 'woocommerce_gzd_hide_unit_text', false, $this ) ) 
  310. return apply_filters( 'woocommerce_germanized_disabled_unit_text', '', $this ); 
  311.  
  312. $display_price = $this->get_unit_price(); 
  313. $display_regular_price = $this->get_unit_price( 1, $this->get_unit_regular_price() ); 
  314. $display_sale_price = $this->get_unit_price( 1, $this->get_unit_sale_price() ); 
  315.  
  316. $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 ) ); 
  317. $html = ''; 
  318. $text = get_option( 'woocommerce_gzd_unit_price_text' ); 
  319.  
  320. if ( $this->has_unit() ) { 
  321.  
  322. if ( strpos( $text, '{price}' ) !== false ) { 
  323.  
  324. $html = str_replace( '{price}', $price_html . apply_filters( 'wc_gzd_unit_price_seperator', ' / ' ) . $this->get_unit_base(), $text ); 
  325.  
  326. } else { 
  327.  
  328. $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 ); 
  329.  
  330.  
  331.  
  332. return apply_filters( 'woocommerce_gzd_unit_price_html', $html, $this ); 
  333.  
  334. public function is_unit_price_calculated_automatically() { 
  335. return $this->unit_price_auto === 'yes'; 
  336.  
  337. public function get_unit_products() { 
  338. return $this->unit_product; 
  339.  
  340. public function has_product_units() { 
  341. $products = $this->get_unit_products(); 
  342. return ( $products && ! empty( $products ) && $this->get_unit() ); 
  343.  
  344. /** 
  345. * Formats the amount of product units 
  346. *  
  347. * @return string  
  348. */ 
  349. public function get_product_units_html() { 
  350.  
  351. if ( apply_filters( 'woocommerce_gzd_hide_product_units_text', false, $this ) ) 
  352. return apply_filters( 'woocommerce_germanized_disabled_product_units_text', '', $this ); 
  353.  
  354. $html = ''; 
  355. $text = get_option( 'woocommerce_gzd_product_units_text' ); 
  356.  
  357. if ( $this->has_product_units() ) 
  358. $html = str_replace( array( '{product_units}', '{unit}', '{unit_price}' ), array( str_replace( '.', ', ', $this->get_unit_products() ), $this->get_unit(), $this->get_unit_html() ), $text ); 
  359.  
  360. return apply_filters( 'woocommerce_gzd_product_units_html', $html, $this ); 
  361.  
  362.  
  363. /** 
  364. * Returns the current products delivery time term without falling back to default term 
  365. *  
  366. * @return bool|object false returns false if term does not exist otherwise returns term object 
  367. */ 
  368. public function get_delivery_time() { 
  369.  
  370. $terms = get_the_terms( wc_gzd_get_crud_data( $this->child, 'id' ), 'product_delivery_time' ); 
  371.  
  372. if ( empty( $terms ) && $this->child->is_type( 'variation' ) ) { 
  373.  
  374. $parent_terms = get_the_terms( wc_gzd_get_crud_data( $this->child, 'parent' ), 'product_delivery_time' ); 
  375.  
  376. if ( ! empty( $parent_terms ) && ! is_wp_error( $parent_terms ) ) 
  377. $terms = $parent_terms; 
  378.  
  379. if ( is_wp_error( $terms ) || empty( $terms ) ) 
  380. return false; 
  381.  
  382. return $terms[ 0 ]; 
  383.  
  384. /** 
  385. * Returns current product's delivery time term. If none has been set and a default delivery time has been set, returns that instead. 
  386. *  
  387. * @return object 
  388. */ 
  389. public function get_delivery_time_term() { 
  390.  
  391. $delivery_time = $this->delivery_time; 
  392.  
  393. if ( empty( $delivery_time ) && get_option( 'woocommerce_gzd_default_delivery_time' ) && ! $this->is_downloadable() ) { 
  394.  
  395. $delivery_time = array( get_term_by( 'id', get_option( 'woocommerce_gzd_default_delivery_time' ), 'product_delivery_time' ) ); 
  396. if ( is_array( $delivery_time ) ) { 
  397. array_values( $delivery_time ); 
  398. $delivery_time = $delivery_time[0]; 
  399.  
  400. return ( ! is_wp_error( $delivery_time ) && ! empty( $delivery_time ) ) ? $delivery_time : false; 
  401.  
  402. /** 
  403. * Returns the delivery time html output 
  404. *  
  405. * @return string  
  406. */ 
  407. public function get_delivery_time_html() { 
  408.  
  409. $html = ''; 
  410.  
  411. if ( apply_filters( 'woocommerce_germanized_hide_delivery_time_text', false, $this ) ) 
  412. return apply_filters( 'woocommerce_germanized_disabled_delivery_time_text', '', $this ); 
  413.  
  414. if ( $this->get_delivery_time_term() ) { 
  415. $html = $this->get_delivery_time_term()->name; 
  416. } else { 
  417. $html = apply_filters( 'woocommerce_germanized_empty_delivery_time_text', '', $this ); 
  418.  
  419. return ( ! empty( $html ) ? apply_filters( 'woocommerce_germanized_delivery_time_html', str_replace( '{delivery_time}', $html, get_option( 'woocommerce_gzd_delivery_time_text' ) ), $html ) : '' ); 
  420.  
  421. public function has_free_shipping() { 
  422. return ( apply_filters( 'woocommerce_germanized_product_has_free_shipping', ( $this->free_shipping === 'yes' ? true : false ), $this ) ); 
  423.  
  424. /** 
  425. * Returns the shipping costs notice html output 
  426. *  
  427. * @return string  
  428. */ 
  429. public function get_shipping_costs_html() { 
  430.  
  431. if ( apply_filters( 'woocommerce_germanized_hide_shipping_costs_text', false, $this ) ) 
  432. return apply_filters( 'woocommerce_germanized_disabled_shipping_text', '', $this ); 
  433.  
  434. $find = array( 
  435. '{link}',  
  436. '{/link}' 
  437. ); 
  438.  
  439. $replace = array( 
  440. '<a href="' . esc_url( get_permalink( wc_get_page_id( 'shipping_costs' ) ) ) . '" target="_blank">',  
  441. '</a>' 
  442. ); 
  443.  
  444. 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 ); 
  445.