Нет описания

class-wc-product-variable.php 22KB

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