/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. $variation_data = array_map( 'urlencode', $this->get_variation_attributes() ); 
  136. $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(); 
  137. return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this ); 
  138.  
  139. /** 
  140. * Get SKU (Stock-keeping unit) - product unique ID. 
  141. * 
  142. * @param string $context 
  143. * @return string 
  144. */ 
  145. public function get_sku( $context = 'view' ) { 
  146. $value = $this->get_prop( 'sku', $context ); 
  147.  
  148. // Inherit value from parent. 
  149. if ( 'view' === $context && empty( $value ) ) { 
  150. $value = apply_filters( $this->get_hook_prefix() . 'sku', $this->parent_data['sku'], $this ); 
  151. return $value; 
  152.  
  153. /** 
  154. * Returns the product's weight. 
  155. * 
  156. * @param string $context 
  157. * @return string 
  158. */ 
  159. public function get_weight( $context = 'view' ) { 
  160. $value = $this->get_prop( 'weight', $context ); 
  161.  
  162. // Inherit value from parent. 
  163. if ( 'view' === $context && empty( $value ) ) { 
  164. $value = apply_filters( $this->get_hook_prefix() . 'weight', $this->parent_data['weight'], $this ); 
  165. return $value; 
  166.  
  167. /** 
  168. * Returns the product length. 
  169. * 
  170. * @param string $context 
  171. * @return string 
  172. */ 
  173. public function get_length( $context = 'view' ) { 
  174. $value = $this->get_prop( 'length', $context ); 
  175.  
  176. // Inherit value from parent. 
  177. if ( 'view' === $context && empty( $value ) ) { 
  178. $value = apply_filters( $this->get_hook_prefix() . 'length', $this->parent_data['length'], $this ); 
  179. return $value; 
  180.  
  181. /** 
  182. * Returns the product width. 
  183. * 
  184. * @param string $context 
  185. * @return string 
  186. */ 
  187. public function get_width( $context = 'view' ) { 
  188. $value = $this->get_prop( 'width', $context ); 
  189.  
  190. // Inherit value from parent. 
  191. if ( 'view' === $context && empty( $value ) ) { 
  192. $value = apply_filters( $this->get_hook_prefix() . 'width', $this->parent_data['width'], $this ); 
  193. return $value; 
  194.  
  195. /** 
  196. * Returns the product height. 
  197. * 
  198. * @param string $context 
  199. * @return string 
  200. */ 
  201. public function get_height( $context = 'view' ) { 
  202. $value = $this->get_prop( 'height', $context ); 
  203.  
  204. // Inherit value from parent. 
  205. if ( 'view' === $context && empty( $value ) ) { 
  206. $value = apply_filters( $this->get_hook_prefix() . 'height', $this->parent_data['height'], $this ); 
  207. return $value; 
  208.  
  209. /** 
  210. * Returns the tax class. 
  211. * 
  212. * Does not use get_prop so it can handle 'parent' Inheritance correctly. 
  213. * 
  214. * @param string $context view, edit, or unfiltered 
  215. * @return string 
  216. */ 
  217. public function get_tax_class( $context = 'view' ) { 
  218. $value = null; 
  219.  
  220. if ( array_key_exists( 'tax_class', $this->data ) ) { 
  221. $value = array_key_exists( 'tax_class', $this->changes ) ? $this->changes['tax_class'] : $this->data['tax_class']; 
  222.  
  223. if ( 'edit' !== $context && 'parent' === $value ) { 
  224. $value = $this->parent_data['tax_class']; 
  225.  
  226. if ( 'view' === $context ) { 
  227. $value = apply_filters( $this->get_hook_prefix() . 'tax_class', $value, $this ); 
  228. return $value; 
  229.  
  230. /** 
  231. * Return if product manage stock. 
  232. * 
  233. * @since 3.0.0 
  234. * @param string $context 
  235. * @return boolean|string true, false, or parent. 
  236. */ 
  237. public function get_manage_stock( $context = 'view' ) { 
  238. $value = $this->get_prop( 'manage_stock', $context ); 
  239.  
  240. // Inherit value from parent. 
  241. if ( 'view' === $context && false === $value && true === wc_string_to_bool( $this->parent_data['manage_stock'] ) ) { 
  242. $value = 'parent'; 
  243. return $value; 
  244.  
  245. /** 
  246. * Returns number of items available for sale. 
  247. * 
  248. * @param string $context 
  249. * @return int|null 
  250. */ 
  251. public function get_stock_quantity( $context = 'view' ) { 
  252. $value = $this->get_prop( 'stock_quantity', $context ); 
  253.  
  254. // Inherit value from parent. 
  255. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { 
  256. $value = apply_filters( $this->get_hook_prefix() . 'stock_quantity', $this->parent_data['stock_quantity'], $this ); 
  257. return $value; 
  258.  
  259. /** 
  260. * Get backorders. 
  261. * 
  262. * @param string $context 
  263. * @since 3.0.0 
  264. * @return string yes no or notify 
  265. */ 
  266. public function get_backorders( $context = 'view' ) { 
  267. $value = $this->get_prop( 'backorders', $context ); 
  268.  
  269. // Inherit value from parent. 
  270. if ( 'view' === $context && 'parent' === $this->get_manage_stock() ) { 
  271. $value = apply_filters( $this->get_hook_prefix() . 'backorders', $this->parent_data['backorders'], $this ); 
  272. return $value; 
  273.  
  274. /** 
  275. * Get main image ID. 
  276. * 
  277. * @since 3.0.0 
  278. * @param string $context 
  279. * @return string 
  280. */ 
  281. public function get_image_id( $context = 'view' ) { 
  282. $image_id = $this->get_prop( 'image_id', $context ); 
  283.  
  284. if ( 'view' === $context && ! $image_id ) { 
  285. $value = apply_filters( $this->get_hook_prefix() . 'image_id', $this->parent_data['image_id'], $this ); 
  286.  
  287. return $image_id; 
  288.  
  289. /** 
  290. * Get shipping class ID. 
  291. * 
  292. * @since 3.0.0 
  293. * @param string $context 
  294. * @return int 
  295. */ 
  296. public function get_shipping_class_id( $context = 'view' ) { 
  297. $shipping_class_id = $this->get_prop( 'shipping_class_id', $context ); 
  298.  
  299. if ( 'view' === $context && ! $shipping_class_id ) { 
  300. $value = apply_filters( $this->get_hook_prefix() . 'shipping_class_id', $this->parent_data['shipping_class_id'], $this ); 
  301.  
  302. return $shipping_class_id; 
  303.  
  304. /** 
  305. |-------------------------------------------------------------------------- 
  306. | CRUD methods 
  307. |-------------------------------------------------------------------------- 
  308. */ 
  309.  
  310. /** 
  311. * Set the parent data array for this variation. 
  312. * 
  313. * @since 3.0.0 
  314. * @param array 
  315. */ 
  316. public function set_parent_data( $parent_data ) { 
  317. $this->parent_data = $parent_data; 
  318.  
  319. /** 
  320. * Get the parent data array for this variation. 
  321. * 
  322. * @since 3.0.0 
  323. * @return array 
  324. */ 
  325. public function get_parent_data() { 
  326. return $this->parent_data; 
  327.  
  328. /** 
  329. * Set attributes. Unlike the parent product which uses terms, variations are assigned 
  330. * specific attributes using name value pairs. 
  331. * @param array $raw_attributes 
  332. */ 
  333. public function set_attributes( $raw_attributes ) { 
  334. $raw_attributes = (array) $raw_attributes; 
  335. $attributes = array(); 
  336.  
  337. foreach ( $raw_attributes as $key => $value ) { 
  338. // Remove attribute prefix which meta gets stored with. 
  339. if ( 0 === strpos( $key, 'attribute_' ) ) { 
  340. $key = substr( $key, 10 ); 
  341. $attributes[ $key ] = $value; 
  342. $this->set_prop( 'attributes', $attributes ); 
  343.  
  344. /** 
  345. * Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored. 
  346. * 
  347. * @param string $context 
  348. * @return array 
  349. */ 
  350. public function get_attributes( $context = 'view' ) { 
  351. return $this->get_prop( 'attributes', $context ); 
  352.  
  353. /** 
  354. |-------------------------------------------------------------------------- 
  355. | Conditionals 
  356. |-------------------------------------------------------------------------- 
  357. */ 
  358.  
  359. /** 
  360. * Returns false if the product cannot be bought. 
  361. * 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. 
  362. * 
  363. * @return bool 
  364. */ 
  365. public function is_purchasable() { 
  366. return apply_filters( 'woocommerce_variation_is_purchasable', $this->variation_is_visible() && parent::is_purchasable(), $this ); 
  367.  
  368. /** 
  369. * Controls whether this particular variation will appear greyed-out (inactive) or not (active). 
  370. * Used by extensions to make incompatible variations appear greyed-out, etc. 
  371. * Other possible uses: prevent out-of-stock variations from being selected. 
  372. * 
  373. * @return bool 
  374. */ 
  375. public function variation_is_active() { 
  376. return apply_filters( 'woocommerce_variation_is_active', true, $this ); 
  377.  
  378. /** 
  379. * Checks if this particular variation is visible. Invisible variations are enabled and can be selected, but no price / stock info is displayed. 
  380. * Instead, a suitable 'unavailable' message is displayed. 
  381. * Invisible by default: Disabled variations and variations with an empty price. 
  382. * 
  383. * @return bool 
  384. */ 
  385. public function variation_is_visible() { 
  386. 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 ); 
.