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

  1. <?php 
  2. if ( ! defined( 'ABSPATH' ) ) { 
  3. exit; 
  4.  
  5. /** 
  6. * Product Variation Class. 
  7. * 
  8. * The WooCommerce product variation class handles product variation data. 
  9. * 
  10. * @class WC_Product_Variation 
  11. * @version 3.0.0 
  12. * @package WooCommerce/Classes 
  13. * @category Class 
  14. * @author WooThemes 
  15. */ 
  16. class WC_Product_Variation extends WC_Product_Simple { 
  17.  
  18. /** 
  19. * Post type. 
  20. * @var string 
  21. */ 
  22. public $post_type = 'product_variation'; 
  23.  
  24. /** 
  25. * Parent data. 
  26. * @var array 
  27. */ 
  28. protected $parent_data = array( 
  29. 'title' => '',  
  30. 'sku' => '',  
  31. 'manage_stock' => '',  
  32. 'backorders' => '',  
  33. 'stock_quantity' => '',  
  34. 'weight' => '',  
  35. 'length' => '',  
  36. 'width' => '',  
  37. 'height' => '',  
  38. 'tax_class' => '',  
  39. 'shipping_class_id' => '',  
  40. 'image_id' => '',  
  41. ); 
  42.  
  43. /** 
  44. * Prefix for action and filter hooks on data. 
  45. * 
  46. * @since 3.0.0 
  47. * @return string 
  48. */ 
  49. protected function get_hook_prefix() { 
  50. return 'woocommerce_product_variation_get_'; 
  51.  
  52. /** 
  53. * Get internal type. 
  54. * @return string 
  55. */ 
  56. public function get_type() { 
  57. return 'variation'; 
  58.  
  59. /** 
  60. * If the stock level comes from another product ID. 
  61. * @since 3.0.0 
  62. * @return int 
  63. */ 
  64. public function get_stock_managed_by_id() { 
  65. return 'parent' === $this->get_manage_stock() ? $this->get_parent_id() : $this->get_id(); 
  66.  
  67. /** 
  68. * Get the product's title. For variations this is the parent product name. 
  69. * 
  70. * @return string 
  71. */ 
  72. public function get_title() { 
  73. return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this ); 
  74.  
  75. /** 
  76. * Get variation attribute values. Keys are prefixed with attribute_, as stored. 
  77. * 
  78. * @return array of attributes and their values for this variation 
  79. */ 
  80. public function get_variation_attributes() { 
  81. $attributes = $this->get_attributes(); 
  82. $variation_attributes = array(); 
  83. foreach ( $attributes as $key => $value ) { 
  84. $variation_attributes[ 'attribute_' . $key ] = $value; 
  85. return $variation_attributes; 
  86.  
  87. /** 
  88. * Returns a single product attribute as a string. 
  89. * @param string $attribute to get. 
  90. * @return string 
  91. */ 
  92. public function get_attribute( $attribute ) { 
  93. $attributes = $this->get_attributes(); 
  94. $attribute = sanitize_title( $attribute ); 
  95.  
  96. if ( isset( $attributes[ $attribute ] ) ) { 
  97. $value = $attributes[ $attribute ]; 
  98. $term = taxonomy_exists( $attribute ) ? get_term_by( 'slug', $value, $attribute ) : false; 
  99. $value = ! is_wp_error( $term ) && $term ? $term->name : $value; 
  100. } elseif ( isset( $attributes[ 'pa_' . $attribute ] ) ) { 
  101. $value = $attributes[ 'pa_' . $attribute ]; 
  102. $term = taxonomy_exists( 'pa_' . $attribute ) ? get_term_by( 'slug', $value, 'pa_' . $attribute ) : false; 
  103. $value = ! is_wp_error( $term ) && $term ? $term->name : $value; 
  104. } else { 
  105. return ''; 
  106.  
  107. return $value; 
  108.  
  109. /** 
  110. * Wrapper for get_permalink. Adds this variations attributes to the URL. 
  111. * 
  112. * @param $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. 
  113. * @return string 
  114. */ 
  115. public function get_permalink( $item_object = null ) { 
  116. $url = get_permalink( $this->get_parent_id() ); 
  117.  
  118. if ( ! empty( $item_object['variation'] ) ) { 
  119. $data = $item_object['variation']; 
  120. } elseif ( ! empty( $item_object['item_meta_array'] ) ) { 
  121. $data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) ); 
  122. $data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' ); 
  123. $data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_variation_attributes() ); 
  124. } else { 
  125. $data = $this->get_variation_attributes(); 
  126.  
  127. return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), $url ); 
  128.  
  129. /** 
  130. * Get the add to url used mainly in loops. 
  131. * 
  132. * @return string 
  133. */ 
  134. public function add_to_cart_url() { 
  135. $url = $this->is_purchasable() ? remove_query_arg( 'added-to-cart', add_query_arg( array( 'variation_id' => $this->get_id(), 'add-to-cart' => $this->get_parent_id() ), $this->get_permalink() ) ) : $this->get_permalink(); 
  136. return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this ); 
  137.  
  138. /** 
  139. * Get SKU (Stock-keeping unit) - product unique ID. 
  140. * 
  141. * @param string $context 
  142. * @return string 
  143. */ 
  144. public function get_sku( $context = 'view' ) { 
  145. $value = $this->get_prop( 'sku', $context ); 
  146.  
  147. // Inherit value from parent. 
  148. if ( 'view' === $context && empty( $value ) ) { 
  149. $value = apply_filters( $this->get_hook_prefix() . 'sku', $this->parent_data['sku'], $this ); 
  150. return $value; 
  151.  
  152. /** 
  153. * Returns the product's weight. 
  154. * 
  155. * @param string $context 
  156. * @return string 
  157. */ 
  158. public function get_weight( $context = 'view' ) { 
  159. $value = $this->get_prop( 'weight', $context ); 
  160.  
  161. // Inherit value from parent. 
  162. if ( 'view' === $context && empty( $value ) ) { 
  163. $value = apply_filters( $this->get_hook_prefix() . 'weight', $this->parent_data['weight'], $this ); 
  164. return $value; 
  165.  
  166. /** 
  167. * Returns the product length. 
  168. * 
  169. * @param string $context 
  170. * @return string 
  171. */ 
  172. public function get_length( $context = 'view' ) { 
  173. $value = $this->get_prop( 'length', $context ); 
  174.  
  175. // Inherit value from parent. 
  176. if ( 'view' === $context && empty( $value ) ) { 
  177. $value = apply_filters( $this->get_hook_prefix() . 'length', $this->parent_data['length'], $this ); 
  178. return $value; 
  179.  
  180. /** 
  181. * Returns the product width. 
  182. * 
  183. * @param string $context 
  184. * @return string 
  185. */ 
  186. public function get_width( $context = 'view' ) { 
  187. $value = $this->get_prop( 'width', $context ); 
  188.  
  189. // Inherit value from parent. 
  190. if ( 'view' === $context && empty( $value ) ) { 
  191. $value = apply_filters( $this->get_hook_prefix() . 'width', $this->parent_data['width'], $this ); 
  192. return $value; 
  193.  
  194. /** 
  195. * Returns the product height. 
  196. * 
  197. * @param string $context 
  198. * @return string 
  199. */ 
  200. public function get_height( $context = 'view' ) { 
  201. $value = $this->get_prop( 'height', $context ); 
  202.  
  203. // Inherit value from parent. 
  204. if ( 'view' === $context && empty( $value ) ) { 
  205. $value = apply_filters( $this->get_hook_prefix() . 'height', $this->parent_data['height'], $this ); 
  206. return $value; 
  207.  
  208. /** 
  209. * Returns the tax class. 
  210. * 
  211. * Does not use get_prop so it can handle 'parent' Inheritance correctly. 
  212. * 
  213. * @param string $context view, edit, or unfiltered 
  214. * @return string 
  215. */ 
  216. public function get_tax_class( $context = 'view' ) { 
  217. $value = null; 
  218.  
  219. if ( array_key_exists( 'tax_class', $this->data ) ) { 
  220. $value = array_key_exists( 'tax_class', $this->changes ) ? $this->changes['tax_class'] : $this->data['tax_class']; 
  221.  
  222. if ( 'edit' !== $context && 'parent' === $value ) { 
  223. $value = $this->parent_data['tax_class']; 
  224.  
  225. if ( 'view' === $context ) { 
  226. $value = apply_filters( $this->get_hook_prefix() . 'tax_class', $value, $this ); 
  227. return $value; 
  228.  
  229. /** 
  230. * Return if product manage stock. 
  231. * 
  232. * @since 3.0.0 
  233. * @param string $context 
  234. * @return boolean|string true, false, or parent. 
  235. */ 
  236. public function get_manage_stock( $context = 'view' ) { 
  237. $value = $this->get_prop( 'manage_stock', $context ); 
  238.  
  239. // Inherit value from parent. 
  240. if ( 'view' === $context && false === $value && true === wc_string_to_bool( $this->parent_data['manage_stock'] ) ) { 
  241. $value = 'parent'; 
  242. return $value; 
  243.  
  244. /** 
  245. * Returns number of items available for sale. 
  246. * 
  247. * @param string $context 
  248. * @return int|null 
  249. */ 
  250. public function get_stock_quantity( $context = 'view' ) { 
  251. $value = $this->get_prop( 'stock_quantity', $context ); 
  252.  
  253. // Inherit value from parent. 
  254. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { 
  255. $value = apply_filters( $this->get_hook_prefix() . 'stock_quantity', $this->parent_data['stock_quantity'], $this ); 
  256. return $value; 
  257.  
  258. /** 
  259. * Get backorders. 
  260. * 
  261. * @param string $context 
  262. * @since 3.0.0 
  263. * @return string yes no or notify 
  264. */ 
  265. public function get_backorders( $context = 'view' ) { 
  266. $value = $this->get_prop( 'backorders', $context ); 
  267.  
  268. // Inherit value from parent. 
  269. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { 
  270. $value = apply_filters( $this->get_hook_prefix() . 'backorders', $this->parent_data['backorders'], $this ); 
  271. return $value; 
  272.  
  273. /** 
  274. * Get main image ID. 
  275. * 
  276. * @since 3.0.0 
  277. * @param string $context 
  278. * @return string 
  279. */ 
  280. public function get_image_id( $context = 'view' ) { 
  281. $image_id = $this->get_prop( 'image_id', $context ); 
  282.  
  283. if ( 'view' === $context && ! $image_id ) { 
  284. $image_id = apply_filters( $this->get_hook_prefix() . 'image_id', $this->parent_data['image_id'], $this ); 
  285.  
  286. return $image_id; 
  287.  
  288. /** 
  289. * Get shipping class ID. 
  290. * 
  291. * @since 3.0.0 
  292. * @param string $context 
  293. * @return int 
  294. */ 
  295. public function get_shipping_class_id( $context = 'view' ) { 
  296. $shipping_class_id = $this->get_prop( 'shipping_class_id', $context ); 
  297.  
  298. if ( 'view' === $context && ! $shipping_class_id ) { 
  299. $shipping_class_id = apply_filters( $this->get_hook_prefix() . 'shipping_class_id', $this->parent_data['shipping_class_id'], $this ); 
  300.  
  301. return $shipping_class_id; 
  302.  
  303. /** 
  304. |-------------------------------------------------------------------------- 
  305. | CRUD methods 
  306. |-------------------------------------------------------------------------- 
  307. */ 
  308.  
  309. /** 
  310. * Set the parent data array for this variation. 
  311. * 
  312. * @since 3.0.0 
  313. * @param array 
  314. */ 
  315. public function set_parent_data( $parent_data ) { 
  316. $this->parent_data = $parent_data; 
  317.  
  318. /** 
  319. * Get the parent data array for this variation. 
  320. * 
  321. * @since 3.0.0 
  322. * @return array 
  323. */ 
  324. public function get_parent_data() { 
  325. return $this->parent_data; 
  326.  
  327. /** 
  328. * Set attributes. Unlike the parent product which uses terms, variations are assigned 
  329. * specific attributes using name value pairs. 
  330. * @param array $raw_attributes 
  331. */ 
  332. public function set_attributes( $raw_attributes ) { 
  333. $raw_attributes = (array) $raw_attributes; 
  334. $attributes = array(); 
  335.  
  336. foreach ( $raw_attributes as $key => $value ) { 
  337. // Remove attribute prefix which meta gets stored with. 
  338. if ( 0 === strpos( $key, 'attribute_' ) ) { 
  339. $key = substr( $key, 10 ); 
  340. $attributes[ $key ] = $value; 
  341. $this->set_prop( 'attributes', $attributes ); 
  342.  
  343. /** 
  344. * Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored. 
  345. * 
  346. * @param string $context 
  347. * @return array 
  348. */ 
  349. public function get_attributes( $context = 'view' ) { 
  350. return $this->get_prop( 'attributes', $context ); 
  351.  
  352. /** 
  353. * Returns whether or not the product has any visible attributes. 
  354. * 
  355. * Variations are mapped to specific attributes unlike products, and the return 
  356. * value of ->get_attributes differs. Therefore this returns false. 
  357. * 
  358. * @return boolean 
  359. */ 
  360. public function has_attributes() { 
  361. return false; 
  362.  
  363. /** 
  364. |-------------------------------------------------------------------------- 
  365. | Conditionals 
  366. |-------------------------------------------------------------------------- 
  367. */ 
  368.  
  369. /** 
  370. * Returns false if the product cannot be bought. 
  371. * 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. 
  372. * 
  373. * @return bool 
  374. */ 
  375. public function is_purchasable() { 
  376. return apply_filters( 'woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable(), $this ); 
  377.  
  378. /** 
  379. * Controls whether this particular variation will appear greyed-out (inactive) or not (active). 
  380. * Used by extensions to make incompatible variations appear greyed-out, etc. 
  381. * Other possible uses: prevent out-of-stock variations from being selected. 
  382. * 
  383. * @return bool 
  384. */ 
  385. public function variation_is_active() { 
  386. return apply_filters( 'woocommerce_variation_is_active', true, $this ); 
  387.  
  388. /** 
  389. * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed. 
  390. * Instead, a suitable 'unavailable' message is displayed. 
  391. * Invisible by default: Disabled variations and variations with an empty price. 
  392. * 
  393. * @return bool 
  394. */ 
  395. public function variation_is_visible() { 
  396. 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 ); 
.