Sin descripción

class-wc-product-variation.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. <?php
  2. /**
  3. * Product Variation
  4. *
  5. * The WooCommerce product variation class handles product variation data.
  6. *
  7. * @package WooCommerce\Classes
  8. * @version 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Product variation class.
  13. */
  14. class WC_Product_Variation extends WC_Product_Simple {
  15. /**
  16. * Post type.
  17. *
  18. * @var string
  19. */
  20. protected $post_type = 'product_variation';
  21. /**
  22. * Parent data.
  23. *
  24. * @var array
  25. */
  26. protected $parent_data = array(
  27. 'title' => '',
  28. 'sku' => '',
  29. 'manage_stock' => '',
  30. 'backorders' => '',
  31. 'stock_quantity' => '',
  32. 'weight' => '',
  33. 'length' => '',
  34. 'width' => '',
  35. 'height' => '',
  36. 'tax_class' => '',
  37. 'shipping_class_id' => '',
  38. 'image_id' => '',
  39. 'purchase_note' => '',
  40. );
  41. /**
  42. * Override the default constructor to set custom defaults.
  43. *
  44. * @param int|WC_Product|object $product Product to init.
  45. */
  46. public function __construct( $product = 0 ) {
  47. $this->data['tax_class'] = 'parent';
  48. $this->data['attribute_summary'] = '';
  49. parent::__construct( $product );
  50. }
  51. /**
  52. * Prefix for action and filter hooks on data.
  53. *
  54. * @since 3.0.0
  55. * @return string
  56. */
  57. protected function get_hook_prefix() {
  58. return 'woocommerce_product_variation_get_';
  59. }
  60. /**
  61. * Get internal type.
  62. *
  63. * @return string
  64. */
  65. public function get_type() {
  66. return 'variation';
  67. }
  68. /**
  69. * If the stock level comes from another product ID.
  70. *
  71. * @since 3.0.0
  72. * @return int
  73. */
  74. public function get_stock_managed_by_id() {
  75. return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id();
  76. }
  77. /**
  78. * Get the product's title. For variations this is the parent product name.
  79. *
  80. * @return string
  81. */
  82. public function get_title() {
  83. return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this );
  84. }
  85. /**
  86. * Get product name with SKU or ID. Used within admin.
  87. *
  88. * @return string Formatted product name
  89. */
  90. public function get_formatted_name() {
  91. if ( $this->get_sku() ) {
  92. $identifier = $this->get_sku();
  93. } else {
  94. $identifier = '#' . $this->get_id();
  95. }
  96. $formatted_variation_list = wc_get_formatted_variation( $this, true, true, true );
  97. return sprintf( '%2$s (%1$s)', $identifier, $this->get_name() ) . '<span class="description">' . $formatted_variation_list . '</span>';
  98. }
  99. /**
  100. * Get variation attribute values. Keys are prefixed with attribute_, as stored, unless $with_prefix is false.
  101. *
  102. * @param bool $with_prefix Whether keys should be prepended with attribute_ or not, default is true.
  103. * @return array of attributes and their values for this variation.
  104. */
  105. public function get_variation_attributes( $with_prefix = true ) {
  106. $attributes = $this->get_attributes();
  107. $variation_attributes = array();
  108. $prefix = $with_prefix ? 'attribute_' : '';
  109. foreach ( $attributes as $key => $value ) {
  110. $variation_attributes[ $prefix . $key ] = $value;
  111. }
  112. return $variation_attributes;
  113. }
  114. /**
  115. * Returns a single product attribute as a string.
  116. *
  117. * @param string $attribute to get.
  118. * @return string
  119. */
  120. public function get_attribute( $attribute ) {
  121. $attributes = $this->get_attributes();
  122. $attribute = sanitize_title( $attribute );
  123. if ( isset( $attributes[ $attribute ] ) ) {
  124. $value = $attributes[ $attribute ];
  125. $term = taxonomy_exists( $attribute ) ? get_term_by( 'slug', $value, $attribute ) : false;
  126. return ! is_wp_error( $term ) && $term ? $term->name : $value;
  127. }
  128. $att_str = 'pa_' . $attribute;
  129. if ( isset( $attributes[ $att_str ] ) ) {
  130. $value = $attributes[ $att_str ];
  131. $term = taxonomy_exists( $att_str ) ? get_term_by( 'slug', $value, $att_str ) : false;
  132. return ! is_wp_error( $term ) && $term ? $term->name : $value;
  133. }
  134. return '';
  135. }
  136. /**
  137. * Wrapper for get_permalink. Adds this variations attributes to the URL.
  138. *
  139. * @param array|null $item_object item array If a cart or order item is passed, we can get a link containing the exact attributes selected for the variation, rather than the default attributes.
  140. * @return string
  141. */
  142. public function get_permalink( $item_object = null ) {
  143. $url = get_permalink( $this->get_parent_id() );
  144. if ( ! empty( $item_object['variation'] ) ) {
  145. $data = $item_object['variation'];
  146. } elseif ( ! empty( $item_object['item_meta_array'] ) ) {
  147. $data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) );
  148. $data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' );
  149. $data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_variation_attributes() );
  150. } else {
  151. $data = $this->get_variation_attributes();
  152. }
  153. $data = array_filter( $data, 'wc_array_filter_default_attributes' );
  154. if ( empty( $data ) ) {
  155. return $url;
  156. }
  157. // Filter and encode keys and values so this is not broken by add_query_arg.
  158. $data = array_map( 'urlencode', $data );
  159. $keys = array_map( 'urlencode', array_keys( $data ) );
  160. return add_query_arg( array_combine( $keys, $data ), $url );
  161. }
  162. /**
  163. * Get the add to url used mainly in loops.
  164. *
  165. * @return string
  166. */
  167. public function add_to_cart_url() {
  168. $url = $this->is_purchasable() ? remove_query_arg(
  169. 'added-to-cart',
  170. add_query_arg(
  171. array(
  172. 'variation_id' => $this->get_id(),
  173. 'add-to-cart' => $this->get_parent_id(),
  174. ),
  175. $this->get_permalink()
  176. )
  177. ) : $this->get_permalink();
  178. return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
  179. }
  180. /**
  181. * Get SKU (Stock-keeping unit) - product unique ID.
  182. *
  183. * @param string $context What the value is for. Valid values are view and edit.
  184. * @return string
  185. */
  186. public function get_sku( $context = 'view' ) {
  187. $value = $this->get_prop( 'sku', $context );
  188. // Inherit value from parent.
  189. if ( 'view' === $context && empty( $value ) ) {
  190. $value = apply_filters( $this->get_hook_prefix() . 'sku', $this->parent_data['sku'], $this );
  191. }
  192. return $value;
  193. }
  194. /**
  195. * Returns the product's weight.
  196. *
  197. * @param string $context What the value is for. Valid values are view and edit.
  198. * @return string
  199. */
  200. public function get_weight( $context = 'view' ) {
  201. $value = $this->get_prop( 'weight', $context );
  202. // Inherit value from parent.
  203. if ( 'view' === $context && empty( $value ) ) {
  204. $value = apply_filters( $this->get_hook_prefix() . 'weight', $this->parent_data['weight'], $this );
  205. }
  206. return $value;
  207. }
  208. /**
  209. * Returns the product length.
  210. *
  211. * @param string $context What the value is for. Valid values are view and edit.
  212. * @return string
  213. */
  214. public function get_length( $context = 'view' ) {
  215. $value = $this->get_prop( 'length', $context );
  216. // Inherit value from parent.
  217. if ( 'view' === $context && empty( $value ) ) {
  218. $value = apply_filters( $this->get_hook_prefix() . 'length', $this->parent_data['length'], $this );
  219. }
  220. return $value;
  221. }
  222. /**
  223. * Returns the product width.
  224. *
  225. * @param string $context What the value is for. Valid values are view and edit.
  226. * @return string
  227. */
  228. public function get_width( $context = 'view' ) {
  229. $value = $this->get_prop( 'width', $context );
  230. // Inherit value from parent.
  231. if ( 'view' === $context && empty( $value ) ) {
  232. $value = apply_filters( $this->get_hook_prefix() . 'width', $this->parent_data['width'], $this );
  233. }
  234. return $value;
  235. }
  236. /**
  237. * Returns the product height.
  238. *
  239. * @param string $context What the value is for. Valid values are view and edit.
  240. * @return string
  241. */
  242. public function get_height( $context = 'view' ) {
  243. $value = $this->get_prop( 'height', $context );
  244. // Inherit value from parent.
  245. if ( 'view' === $context && empty( $value ) ) {
  246. $value = apply_filters( $this->get_hook_prefix() . 'height', $this->parent_data['height'], $this );
  247. }
  248. return $value;
  249. }
  250. /**
  251. * Returns the tax class.
  252. *
  253. * Does not use get_prop so it can handle 'parent' inheritance correctly.
  254. *
  255. * @param string $context view, edit, or unfiltered.
  256. * @return string
  257. */
  258. public function get_tax_class( $context = 'view' ) {
  259. $value = null;
  260. if ( array_key_exists( 'tax_class', $this->data ) ) {
  261. $value = array_key_exists( 'tax_class', $this->changes ) ? $this->changes['tax_class'] : $this->data['tax_class'];
  262. if ( 'edit' !== $context && 'parent' === $value ) {
  263. $value = $this->parent_data['tax_class'];
  264. }
  265. if ( 'view' === $context ) {
  266. $value = apply_filters( $this->get_hook_prefix() . 'tax_class', $value, $this );
  267. }
  268. }
  269. return $value;
  270. }
  271. /**
  272. * Return if product manage stock.
  273. *
  274. * @since 3.0.0
  275. * @param string $context What the value is for. Valid values are view and edit.
  276. * @return boolean|string true, false, or parent.
  277. */
  278. public function get_manage_stock( $context = 'view' ) {
  279. $value = $this->get_prop( 'manage_stock', $context );
  280. // Inherit value from parent.
  281. if ( 'view' === $context && false === $value && true === wc_string_to_bool( $this->parent_data['manage_stock'] ) ) {
  282. $value = 'parent';
  283. }
  284. return $value;
  285. }
  286. /**
  287. * Returns number of items available for sale.
  288. *
  289. * @param string $context What the value is for. Valid values are view and edit.
  290. * @return int|null
  291. */
  292. public function get_stock_quantity( $context = 'view' ) {
  293. $value = $this->get_prop( 'stock_quantity', $context );
  294. // Inherit value from parent.
  295. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) {
  296. $value = apply_filters( $this->get_hook_prefix() . 'stock_quantity', $this->parent_data['stock_quantity'], $this );
  297. }
  298. return $value;
  299. }
  300. /**
  301. * Get backorders.
  302. *
  303. * @param string $context What the value is for. Valid values are view and edit.
  304. * @since 3.0.0
  305. * @return string yes no or notify
  306. */
  307. public function get_backorders( $context = 'view' ) {
  308. $value = $this->get_prop( 'backorders', $context );
  309. // Inherit value from parent.
  310. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) {
  311. $value = apply_filters( $this->get_hook_prefix() . 'backorders', $this->parent_data['backorders'], $this );
  312. }
  313. return $value;
  314. }
  315. /**
  316. * Get main image ID.
  317. *
  318. * @since 3.0.0
  319. * @param string $context What the value is for. Valid values are view and edit.
  320. * @return string
  321. */
  322. public function get_image_id( $context = 'view' ) {
  323. $image_id = $this->get_prop( 'image_id', $context );
  324. if ( 'view' === $context && ! $image_id ) {
  325. $image_id = apply_filters( $this->get_hook_prefix() . 'image_id', $this->parent_data['image_id'], $this );
  326. }
  327. return $image_id;
  328. }
  329. /**
  330. * Get purchase note.
  331. *
  332. * @since 3.0.0
  333. * @param string $context What the value is for. Valid values are view and edit.
  334. * @return string
  335. */
  336. public function get_purchase_note( $context = 'view' ) {
  337. $value = $this->get_prop( 'purchase_note', $context );
  338. // Inherit value from parent.
  339. if ( 'view' === $context && empty( $value ) ) {
  340. $value = apply_filters( $this->get_hook_prefix() . 'purchase_note', $this->parent_data['purchase_note'], $this );
  341. }
  342. return $value;
  343. }
  344. /**
  345. * Get shipping class ID.
  346. *
  347. * @since 3.0.0
  348. * @param string $context What the value is for. Valid values are view and edit.
  349. * @return int
  350. */
  351. public function get_shipping_class_id( $context = 'view' ) {
  352. $shipping_class_id = $this->get_prop( 'shipping_class_id', $context );
  353. if ( 'view' === $context && ! $shipping_class_id ) {
  354. $shipping_class_id = apply_filters( $this->get_hook_prefix() . 'shipping_class_id', $this->parent_data['shipping_class_id'], $this );
  355. }
  356. return $shipping_class_id;
  357. }
  358. /**
  359. * Get catalog visibility.
  360. *
  361. * @param string $context What the value is for. Valid values are view and edit.
  362. * @return string
  363. */
  364. public function get_catalog_visibility( $context = 'view' ) {
  365. return apply_filters( $this->get_hook_prefix() . 'catalog_visibility', $this->parent_data['catalog_visibility'], $this );
  366. }
  367. /**
  368. * Get attribute summary.
  369. *
  370. * By default, attribute summary contains comma-delimited 'attribute_name: attribute_value' pairs for all attributes.
  371. *
  372. * @param string $context What the value is for. Valid values are view and edit.
  373. *
  374. * @since 3.6.0
  375. * @return string
  376. */
  377. public function get_attribute_summary( $context = 'view' ) {
  378. return $this->get_prop( 'attribute_summary', $context );
  379. }
  380. /**
  381. * Set attribute summary.
  382. *
  383. * By default, attribute summary contains comma-delimited 'attribute_name: attribute_value' pairs for all attributes.
  384. *
  385. * @since 3.6.0
  386. * @param string $attribute_summary Summary of attribute names and values assigned to the variation.
  387. */
  388. public function set_attribute_summary( $attribute_summary ) {
  389. $this->set_prop( 'attribute_summary', $attribute_summary );
  390. }
  391. /*
  392. |--------------------------------------------------------------------------
  393. | CRUD methods
  394. |--------------------------------------------------------------------------
  395. */
  396. /**
  397. * Set the parent data array for this variation.
  398. *
  399. * @since 3.0.0
  400. * @param array $parent_data parent data array for this variation.
  401. */
  402. public function set_parent_data( $parent_data ) {
  403. $parent_data = wp_parse_args(
  404. $parent_data,
  405. array(
  406. 'title' => '',
  407. 'status' => '',
  408. 'sku' => '',
  409. 'manage_stock' => 'no',
  410. 'backorders' => 'no',
  411. 'stock_quantity' => '',
  412. 'weight' => '',
  413. 'length' => '',
  414. 'width' => '',
  415. 'height' => '',
  416. 'tax_class' => '',
  417. 'shipping_class_id' => 0,
  418. 'image_id' => 0,
  419. 'purchase_note' => '',
  420. 'catalog_visibility' => 'visible',
  421. )
  422. );
  423. // Normalize tax class.
  424. $parent_data['tax_class'] = sanitize_title( $parent_data['tax_class'] );
  425. $parent_data['tax_class'] = 'standard' === $parent_data['tax_class'] ? '' : $parent_data['tax_class'];
  426. $valid_classes = $this->get_valid_tax_classes();
  427. if ( ! in_array( $parent_data['tax_class'], $valid_classes, true ) ) {
  428. $parent_data['tax_class'] = '';
  429. }
  430. $this->parent_data = $parent_data;
  431. }
  432. /**
  433. * Get the parent data array for this variation.
  434. *
  435. * @since 3.0.0
  436. * @return array
  437. */
  438. public function get_parent_data() {
  439. return $this->parent_data;
  440. }
  441. /**
  442. * Set attributes. Unlike the parent product which uses terms, variations are assigned
  443. * specific attributes using name value pairs.
  444. *
  445. * @param array $raw_attributes array of raw attributes.
  446. */
  447. public function set_attributes( $raw_attributes ) {
  448. $raw_attributes = (array) $raw_attributes;
  449. $attributes = array();
  450. foreach ( $raw_attributes as $key => $value ) {
  451. // Remove attribute prefix which meta gets stored with.
  452. if ( 0 === strpos( $key, 'attribute_' ) ) {
  453. $key = substr( $key, 10 );
  454. }
  455. $attributes[ $key ] = $value;
  456. }
  457. $this->set_prop( 'attributes', $attributes );
  458. }
  459. /**
  460. * Returns whether or not the product has any visible attributes.
  461. *
  462. * Variations are mapped to specific attributes unlike products, and the return
  463. * value of ->get_attributes differs. Therefore this returns false.
  464. *
  465. * @return boolean
  466. */
  467. public function has_attributes() {
  468. return false;
  469. }
  470. /*
  471. |--------------------------------------------------------------------------
  472. | Conditionals
  473. |--------------------------------------------------------------------------
  474. */
  475. /**
  476. * Returns false if the product cannot be bought.
  477. * Override abstract method so that: i) Disabled variations are not be purchasable by admins. ii) Enabled variations are not purchasable if the parent product is not purchasable.
  478. *
  479. * @return bool
  480. */
  481. public function is_purchasable() {
  482. return apply_filters( 'woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable() && ( 'publish' === $this->parent_data['status'] || current_user_can( 'edit_post', $this->get_parent_id() ) ), $this );
  483. }
  484. /**
  485. * Controls whether this particular variation will appear greyed-out (inactive) or not (active).
  486. * Used by extensions to make incompatible variations appear greyed-out, etc.
  487. * Other possible uses: prevent out-of-stock variations from being selected.
  488. *
  489. * @return bool
  490. */
  491. public function variation_is_active() {
  492. return apply_filters( 'woocommerce_variation_is_active', true, $this );
  493. }
  494. /**
  495. * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed.
  496. * Instead, a suitable 'unavailable' message is displayed.
  497. * Invisible by default: Disabled variations and variations with an empty price.
  498. *
  499. * @return bool
  500. */
  501. public function variation_is_visible() {
  502. return apply_filters( 'woocommerce_variation_is_visible', 'publish' === get_post_status( $this->get_id() ) && '' !== $this->get_price(), $this->get_id(), $this->get_parent_id(), $this );
  503. }
  504. /**
  505. * Return valid tax classes. Adds 'parent' to the default list of valid tax classes.
  506. *
  507. * @return array valid tax classes
  508. */
  509. protected function get_valid_tax_classes() {
  510. $valid_classes = WC_Tax::get_tax_class_slugs();
  511. $valid_classes[] = 'parent';
  512. return $valid_classes;
  513. }
  514. }