class-wc-product-variation.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. parent::__construct( $product );
  49. }
  50. /**
  51. * Prefix for action and filter hooks on data.
  52. *
  53. * @since 3.0.0
  54. * @return string
  55. */
  56. protected function get_hook_prefix() {
  57. return 'woocommerce_product_variation_get_';
  58. }
  59. /**
  60. * Get internal type.
  61. *
  62. * @return string
  63. */
  64. public function get_type() {
  65. return 'variation';
  66. }
  67. /**
  68. * If the stock level comes from another product ID.
  69. *
  70. * @since 3.0.0
  71. * @return int
  72. */
  73. public function get_stock_managed_by_id() {
  74. return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id();
  75. }
  76. /**
  77. * Get the product's title. For variations this is the parent product name.
  78. *
  79. * @return string
  80. */
  81. public function get_title() {
  82. return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this );
  83. }
  84. /**
  85. * Get product name with SKU or ID. Used within admin.
  86. *
  87. * @return string Formatted product name
  88. */
  89. public function get_formatted_name() {
  90. if ( $this->get_sku() ) {
  91. $identifier = $this->get_sku();
  92. } else {
  93. $identifier = '#' . $this->get_id();
  94. }
  95. $formatted_variation_list = wc_get_formatted_variation( $this, true, true, true );
  96. return sprintf( '%2$s (%1$s)', $identifier, $this->get_name() ) . '<span class="description">' . $formatted_variation_list . '</span>';
  97. }
  98. /**
  99. * Get variation attribute values. Keys are prefixed with attribute_, as stored.
  100. *
  101. * @return array of attributes and their values for this variation
  102. */
  103. public function get_variation_attributes() {
  104. $attributes = $this->get_attributes();
  105. $variation_attributes = array();
  106. foreach ( $attributes as $key => $value ) {
  107. $variation_attributes[ 'attribute_' . $key ] = $value;
  108. }
  109. return $variation_attributes;
  110. }
  111. /**
  112. * Returns a single product attribute as a string.
  113. *
  114. * @param string $attribute to get.
  115. * @return string
  116. */
  117. public function get_attribute( $attribute ) {
  118. $attributes = $this->get_attributes();
  119. $attribute = sanitize_title( $attribute );
  120. if ( isset( $attributes[ $attribute ] ) ) {
  121. $value = $attributes[ $attribute ];
  122. $term = taxonomy_exists( $attribute ) ? get_term_by( 'slug', $value, $attribute ) : false;
  123. return ! is_wp_error( $term ) && $term ? $term->name : $value;
  124. }
  125. $att_str = 'pa_' . $attribute;
  126. if ( isset( $attributes[ $att_str ] ) ) {
  127. $value = $attributes[ $att_str ];
  128. $term = taxonomy_exists( $att_str ) ? get_term_by( 'slug', $value, $att_str ) : false;
  129. return ! is_wp_error( $term ) && $term ? $term->name : $value;
  130. }
  131. return '';
  132. }
  133. /**
  134. * Wrapper for get_permalink. Adds this variations attributes to the URL.
  135. *
  136. * @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.
  137. * @return string
  138. */
  139. public function get_permalink( $item_object = null ) {
  140. $url = get_permalink( $this->get_parent_id() );
  141. if ( ! empty( $item_object['variation'] ) ) {
  142. $data = $item_object['variation'];
  143. } elseif ( ! empty( $item_object['item_meta_array'] ) ) {
  144. $data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) );
  145. $data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' );
  146. $data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_variation_attributes() );
  147. } else {
  148. $data = $this->get_variation_attributes();
  149. }
  150. $data = array_filter( $data );
  151. if ( empty( $data ) ) {
  152. return $url;
  153. }
  154. // Filter and encode keys and values so this is not broken by add_query_arg.
  155. $data = array_map( 'urlencode', $data );
  156. $keys = array_map( 'urlencode', array_keys( $data ) );
  157. return add_query_arg( array_combine( $keys, $data ), $url );
  158. }
  159. /**
  160. * Get the add to url used mainly in loops.
  161. *
  162. * @return string
  163. */
  164. public function add_to_cart_url() {
  165. $url = $this->is_purchasable() ? remove_query_arg(
  166. 'added-to-cart', add_query_arg(
  167. array(
  168. 'variation_id' => $this->get_id(),
  169. 'add-to-cart' => $this->get_parent_id(),
  170. ), $this->get_permalink()
  171. )
  172. ) : $this->get_permalink();
  173. return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
  174. }
  175. /**
  176. * Get SKU (Stock-keeping unit) - product unique ID.
  177. *
  178. * @param string $context What the value is for. Valid values are view and edit.
  179. * @return string
  180. */
  181. public function get_sku( $context = 'view' ) {
  182. $value = $this->get_prop( 'sku', $context );
  183. // Inherit value from parent.
  184. if ( 'view' === $context && empty( $value ) ) {
  185. $value = apply_filters( $this->get_hook_prefix() . 'sku', $this->parent_data['sku'], $this );
  186. }
  187. return $value;
  188. }
  189. /**
  190. * Returns the product's weight.
  191. *
  192. * @param string $context What the value is for. Valid values are view and edit.
  193. * @return string
  194. */
  195. public function get_weight( $context = 'view' ) {
  196. $value = $this->get_prop( 'weight', $context );
  197. // Inherit value from parent.
  198. if ( 'view' === $context && empty( $value ) ) {
  199. $value = apply_filters( $this->get_hook_prefix() . 'weight', $this->parent_data['weight'], $this );
  200. }
  201. return $value;
  202. }
  203. /**
  204. * Returns the product length.
  205. *
  206. * @param string $context What the value is for. Valid values are view and edit.
  207. * @return string
  208. */
  209. public function get_length( $context = 'view' ) {
  210. $value = $this->get_prop( 'length', $context );
  211. // Inherit value from parent.
  212. if ( 'view' === $context && empty( $value ) ) {
  213. $value = apply_filters( $this->get_hook_prefix() . 'length', $this->parent_data['length'], $this );
  214. }
  215. return $value;
  216. }
  217. /**
  218. * Returns the product width.
  219. *
  220. * @param string $context What the value is for. Valid values are view and edit.
  221. * @return string
  222. */
  223. public function get_width( $context = 'view' ) {
  224. $value = $this->get_prop( 'width', $context );
  225. // Inherit value from parent.
  226. if ( 'view' === $context && empty( $value ) ) {
  227. $value = apply_filters( $this->get_hook_prefix() . 'width', $this->parent_data['width'], $this );
  228. }
  229. return $value;
  230. }
  231. /**
  232. * Returns the product height.
  233. *
  234. * @param string $context What the value is for. Valid values are view and edit.
  235. * @return string
  236. */
  237. public function get_height( $context = 'view' ) {
  238. $value = $this->get_prop( 'height', $context );
  239. // Inherit value from parent.
  240. if ( 'view' === $context && empty( $value ) ) {
  241. $value = apply_filters( $this->get_hook_prefix() . 'height', $this->parent_data['height'], $this );
  242. }
  243. return $value;
  244. }
  245. /**
  246. * Returns the tax class.
  247. *
  248. * Does not use get_prop so it can handle 'parent' Inheritance correctly.
  249. *
  250. * @param string $context view, edit, or unfiltered.
  251. * @return string
  252. */
  253. public function get_tax_class( $context = 'view' ) {
  254. $value = null;
  255. if ( array_key_exists( 'tax_class', $this->data ) ) {
  256. $value = array_key_exists( 'tax_class', $this->changes ) ? $this->changes['tax_class'] : $this->data['tax_class'];
  257. if ( 'edit' !== $context && 'parent' === $value ) {
  258. $value = $this->parent_data['tax_class'];
  259. }
  260. if ( 'view' === $context ) {
  261. $value = apply_filters( $this->get_hook_prefix() . 'tax_class', $value, $this );
  262. }
  263. }
  264. return $value;
  265. }
  266. /**
  267. * Return if product manage stock.
  268. *
  269. * @since 3.0.0
  270. * @param string $context What the value is for. Valid values are view and edit.
  271. * @return boolean|string true, false, or parent.
  272. */
  273. public function get_manage_stock( $context = 'view' ) {
  274. $value = $this->get_prop( 'manage_stock', $context );
  275. // Inherit value from parent.
  276. if ( 'view' === $context && false === $value && true === wc_string_to_bool( $this->parent_data['manage_stock'] ) ) {
  277. $value = 'parent';
  278. }
  279. return $value;
  280. }
  281. /**
  282. * Returns number of items available for sale.
  283. *
  284. * @param string $context What the value is for. Valid values are view and edit.
  285. * @return int|null
  286. */
  287. public function get_stock_quantity( $context = 'view' ) {
  288. $value = $this->get_prop( 'stock_quantity', $context );
  289. // Inherit value from parent.
  290. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) {
  291. $value = apply_filters( $this->get_hook_prefix() . 'stock_quantity', $this->parent_data['stock_quantity'], $this );
  292. }
  293. return $value;
  294. }
  295. /**
  296. * Get backorders.
  297. *
  298. * @param string $context What the value is for. Valid values are view and edit.
  299. * @since 3.0.0
  300. * @return string yes no or notify
  301. */
  302. public function get_backorders( $context = 'view' ) {
  303. $value = $this->get_prop( 'backorders', $context );
  304. // Inherit value from parent.
  305. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) {
  306. $value = apply_filters( $this->get_hook_prefix() . 'backorders', $this->parent_data['backorders'], $this );
  307. }
  308. return $value;
  309. }
  310. /**
  311. * Get main image ID.
  312. *
  313. * @since 3.0.0
  314. * @param string $context What the value is for. Valid values are view and edit.
  315. * @return string
  316. */
  317. public function get_image_id( $context = 'view' ) {
  318. $image_id = $this->get_prop( 'image_id', $context );
  319. if ( 'view' === $context && ! $image_id ) {
  320. $image_id = apply_filters( $this->get_hook_prefix() . 'image_id', $this->parent_data['image_id'], $this );
  321. }
  322. return $image_id;
  323. }
  324. /**
  325. * Get purchase note.
  326. *
  327. * @since 3.0.0
  328. * @param string $context What the value is for. Valid values are view and edit.
  329. * @return string
  330. */
  331. public function get_purchase_note( $context = 'view' ) {
  332. $value = $this->get_prop( 'purchase_note', $context );
  333. // Inherit value from parent.
  334. if ( 'view' === $context && empty( $value ) ) {
  335. $value = apply_filters( $this->get_hook_prefix() . 'purchase_note', $this->parent_data['purchase_note'], $this );
  336. }
  337. return $value;
  338. }
  339. /**
  340. * Get shipping class ID.
  341. *
  342. * @since 3.0.0
  343. * @param string $context What the value is for. Valid values are view and edit.
  344. * @return int
  345. */
  346. public function get_shipping_class_id( $context = 'view' ) {
  347. $shipping_class_id = $this->get_prop( 'shipping_class_id', $context );
  348. if ( 'view' === $context && ! $shipping_class_id ) {
  349. $shipping_class_id = apply_filters( $this->get_hook_prefix() . 'shipping_class_id', $this->parent_data['shipping_class_id'], $this );
  350. }
  351. return $shipping_class_id;
  352. }
  353. /**
  354. * Get catalog visibility.
  355. *
  356. * @param string $context What the value is for. Valid values are view and edit.
  357. * @return string
  358. */
  359. public function get_catalog_visibility( $context = 'view' ) {
  360. return apply_filters( $this->get_hook_prefix() . 'catalog_visibility', $this->parent_data['catalog_visibility'], $this );
  361. }
  362. /*
  363. |--------------------------------------------------------------------------
  364. | CRUD methods
  365. |--------------------------------------------------------------------------
  366. */
  367. /**
  368. * Set the parent data array for this variation.
  369. *
  370. * @since 3.0.0
  371. * @param array $parent_data parent data array for this variation.
  372. */
  373. public function set_parent_data( $parent_data ) {
  374. $this->parent_data = $parent_data;
  375. }
  376. /**
  377. * Get the parent data array for this variation.
  378. *
  379. * @since 3.0.0
  380. * @return array
  381. */
  382. public function get_parent_data() {
  383. return $this->parent_data;
  384. }
  385. /**
  386. * Set attributes. Unlike the parent product which uses terms, variations are assigned
  387. * specific attributes using name value pairs.
  388. *
  389. * @param array $raw_attributes array of raw attributes.
  390. */
  391. public function set_attributes( $raw_attributes ) {
  392. $raw_attributes = (array) $raw_attributes;
  393. $attributes = array();
  394. foreach ( $raw_attributes as $key => $value ) {
  395. // Remove attribute prefix which meta gets stored with.
  396. if ( 0 === strpos( $key, 'attribute_' ) ) {
  397. $key = substr( $key, 10 );
  398. }
  399. $attributes[ $key ] = $value;
  400. }
  401. $this->set_prop( 'attributes', $attributes );
  402. }
  403. /**
  404. * Returns whether or not the product has any visible attributes.
  405. *
  406. * Variations are mapped to specific attributes unlike products, and the return
  407. * value of ->get_attributes differs. Therefore this returns false.
  408. *
  409. * @return boolean
  410. */
  411. public function has_attributes() {
  412. return false;
  413. }
  414. /*
  415. |--------------------------------------------------------------------------
  416. | Conditionals
  417. |--------------------------------------------------------------------------
  418. */
  419. /**
  420. * Returns false if the product cannot be bought.
  421. * 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.
  422. *
  423. * @return bool
  424. */
  425. public function is_purchasable() {
  426. 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 );
  427. }
  428. /**
  429. * Controls whether this particular variation will appear greyed-out (inactive) or not (active).
  430. * Used by extensions to make incompatible variations appear greyed-out, etc.
  431. * Other possible uses: prevent out-of-stock variations from being selected.
  432. *
  433. * @return bool
  434. */
  435. public function variation_is_active() {
  436. return apply_filters( 'woocommerce_variation_is_active', true, $this );
  437. }
  438. /**
  439. * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed.
  440. * Instead, a suitable 'unavailable' message is displayed.
  441. * Invisible by default: Disabled variations and variations with an empty price.
  442. *
  443. * @return bool
  444. */
  445. public function variation_is_visible() {
  446. 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 );
  447. }
  448. /**
  449. * Return valid tax classes. Adds 'parent' to the default list of valid tax classes.
  450. *
  451. * @return array valid tax classes
  452. */
  453. protected function get_valid_tax_classes() {
  454. $valid_classes = WC_Tax::get_tax_class_slugs();
  455. $valid_classes[] = 'parent';
  456. return $valid_classes;
  457. }
  458. }