/includes/class-wc-product-variable.php

  1. <?php 
  2. if ( ! defined( 'ABSPATH' ) ) { 
  3. exit; 
  4.  
  5. /** 
  6. * Variable Product Class. 
  7. * 
  8. * The WooCommerce product class handles individual product data. 
  9. * 
  10. * @version 3.0.0 
  11. * @package WooCommerce/Classes/Products 
  12. * @category Class 
  13. * @author WooThemes 
  14. */ 
  15. class WC_Product_Variable extends WC_Product { 
  16.  
  17. /** 
  18. * Array of children variation IDs. Determined by children. 
  19. * 
  20. * @var array 
  21. */ 
  22. protected $children = array(); 
  23.  
  24. /** 
  25. * Array of visible children variation IDs. Determined by children. 
  26. * 
  27. * @var array 
  28. */ 
  29. protected $visible_children = array(); 
  30.  
  31. /** 
  32. * Array of variation attributes IDs. Determined by children. 
  33. * 
  34. * @var array 
  35. */ 
  36. protected $variation_attributes = array(); 
  37.  
  38. /** 
  39. * Get internal type. 
  40. * 
  41. * @return string 
  42. */ 
  43. public function get_type() { 
  44. return 'variable'; 
  45.  
  46. /** 
  47. |-------------------------------------------------------------------------- 
  48. | Getters 
  49. |-------------------------------------------------------------------------- 
  50. */ 
  51.  
  52. /** 
  53. * Get the add to cart button text. 
  54. * 
  55. * @return string 
  56. */ 
  57. public function add_to_cart_text() { 
  58. return apply_filters( 'woocommerce_product_add_to_cart_text', $this->is_purchasable() ? __( 'Select options', 'woocommerce' ) : __( 'Read more', 'woocommerce' ), $this ); 
  59.  
  60. /** 
  61. * Get an array of all sale and regular 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. 
  62. * 
  63. * @param bool $include_taxes Should taxes be included in the prices. 
  64. * @return array() Array of RAW prices, regular prices, and sale prices with keys set to variation ID. 
  65. */ 
  66. public function get_variation_prices( $include_taxes = false ) { 
  67. $prices = $this->data_store->read_price_data( $this, $include_taxes ); 
  68.  
  69. foreach ( $prices as $price_key => $variation_prices ) { 
  70. $prices[ $price_key ] = $this->sort_variation_prices( $variation_prices ); 
  71.  
  72. return $prices; 
  73.  
  74. /** 
  75. * Get the min or max variation regular price. 
  76. * 
  77. * @param string $min_or_max Min or max price. 
  78. * @param boolean $include_taxes Should the price include taxes? 
  79. * @return string 
  80. */ 
  81. public function get_variation_regular_price( $min_or_max = 'min', $include_taxes = false ) { 
  82. $prices = $this->get_variation_prices( $include_taxes ); 
  83. $price = 'min' === $min_or_max ? current( $prices['regular_price'] ) : end( $prices['regular_price'] ); 
  84. return apply_filters( 'woocommerce_get_variation_regular_price', $price, $this, $min_or_max, $include_taxes ); 
  85.  
  86. /** 
  87. * Get the min or max variation sale price. 
  88. * 
  89. * @param string $min_or_max Min or max price. 
  90. * @param boolean $include_taxes Should the price include taxes? 
  91. * @return string 
  92. */ 
  93. public function get_variation_sale_price( $min_or_max = 'min', $include_taxes = false ) { 
  94. $prices = $this->get_variation_prices( $include_taxes ); 
  95. $price = 'min' === $min_or_max ? current( $prices['sale_price'] ) : end( $prices['sale_price'] ); 
  96. return apply_filters( 'woocommerce_get_variation_sale_price', $price, $this, $min_or_max, $include_taxes ); 
  97.  
  98. /** 
  99. * Get the min or max variation (active) price. 
  100. * 
  101. * @param string $min_or_max Min or max price. 
  102. * @param boolean $include_taxes Should the price include taxes? 
  103. * @return string 
  104. */ 
  105. public function get_variation_price( $min_or_max = 'min', $include_taxes = false ) { 
  106. $prices = $this->get_variation_prices( $include_taxes ); 
  107. $price = 'min' === $min_or_max ? current( $prices['price'] ) : end( $prices['price'] ); 
  108. return apply_filters( 'woocommerce_get_variation_price', $price, $this, $min_or_max, $include_taxes ); 
  109.  
  110. /** 
  111. * Returns the price in html format. 
  112. * 
  113. * Note: Variable prices do not show suffixes like other product types. This 
  114. * is due to some things like tax classes being set at variation level which 
  115. * could differ from the parent price. The only way to show accurate prices 
  116. * would be to load the variation and get IT's price, which adds extra 
  117. * overhead and still has edge cases where the values would be inaccurate. 
  118. * 
  119. * Additionally, ranges of prices no longer show 'striked out' sale prices 
  120. * due to the strings being very long and unclear/confusing. A single range 
  121. * is shown instead. 
  122. * 
  123. * @param string $price (default: '') 
  124. * @return string 
  125. */ 
  126. public function get_price_html( $price = '' ) { 
  127. $prices = $this->get_variation_prices( true ); 
  128.  
  129. if ( empty( $prices['price'] ) ) { 
  130. $price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this ); 
  131. } else { 
  132. $min_price = current( $prices['price'] ); 
  133. $max_price = end( $prices['price'] ); 
  134. $min_reg_price = current( $prices['regular_price'] ); 
  135. $max_reg_price = end( $prices['regular_price'] ); 
  136.  
  137. if ( $min_price !== $max_price ) { 
  138. $price = apply_filters( 'woocommerce_variable_price_html', wc_format_price_range( $min_price, $max_price ) . $this->get_price_suffix(), $this ); 
  139. } elseif ( $this->is_on_sale() && $min_reg_price === $max_reg_price ) { 
  140. $price = apply_filters( 'woocommerce_variable_price_html', wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) ) . $this->get_price_suffix(), $this ); 
  141. } else { 
  142. $price = apply_filters( 'woocommerce_variable_price_html', wc_price( $min_price ) . $this->get_price_suffix(), $this ); 
  143.  
  144. return apply_filters( 'woocommerce_get_price_html', $price, $this ); 
  145.  
  146. /** 
  147. * Get the suffix to display after prices > 0. 
  148. * 
  149. * This is skipped if the suffix 
  150. * has dynamic values such as {price_excluding_tax} for variable products. 
  151. * @see get_price_html for an explanation as to why. 
  152. * 
  153. * @param string $price to calculate, left blank to just use get_price() 
  154. * @param integer $qty passed on to get_price_including_tax() or get_price_excluding_tax() 
  155. * @return string 
  156. */ 
  157. public function get_price_suffix( $price = '', $qty = 1 ) { 
  158. $suffix = get_option( 'woocommerce_price_display_suffix' ); 
  159.  
  160. if ( strstr( $suffix, '{' ) ) { 
  161. return apply_filters( 'woocommerce_get_price_suffix', '', $this, $price, $qty ); 
  162. } else { 
  163. return parent::get_price_suffix( $price, $qty ); 
  164.  
  165. /** 
  166. * Return a products child ids. 
  167. * 
  168. * @return array Children ids 
  169. */ 
  170. public function get_children( $visible_only = '' ) { 
  171. if ( is_bool( $visible_only ) ) { 
  172. wc_deprecated_argument( 'visible_only', '3.0', 'WC_Product_Variable::get_visible_children' ); 
  173. return $visible_only ? $this->get_visible_children() : $this->get_children(); 
  174. return apply_filters( 'woocommerce_get_children', $this->children, $this, false ); 
  175.  
  176. /** 
  177. * Return a products child ids - visible only. 
  178. * 
  179. * @since 3.0.0 
  180. * @return array Children ids 
  181. */ 
  182. public function get_visible_children() { 
  183. return apply_filters( 'woocommerce_get_children', $this->visible_children, $this, true ); 
  184.  
  185. /** 
  186. * Return an array of attributes used for variations, as well as their possible values. 
  187. * 
  188. * @return array Attributes and their available values 
  189. */ 
  190. public function get_variation_attributes() { 
  191. return $this->variation_attributes; 
  192.  
  193. /** 
  194. * If set, get the default attributes for a variable product. 
  195. * 
  196. * @param string $attribute_name 
  197. * @return string 
  198. */ 
  199. public function get_variation_default_attribute( $attribute_name ) { 
  200. $defaults = $this->get_default_attributes(); 
  201. $attribute_name = sanitize_title( $attribute_name ); 
  202. return isset( $defaults[ $attribute_name ] ) ? $defaults[ $attribute_name ] : ''; 
  203.  
  204. /** 
  205. * Variable products themselves cannot be downloadable. 
  206. */ 
  207. public function get_downloadable( $context = 'view' ) { 
  208. return false; 
  209.  
  210. /** 
  211. * Variable products themselves cannot be virtual. 
  212. */ 
  213. public function get_virtual( $context = 'view' ) { 
  214. return false; 
  215.  
  216. /** 
  217. * Get an array of available variations for the current product. 
  218. * 
  219. * @return array 
  220. */ 
  221. public function get_available_variations() { 
  222. $available_variations = array(); 
  223.  
  224. foreach ( $this->get_children() as $child_id ) { 
  225. $variation = wc_get_product( $child_id ); 
  226.  
  227. // Hide out of stock variations if 'Hide out of stock items from the catalog' is checked 
  228. if ( ! $variation || ! $variation->exists() || ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ! $variation->is_in_stock() ) ) { 
  229. continue; 
  230.  
  231. // Filter 'woocommerce_hide_invisible_variations' to optionally hide invisible variations (disabled variations and variations with empty price) 
  232. if ( apply_filters( 'woocommerce_hide_invisible_variations', false, $this->get_id(), $variation ) && ! $variation->variation_is_visible() ) { 
  233. continue; 
  234.  
  235. $available_variations[] = $this->get_available_variation( $variation ); 
  236.  
  237. return $available_variations; 
  238.  
  239. /** 
  240. * Returns an array of data for a variation. Used in the add to cart form. 
  241. * @since 2.4.0 
  242. * @param WC_Product $variation Variation product object or ID 
  243. * @return array 
  244. */ 
  245. public function get_available_variation( $variation ) { 
  246. if ( is_numeric( $variation ) ) { 
  247. $variation = wc_get_product( $variation ); 
  248.  
  249. // See if prices should be shown for each variation after selection. 
  250. $show_variation_price = apply_filters( 'woocommerce_show_variation_price', $variation->get_price() === "" || $this->get_variation_sale_price( 'min' ) !== $this->get_variation_sale_price( 'max' ) || $this->get_variation_regular_price( 'min' ) !== $this->get_variation_regular_price( 'max' ), $this, $variation ); 
  251.  
  252. return apply_filters( 'woocommerce_available_variation', array_merge( $variation->get_data(), array( 
  253. 'attributes' => $variation->get_variation_attributes(),  
  254. 'image' => wc_get_product_attachment_props( $variation->get_image_id() ),  
  255. 'weight_html' => wc_format_weight( $variation->get_weight() ),  
  256. 'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),  
  257. 'price_html' => $show_variation_price ? '<span class="price">' . $variation->get_price_html() . '</span>' : '',  
  258. 'availability_html' => wc_get_stock_html( $variation ),  
  259. 'variation_id' => $variation->get_id(),  
  260. 'variation_is_visible' => $variation->variation_is_visible(),  
  261. 'variation_is_active' => $variation->variation_is_active(),  
  262. 'is_purchasable' => $variation->is_purchasable(),  
  263. 'display_price' => wc_get_price_to_display( $variation ),  
  264. 'display_regular_price' => wc_get_price_to_display( $variation, array( 'price' => $variation->get_regular_price() ) ),  
  265. 'dimensions' => wc_format_dimensions( $variation->get_dimensions( false ) ),  
  266. 'min_qty' => $variation->get_min_purchase_quantity(),  
  267. 'max_qty' => 0 < $variation->get_max_purchase_quantity() ? $variation->get_max_purchase_quantity() : '',  
  268. 'backorders_allowed' => $variation->backorders_allowed(),  
  269. 'is_in_stock' => $variation->is_in_stock(),  
  270. 'is_downloadable' => $variation->is_downloadable(),  
  271. 'is_virtual' => $variation->is_virtual(),  
  272. 'is_sold_individually' => $variation->is_sold_individually() ? 'yes' : 'no',  
  273. 'variation_description' => wc_format_content( $variation->get_description() ),  
  274. ) ), $this, $variation ); 
  275.  
  276. /** 
  277. |-------------------------------------------------------------------------- 
  278. | Setters 
  279. |-------------------------------------------------------------------------- 
  280. */ 
  281.  
  282. /** 
  283. * Sets an array of variation attributes. 
  284. * 
  285. * @since 3.0.0 
  286. * @param array 
  287. */ 
  288. public function set_variation_attributes( $variation_attributes ) { 
  289. $this->variation_attributes = $variation_attributes; 
  290.  
  291. /** 
  292. * Sets an array of children for the product. 
  293. * 
  294. * @since 3.0.0 
  295. * @param array 
  296. */ 
  297. public function set_children( $children ) { 
  298. $this->children = array_filter( wp_parse_id_list( (array) $children ) ); 
  299.  
  300. /** 
  301. * Sets an array of visible children only. 
  302. * 
  303. * @since 3.0.0 
  304. * @param array 
  305. */ 
  306. public function set_visible_children( $visible_children ) { 
  307. $this->visible_children = array_filter( wp_parse_id_list( (array) $visible_children ) ); 
  308.  
  309. /** 
  310. |-------------------------------------------------------------------------- 
  311. | CRUD methods 
  312. |-------------------------------------------------------------------------- 
  313. */ 
  314.  
  315. /** 
  316. * Ensure properties are set correctly before save. 
  317. * @since 3.0.0 
  318. */ 
  319. public function validate_props() { 
  320. // Before updating, ensure stock props are all aligned. Qty and backorders are not needed if not stock managed. 
  321. if ( ! $this->get_manage_stock() ) { 
  322. $this->set_stock_quantity( '' ); 
  323. $this->set_backorders( 'no' ); 
  324. $this->set_stock_status( $this->child_is_in_stock() ? 'instock' : 'outofstock' ); 
  325.  
  326. // If we are stock managing and we don't have stock, force out of stock status. 
  327. } elseif ( $this->get_stock_quantity() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { 
  328. $this->set_stock_status( 'outofstock' ); 
  329.  
  330. // If the stock level is changing and we do now have enough, force in stock status. 
  331. } elseif ( $this->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' ) && array_key_exists( 'stock_quantity', $this->get_changes() ) ) { 
  332. $this->set_stock_status( 'instock' ); 
  333.  
  334. // Otherwise revert to status the children have. 
  335. } else { 
  336. $this->set_stock_status( $this->child_is_in_stock() ? 'instock' : 'outofstock' ); 
  337.  
  338. /** 
  339. * Save data (either create or update depending on if we are working on an existing product). 
  340. * 
  341. * @since 3.0.0 
  342. */ 
  343. public function save() { 
  344. $this->validate_props(); 
  345. if ( $this->data_store ) { 
  346. // Trigger action before saving to the DB. Allows you to adjust object props before save. 
  347. do_action( 'woocommerce_before_' . $this->object_type . '_object_save', $this, $this->data_store ); 
  348.  
  349. // Get names before save. 
  350. $previous_name = $this->data['name']; 
  351. $new_name = $this->get_name( 'edit' ); 
  352.  
  353. if ( $this->get_id() ) { 
  354. $this->data_store->update( $this ); 
  355. } else { 
  356. $this->data_store->create( $this ); 
  357.  
  358. $this->data_store->sync_variation_names( $this, $previous_name, $new_name ); 
  359. $this->data_store->sync_managed_variation_stock_status( $this ); 
  360.  
  361. return $this->get_id(); 
  362.  
  363. /** 
  364. |-------------------------------------------------------------------------- 
  365. | Conditionals 
  366. |-------------------------------------------------------------------------- 
  367. */ 
  368.  
  369. /** 
  370. * Returns whether or not the product is on sale. 
  371. * 
  372. * @param string $context What the value is for. Valid values are view and edit. 
  373. * @return bool 
  374. */ 
  375. public function is_on_sale( $context = 'view' ) { 
  376. $prices = $this->get_variation_prices(); 
  377. $on_sale = $prices['regular_price'] !== $prices['sale_price'] && $prices['sale_price'] === $prices['price']; 
  378.  
  379. return 'view' === $context ? apply_filters( 'woocommerce_product_is_on_sale', $on_sale, $this ) : $on_sale; 
  380.  
  381. /** 
  382. * Is a child in stock? 
  383. * @return boolean 
  384. */ 
  385. public function child_is_in_stock() { 
  386. return $this->data_store->child_is_in_stock( $this ); 
  387.  
  388. /** 
  389. * Does a child have a weight set? 
  390. * @return boolean 
  391. */ 
  392. public function child_has_weight() { 
  393. $transient_name = 'wc_child_has_weight_' . $this->get_id(); 
  394. $has_weight = get_transient( $transient_name ); 
  395.  
  396. if ( false === $has_weight ) { 
  397. $has_weight = $this->data_store->child_has_weight( $this ); 
  398. set_transient( $transient_name, $has_weight, DAY_IN_SECONDS * 30 ); 
  399. return (bool) $has_weight; 
  400.  
  401. /** 
  402. * Does a child have dimensions set? 
  403. * @return boolean 
  404. */ 
  405. public function child_has_dimensions() { 
  406. $transient_name = 'wc_child_has_dimensions_' . $this->get_id(); 
  407. $has_dimension = get_transient( $transient_name ); 
  408.  
  409. if ( false === $has_dimension ) { 
  410. $has_dimension = $this->data_store->child_has_dimensions( $this ); 
  411. set_transient( $transient_name, $has_dimension, DAY_IN_SECONDS * 30 ); 
  412. return (bool) $has_dimension; 
  413.  
  414. /** 
  415. * Returns whether or not the product has dimensions set. 
  416. * 
  417. * @return bool 
  418. */ 
  419. public function has_dimensions() { 
  420. return parent::has_dimensions() || $this->child_has_dimensions(); 
  421.  
  422. /** 
  423. * Returns whether or not the product has weight set. 
  424. * 
  425. * @return bool 
  426. */ 
  427. public function has_weight() { 
  428. return parent::has_weight() || $this->child_has_weight(); 
  429.  
  430. /** 
  431. * Returns whether or not the product has additonal options that need 
  432. * selecting before adding to cart. 
  433. * 
  434. * @since 3.0.0 
  435. * @return boolean 
  436. */ 
  437. public function has_options() { 
  438. return true; 
  439.  
  440. /** 
  441. |-------------------------------------------------------------------------- 
  442. | Sync with child variations. 
  443. |-------------------------------------------------------------------------- 
  444. */ 
  445.  
  446. /** 
  447. * Sync a variable product with it's children. These sync functions sync 
  448. * upwards (from child to parent) when the variation is saved. 
  449. * 
  450. * @param WC_Product|int $product Product object or ID for which you wish to sync. 
  451. * @param bool $save If true, the prouduct object will be saved to the DB before returning it. 
  452. * @return WC_Product Synced product object. 
  453. */ 
  454. public static function sync( $product, $save = true ) { 
  455. if ( ! is_a( $product, 'WC_Product' ) ) { 
  456. $product = wc_get_product( $product ); 
  457. if ( is_a( $product, 'WC_Product_Variable' ) ) { 
  458. $data_store = WC_Data_Store::load( 'product-' . $product->get_type() ); 
  459. $data_store->sync_price( $product ); 
  460. $data_store->sync_stock_status( $product ); 
  461. self::sync_attributes( $product ); // Legacy update of attributes. 
  462.  
  463. do_action( 'woocommerce_variable_product_sync_data', $product ); 
  464.  
  465. if ( $save ) { 
  466. $product->save(); 
  467.  
  468. wc_do_deprecated_action( 'woocommerce_variable_product_sync', array( $product->get_id(), $product->get_visible_children() ), '3.0', 'woocommerce_variable_product_sync_data, woocommerce_new_product or woocommerce_update_product' ); 
  469. return $product; 
  470.  
  471. /** 
  472. * Sync parent stock status with the status of all children and save. 
  473. * 
  474. * @param WC_Product|int $product Product object or ID for which you wish to sync. 
  475. * @param bool $save If true, the prouduct object will be saved to the DB before returning it. 
  476. * @return WC_Product Synced product object. 
  477. */ 
  478. public static function sync_stock_status( $product, $save = true ) { 
  479. if ( ! is_a( $product, 'WC_Product' ) ) { 
  480. $product = wc_get_product( $product ); 
  481. if ( is_a( $product, 'WC_Product_Variable' ) ) { 
  482. $data_store = WC_Data_Store::load( 'product-' . $product->get_type() ); 
  483. $data_store->sync_stock_status( $product ); 
  484.  
  485. if ( $save ) { 
  486. $product->save(); 
  487. return $product; 
  488.  
  489. /** 
  490. * Sort an associativate array of $variation_id => $price pairs in order of min and max prices. 
  491. * 
  492. * @param array $prices Associativate array of $variation_id => $price pairs 
  493. * @return array 
  494. */ 
  495. protected function sort_variation_prices( $prices ) { 
  496. asort( $prices ); 
  497. return $prices; 
.