WC_Product_Variable

Variable Product Class.

Defined (1)

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

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