WC_Product_Variation

Product Variation Class.

Defined (1)

The class is defined in the following location(s).

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