/classes/product_variation.class.php

https://github.com/coolio197/woocommerce · PHP · 215 lines · 128 code · 39 blank · 48 comment · 37 complexity · 2043e770a39956a15e0ab78c65b002cc MD5 · raw file

  1. <?php
  2. /**
  3. * Product Variation Class
  4. *
  5. * The WooCommerce product variation class handles product variation data.
  6. *
  7. * @class woocommerce_product_variation
  8. * @package WooCommerce
  9. * @category Class
  10. * @author WooThemes
  11. */
  12. class woocommerce_product_variation extends woocommerce_product {
  13. var $variation_data;
  14. var $variation_id;
  15. var $variation_has_weight;
  16. var $variation_has_price;
  17. var $variation_has_sale_price;
  18. var $variation_has_stock;
  19. var $variation_has_sku;
  20. /**
  21. * Loads all product data from custom fields
  22. *
  23. * @param int $id ID of the product to load
  24. */
  25. function woocommerce_product_variation( $variation_id, $parent_id = '', $parent_custom_fields = '' ) {
  26. $this->variation_id = $variation_id;
  27. $product_custom_fields = get_post_custom( $this->variation_id );
  28. $this->exists = (sizeof($product_custom_fields)>0) ? true : false;
  29. $this->variation_data = array();
  30. foreach ($product_custom_fields as $name => $value) :
  31. if (!strstr($name, 'attribute_')) continue;
  32. $this->variation_data[$name] = $value[0];
  33. endforeach;
  34. /* Get main product data from parent */
  35. $this->id = ($parent_id>0) ? $parent_id : wp_get_post_parent_id( $this->variation_id );
  36. if (!$parent_custom_fields) $parent_custom_fields = get_post_custom( $this->id );
  37. // Define the data we're going to load from the parent: Key => Default value
  38. $load_data = array(
  39. 'sku' => $this->id,
  40. 'price' => 0,
  41. 'visibility' => 'hidden',
  42. 'stock' => 0,
  43. 'stock_status' => 'instock',
  44. 'backorders' => 'no',
  45. 'manage_stock' => 'no',
  46. 'sale_price' => '',
  47. 'regular_price' => '',
  48. 'weight' => '',
  49. 'tax_status' => 'taxable',
  50. 'tax_class' => '',
  51. 'upsell_ids' => array(),
  52. 'crosssell_ids' => array()
  53. );
  54. // Load the data from the custom fields
  55. foreach ($load_data as $key => $default) :
  56. $this->$key = (isset($parent_custom_fields[$key][0]) && $parent_custom_fields[$key][0]!=='') ? $parent_custom_fields[$key][0] : $default;
  57. endforeach;
  58. // Load serialised data, unserialise twice to fix WP bug
  59. if (isset($product_custom_fields['product_attributes'][0])) $this->attributes = maybe_unserialize( maybe_unserialize( $product_custom_fields['product_attributes'][0] )); else $this->attributes = array();
  60. $this->product_type = 'variable';
  61. $this->variation_has_sku = $this->variation_has_stock = $this->variation_has_weight = $this->variation_has_price = $this->variation_has_sale_price = false;
  62. /* Override parent data with variation */
  63. if (isset($product_custom_fields['sku'][0]) && !empty($product_custom_fields['sku'][0])) :
  64. $this->variation_has_sku = true;
  65. $this->sku = $product_custom_fields['sku'][0];
  66. endif;
  67. if (isset($product_custom_fields['stock'][0]) && $product_custom_fields['stock'][0]!=='') :
  68. $this->variation_has_stock = true;
  69. $this->manage_stock = 'yes';
  70. $this->stock = $product_custom_fields['stock'][0];
  71. endif;
  72. if (isset($product_custom_fields['weight'][0]) && $product_custom_fields['weight'][0]!=='') :
  73. $this->variation_has_weight = true;
  74. $this->weight = $product_custom_fields['weight'][0];
  75. endif;
  76. if (isset($product_custom_fields['price'][0]) && $product_custom_fields['price'][0]!=='') :
  77. $this->variation_has_price = true;
  78. $this->price = $product_custom_fields['price'][0];
  79. $this->regular_price = $product_custom_fields['price'][0];
  80. endif;
  81. if (isset($product_custom_fields['sale_price'][0]) && $product_custom_fields['sale_price'][0]!=='') :
  82. $this->variation_has_sale_price = true;
  83. $this->sale_price = $product_custom_fields['sale_price'][0];
  84. if ($this->sale_price < $this->price) $this->price = $this->sale_price;
  85. endif;
  86. $this->total_stock = $this->stock;
  87. }
  88. /**
  89. * Get variation ID
  90. *
  91. * @return int
  92. */
  93. function get_variation_id() {
  94. return (int) $this->variation_id;
  95. }
  96. /**
  97. * Get variation attribute values
  98. *
  99. * @return array of attributes and their values for this variation
  100. */
  101. function get_variation_attributes() {
  102. return $this->variation_data;
  103. }
  104. /**
  105. * Get variation attribute values
  106. *
  107. * @return string containing the formatted price
  108. */
  109. function get_price_html() {
  110. if ($this->variation_has_price || $this->variation_has_sale_price) :
  111. $price = '';
  112. if ($this->price) :
  113. if ($this->variation_has_sale_price) :
  114. $price .= '<del>'.woocommerce_price( $this->regular_price ).'</del> <ins>'.woocommerce_price( $this->sale_price ).'</ins>';
  115. else :
  116. $price .= woocommerce_price( $this->price );
  117. endif;
  118. endif;
  119. return $price;
  120. else :
  121. return woocommerce_price(parent::get_price());
  122. endif;
  123. }
  124. /**
  125. * Reduce stock level of the product
  126. *
  127. * @param int $by Amount to reduce by
  128. */
  129. function reduce_stock( $by = 1 ) {
  130. if ($this->variation_has_stock) :
  131. if ($this->managing_stock()) :
  132. $this->stock = $this->stock - $by;
  133. $this->total_stock = $this->total_stock - $by;
  134. update_post_meta($this->variation_id, 'stock', $this->stock);
  135. // Parents out of stock attribute
  136. if (!$this->is_in_stock()) :
  137. // Check parent
  138. $parent_product = &new woocommerce_product( $this->id );
  139. if ($parent_product->managing_stock()) :
  140. if (!$parent_product->backorders_allowed()) :
  141. if ($parent_product->total_stock==0 || $parent_product->total_stock<0) :
  142. update_post_meta($this->id, 'stock_status', 'outofstock');
  143. endif;
  144. endif;
  145. else :
  146. if ($parent_product->total_stock==0 || $parent_product->total_stock<0) :
  147. update_post_meta($this->id, 'stock_status', 'outofstock');
  148. endif;
  149. endif;
  150. endif;
  151. return $this->stock;
  152. endif;
  153. else :
  154. return parent::reduce_stock( $by );
  155. endif;
  156. }
  157. /**
  158. * Increase stock level of the product
  159. *
  160. * @param int $by Amount to increase by
  161. */
  162. function increase_stock( $by = 1 ) {
  163. if ($this->variation_has_stock) :
  164. if ($this->managing_stock()) :
  165. $this->stock = $this->stock + $by;
  166. $this->total_stock = $this->total_stock + $by;
  167. update_post_meta($this->variation_id, 'stock', $this->stock);
  168. // Parents out of stock attribute
  169. if ($this->is_in_stock()) update_post_meta($this->id, 'stock_status', 'instock');
  170. return $this->stock;
  171. endif;
  172. else :
  173. return parent::increase_stock( $by );
  174. endif;
  175. }
  176. }