/includes/data-stores/class-wc-product-variable-data-store-cpt.php

  1. <?php 
  2. if ( ! defined( 'ABSPATH' ) ) { 
  3. exit; 
  4.  
  5. /** 
  6. * WC Variable Product Data Store: Stored in CPT. 
  7. * 
  8. * @version 3.0.0 
  9. * @category Class 
  10. * @author WooThemes 
  11. */ 
  12. class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface, WC_Product_Variable_Data_Store_Interface { 
  13.  
  14. /** 
  15. * Cached & hashed prices array for child variations. 
  16. * 
  17. * @var array 
  18. */ 
  19. protected $prices_array = array(); 
  20.  
  21. /** 
  22. * Read product data. 
  23. * 
  24. * @since 3.0.0 
  25. */ 
  26. protected function read_product_data( &$product ) { 
  27. parent::read_product_data( $product ); 
  28.  
  29. // Set directly since individual data needs changed at the WC_Product_Variation level -- these datasets just pull. 
  30. $children = $this->read_children( $product ); 
  31. $product->set_children( $children['all'] ); 
  32. $product->set_visible_children( $children['visible'] ); 
  33. $product->set_variation_attributes( $this->read_variation_attributes( $product ) ); 
  34.  
  35. /** 
  36. * Loads variation child IDs. 
  37. * 
  38. * @param WC_Product 
  39. * @param bool $force_read True to bypass the transient. 
  40. * @return array 
  41. */ 
  42. protected function read_children( &$product, $force_read = false ) { 
  43. $children_transient_name = 'wc_product_children_' . $product->get_id(); 
  44. $children = get_transient( $children_transient_name ); 
  45.  
  46. if ( empty( $children ) || ! is_array( $children ) || ! isset( $children['all'] ) || ! isset( $children['visible'] ) || $force_read ) { 
  47. $all_args = $visible_only_args = array( 
  48. 'post_parent' => $product->get_id(),  
  49. 'post_type' => 'product_variation',  
  50. 'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ),  
  51. 'fields' => 'ids',  
  52. 'post_status' => 'publish',  
  53. 'numberposts' => -1,  
  54. ); 
  55. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 
  56. $visible_only_args['tax_query'][] = array( 
  57. 'taxonomy' => 'product_visibility',  
  58. 'field' => 'name',  
  59. 'terms' => 'outofstock',  
  60. 'operator' => 'NOT IN',  
  61. ); 
  62. $children['all'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $all_args, $product, false ) ); 
  63. $children['visible'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $visible_only_args, $product, true ) ); 
  64.  
  65. set_transient( $children_transient_name, $children, DAY_IN_SECONDS * 30 ); 
  66.  
  67. $children['all'] = wp_parse_id_list( (array) $children['all'] ); 
  68. $children['visible'] = wp_parse_id_list( (array) $children['visible'] ); 
  69.  
  70. return $children; 
  71.  
  72. /** 
  73. * Loads an array of attributes used for variations, as well as their possible values. 
  74. * 
  75. * @param WC_Product 
  76. * @return array 
  77. */ 
  78. protected function read_variation_attributes( &$product ) { 
  79. global $wpdb; 
  80.  
  81. $variation_attributes = array(); 
  82. $attributes = $product->get_attributes(); 
  83. $child_ids = $product->get_children(); 
  84.  
  85. if ( ! empty( $child_ids ) && ! empty( $attributes ) ) { 
  86. foreach ( $attributes as $attribute ) { 
  87. if ( empty( $attribute['is_variation'] ) ) { 
  88. continue; 
  89.  
  90. // Get possible values for this attribute, for only visible variations. 
  91. $values = array_unique( $wpdb->get_col( $wpdb->prepare( 
  92. "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN (" . implode( ', ', array_map( 'absint', $child_ids ) ) . ")",  
  93. wc_variation_attribute_name( $attribute['name'] ) 
  94. ) ) ); 
  95.  
  96. // Empty value indicates that all options for given attribute are available. 
  97. if ( in_array( '', $values ) || empty( $values ) ) { 
  98. $values = $attribute['is_taxonomy'] ? wc_get_object_terms( $product->get_id(), $attribute['name'], 'slug' ) : wc_get_text_attributes( $attribute['value'] ); 
  99. // Get custom attributes (non taxonomy) as defined. 
  100. } elseif ( ! $attribute['is_taxonomy'] ) { 
  101. $text_attributes = wc_get_text_attributes( $attribute['value'] ); 
  102. $assigned_text_attributes = $values; 
  103. $values = array(); 
  104.  
  105. // Pre 2.4 handling where 'slugs' were saved instead of the full text attribute 
  106. if ( version_compare( get_post_meta( $product->get_id(), '_product_version', true ), '2.4.0', '<' ) ) { 
  107. $assigned_text_attributes = array_map( 'sanitize_title', $assigned_text_attributes ); 
  108. foreach ( $text_attributes as $text_attribute ) { 
  109. if ( in_array( sanitize_title( $text_attribute ), $assigned_text_attributes ) ) { 
  110. $values[] = $text_attribute; 
  111. } else { 
  112. foreach ( $text_attributes as $text_attribute ) { 
  113. if ( in_array( $text_attribute, $assigned_text_attributes ) ) { 
  114. $values[] = $text_attribute; 
  115. $variation_attributes[ $attribute['name'] ] = array_unique( $values ); 
  116.  
  117. return $variation_attributes; 
  118.  
  119. /** 
  120. * Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale. 
  121. * 
  122. * Can be filtered by plugins which modify costs, but otherwise will include the raw meta costs unlike get_price() which runs costs through the woocommerce_get_price filter. 
  123. * This is to ensure modified prices are not cached, unless intended. 
  124. * 
  125. * @since 3.0.0 
  126. * @param WC_Product 
  127. * @param bool $include_taxes If taxes should be calculated or not. 
  128. * @return array of prices 
  129. */ 
  130. public function read_price_data( &$product, $include_taxes = false ) { 
  131.  
  132. /** 
  133. * Transient name for storing prices for this product (note: Max transient length is 45) 
  134. * @since 2.5.0 a single transient is used per product for all prices, rather than many transients per product. 
  135. */ 
  136. $transient_name = 'wc_var_prices_' . $product->get_id(); 
  137.  
  138. $price_hash = $this->get_price_hash( $product, $include_taxes ); 
  139.  
  140. /** 
  141. * $this->prices_array is an array of values which may have been modified from what is stored in transients - this may not match $transient_cached_prices_array. 
  142. * If the value has already been generated, we don't need to grab the values again so just return them. They are already filtered. 
  143. */ 
  144. if ( empty( $this->prices_array[ $price_hash ] ) ) { 
  145. $transient_cached_prices_array = array_filter( (array) json_decode( strval( get_transient( $transient_name ) ), true ) ); 
  146.  
  147. // If the product version has changed since the transient was last saved, reset the transient cache. 
  148. if ( empty( $transient_cached_prices_array['version'] ) || WC_Cache_Helper::get_transient_version( 'product' ) !== $transient_cached_prices_array['version'] ) { 
  149. $transient_cached_prices_array = array( 'version' => WC_Cache_Helper::get_transient_version( 'product' ) ); 
  150.  
  151. // If the prices are not stored for this hash, generate them and add to the transient. 
  152. if ( empty( $transient_cached_prices_array[ $price_hash ] ) ) { 
  153. $prices = array(); 
  154. $regular_prices = array(); 
  155. $sale_prices = array(); 
  156. $variation_ids = $product->get_visible_children(); 
  157. foreach ( $variation_ids as $variation_id ) { 
  158. if ( $variation = wc_get_product( $variation_id ) ) { 
  159. $price = apply_filters( 'woocommerce_variation_prices_price', $variation->get_price( 'edit' ), $variation, $product ); 
  160. $regular_price = apply_filters( 'woocommerce_variation_prices_regular_price', $variation->get_regular_price( 'edit' ), $variation, $product ); 
  161. $sale_price = apply_filters( 'woocommerce_variation_prices_sale_price', $variation->get_sale_price( 'edit' ), $variation, $product ); 
  162.  
  163. // Skip empty prices 
  164. if ( '' === $price ) { 
  165. continue; 
  166.  
  167. // If sale price does not equal price, the product is not yet on sale 
  168. if ( $sale_price === $regular_price || $sale_price !== $price ) { 
  169. $sale_price = $regular_price; 
  170.  
  171. // If we are getting prices for display, we need to account for taxes 
  172. if ( $include_taxes ) { 
  173. if ( 'incl' === get_option( 'woocommerce_tax_display_shop' ) ) { 
  174. $price = '' === $price ? '' : wc_get_price_including_tax( $variation, array( 'qty' => 1, 'price' => $price ) ); 
  175. $regular_price = '' === $regular_price ? '' : wc_get_price_including_tax( $variation, array( 'qty' => 1, 'price' => $regular_price ) ); 
  176. $sale_price = '' === $sale_price ? '' : wc_get_price_including_tax( $variation, array( 'qty' => 1, 'price' => $sale_price ) ); 
  177. } else { 
  178. $price = '' === $price ? '' : wc_get_price_excluding_tax( $variation, array( 'qty' => 1, 'price' => $price ) ); 
  179. $regular_price = '' === $regular_price ? '' : wc_get_price_excluding_tax( $variation, array( 'qty' => 1, 'price' => $regular_price ) ); 
  180. $sale_price = '' === $sale_price ? '' : wc_get_price_excluding_tax( $variation, array( 'qty' => 1, 'price' => $sale_price ) ); 
  181.  
  182. $prices[ $variation_id ] = wc_format_decimal( $price, wc_get_price_decimals() ); 
  183. $regular_prices[ $variation_id ] = wc_format_decimal( $regular_price, wc_get_price_decimals() ); 
  184. $sale_prices[ $variation_id ] = wc_format_decimal( $sale_price . '.00', wc_get_price_decimals() ); 
  185.  
  186. $transient_cached_prices_array[ $price_hash ] = array( 
  187. 'price' => $prices,  
  188. 'regular_price' => $regular_prices,  
  189. 'sale_price' => $sale_prices,  
  190. ); 
  191.  
  192. set_transient( $transient_name, json_encode( $transient_cached_prices_array ), DAY_IN_SECONDS * 30 ); 
  193.  
  194. /** 
  195. * Give plugins one last chance to filter the variation prices array which has been generated and store locally to the class. 
  196. * This value may differ from the transient cache. It is filtered once before storing locally. 
  197. */ 
  198. $this->prices_array[ $price_hash ] = apply_filters( 'woocommerce_variation_prices', $transient_cached_prices_array[ $price_hash ], $product, $include_taxes ); 
  199. return $this->prices_array[ $price_hash ]; 
  200.  
  201. /** 
  202. * Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters. 
  203. * DEVELOPERS should filter this hash if offering conditonal pricing to keep it unique. 
  204. * 
  205. * @since 3.0.0 
  206. * @param WC_Product 
  207. * @param bool $include_taxes If taxes should be calculated or not. 
  208. * @return string 
  209. */ 
  210. protected function get_price_hash( &$product, $include_taxes = false ) { 
  211. global $wp_filter; 
  212.  
  213. $price_hash = $include_taxes ? array( get_option( 'woocommerce_tax_display_shop', 'excl' ), WC_Tax::get_rates() ) : array( false ); 
  214. $filter_names = array( 'woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price' ); 
  215.  
  216. foreach ( $filter_names as $filter_name ) { 
  217. if ( ! empty( $wp_filter[ $filter_name ] ) ) { 
  218. $price_hash[ $filter_name ] = array(); 
  219.  
  220. foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) { 
  221. $price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) ); 
  222.  
  223. $price_hash[] = WC_Cache_Helper::get_transient_version( 'product' ); 
  224. $price_hash = md5( json_encode( apply_filters( 'woocommerce_get_variation_prices_hash', $price_hash, $product, $include_taxes ) ) ); 
  225.  
  226. return $price_hash; 
  227.  
  228. /** 
  229. * Does a child have a weight set? 
  230. * 
  231. * @since 3.0.0 
  232. * @param WC_Product 
  233. * @return boolean 
  234. */ 
  235. public function child_has_weight( $product ) { 
  236. global $wpdb; 
  237. $children = $product->get_visible_children(); 
  238. return $children ? null !== $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_weight' AND meta_value > 0 AND post_id IN ( " . implode( ', ', array_map( 'absint', $children ) ) . " )" ) : false; 
  239.  
  240. /** 
  241. * Does a child have dimensions set? 
  242. * 
  243. * @since 3.0.0 
  244. * @param WC_Product 
  245. * @return boolean 
  246. */ 
  247. public function child_has_dimensions( $product ) { 
  248. global $wpdb; 
  249. $children = $product->get_visible_children(); 
  250. return $children ? null !== $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key IN ( '_length', '_width', '_height' ) AND meta_value > 0 AND post_id IN ( " . implode( ', ', array_map( 'absint', $children ) ) . " )" ) : false; 
  251.  
  252. /** 
  253. * Is a child in stock? 
  254. * 
  255. * @since 3.0.0 
  256. * @param WC_Product 
  257. * @return boolean 
  258. */ 
  259. public function child_is_in_stock( $product ) { 
  260. global $wpdb; 
  261. $children = $product->get_children(); 
  262. $oufofstock_children = $children ? $wpdb->get_var( "SELECT COUNT( post_id ) FROM $wpdb->postmeta WHERE meta_key = '_stock_status' AND meta_value = 'outofstock' AND post_id IN ( " . implode( ', ', array_map( 'absint', $children ) ) . " )" ) : 0; 
  263. return count( $children ) > $oufofstock_children; 
  264.  
  265. /** 
  266. * Syncs all variation names if the parent name is changed. 
  267. * 
  268. * @param WC_Product $product 
  269. * @param string $previous_name 
  270. * @param string $new_name 
  271. * @since 3.0.0 
  272. */ 
  273. public function sync_variation_names( &$product, $previous_name = '', $new_name = '' ) { 
  274. if ( $new_name !== $previous_name ) { 
  275. global $wpdb; 
  276.  
  277. $wpdb->query( $wpdb->prepare( 
  278. UPDATE {$wpdb->posts} 
  279. SET post_title = REPLACE( post_title, %s, %s ) 
  280. WHERE post_type = 'product_variation' 
  281. AND post_parent = %d 
  282. ",  
  283. $previous_name ? $previous_name : 'AUTO-DRAFT',  
  284. $new_name,  
  285. $product->get_id() 
  286. ) ); 
  287.  
  288. /** 
  289. * Stock managed at the parent level - update children being managed by this product. 
  290. * This sync function syncs downwards (from parent to child) when the variable product is saved. 
  291. * 
  292. * @param WC_Product 
  293. * @since 3.0.0 
  294. */ 
  295. public function sync_managed_variation_stock_status( &$product ) { 
  296. global $wpdb; 
  297.  
  298. if ( $product->get_manage_stock() ) { 
  299. $status = $product->get_stock_status(); 
  300. $children = $product->get_children(); 
  301. $managed_children = $children ? array_unique( $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_manage_stock' AND meta_value != 'yes' AND post_id IN ( " . implode( ', ', array_map( 'absint', $children ) ) . " )" ) ) : array(); 
  302. $changed = false; 
  303. foreach ( $managed_children as $managed_child ) { 
  304. if ( update_post_meta( $managed_child, '_stock_status', $status ) ) { 
  305. $changed = true; 
  306. if ( $changed ) { 
  307. $children = $this->read_children( $product, true ); 
  308. $product->set_children( $children['all'] ); 
  309. $product->set_visible_children( $children['visible'] ); 
  310.  
  311. /** 
  312. * Sync variable product prices with children. 
  313. * 
  314. * @since 3.0.0 
  315. * @param WC_Product|int $product 
  316. */ 
  317. public function sync_price( &$product ) { 
  318. global $wpdb; 
  319.  
  320. $children = $product->get_visible_children(); 
  321. $prices = $children ? array_unique( $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_price' AND post_id IN ( " . implode( ', ', array_map( 'absint', $children ) ) . " )" ) ) : array(); 
  322.  
  323. delete_post_meta( $product->get_id(), '_price' ); 
  324.  
  325. if ( $prices ) { 
  326. sort( $prices ); 
  327. // To allow sorting and filtering by multiple values, we have no choice but to store child prices in this manner. 
  328. foreach ( $prices as $price ) { 
  329. if ( is_null( $price ) || '' === $price ) { 
  330. continue; 
  331. add_post_meta( $product->get_id(), '_price', $price, false ); 
  332.  
  333. /** 
  334. * Sync variable product stock status with children. 
  335. * Change does not persist unless saved by caller. 
  336. * 
  337. * @since 3.0.0 
  338. * @param WC_Product|int $product 
  339. */ 
  340. public function sync_stock_status( &$product ) { 
  341. $product->set_stock_status( $product->child_is_in_stock() ? 'instock' : 'outofstock' ); 
  342.  
  343. /** 
  344. * Delete variations of a product. 
  345. * 
  346. * @since 3.0.0 
  347. * @param int $product_id 
  348. * @param $force_delete False to trash. 
  349. */ 
  350. public function delete_variations( $product_id, $force_delete = false ) { 
  351. if ( ! is_numeric( $product_id ) || 0 >= $product_id ) { 
  352. return; 
  353.  
  354. $variation_ids = wp_parse_id_list( get_posts( array( 
  355. 'post_parent' => $product_id,  
  356. 'post_type' => 'product_variation',  
  357. 'fields' => 'ids',  
  358. 'post_status' => array( 'any', 'trash', 'auto-draft' ),  
  359. 'numberposts' => -1,  
  360. ) ) ); 
  361.  
  362. if ( ! empty( $variation_ids ) ) { 
  363. foreach ( $variation_ids as $variation_id ) { 
  364. if ( $force_delete ) { 
  365. wp_delete_post( $variation_id, true ); 
  366. } else { 
  367. wp_trash_post( $variation_id ); 
  368.  
  369. delete_transient( 'wc_product_children_' . $product_id ); 
  370.  
  371. /** 
  372. * Untrash variations. 
  373. * 
  374. * @param int $product_id 
  375. */ 
  376. public function untrash_variations( $product_id ) { 
  377. $variation_ids = wp_parse_id_list( get_posts( array( 
  378. 'post_parent' => $product_id,  
  379. 'post_type' => 'product_variation',  
  380. 'fields' => 'ids',  
  381. 'post_status' => 'trash',  
  382. 'numberposts' => -1,  
  383. ) ) ); 
  384.  
  385. if ( ! empty( $variation_ids ) ) { 
  386. foreach ( $variation_ids as $variation_id ) { 
  387. wp_untrash_post( $variation_id ); 
  388.  
  389. delete_transient( 'wc_product_children_' . $product_id ); 
.