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rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / data-stores / class-wc-product-variable-data-store-cpt.php
woocommerce / includes / data-stores Last commit date
abstract-wc-order-data-store-cpt.php 2 months ago abstract-wc-order-item-type-data-store.php 7 months ago class-wc-coupon-data-store-cpt.php 2 months ago class-wc-customer-data-store-session.php 9 months ago class-wc-customer-data-store.php 3 months ago class-wc-customer-download-data-store.php 3 years ago class-wc-customer-download-log-data-store.php 1 year ago class-wc-data-store-wp.php 9 months ago class-wc-order-data-store-cpt.php 2 months ago class-wc-order-item-coupon-data-store.php 8 years ago class-wc-order-item-data-store.php 2 months ago class-wc-order-item-fee-data-store.php 8 years ago class-wc-order-item-product-data-store.php 3 years ago class-wc-order-item-shipping-data-store.php 1 year ago class-wc-order-item-tax-data-store.php 6 years ago class-wc-order-refund-data-store-cpt.php 2 months ago class-wc-payment-token-data-store.php 4 months ago class-wc-product-data-store-cpt.php 2 months ago class-wc-product-grouped-data-store-cpt.php 3 months ago class-wc-product-variable-data-store-cpt.php 1 month ago class-wc-product-variation-data-store-cpt.php 9 months ago class-wc-shipping-zone-data-store.php 5 months ago class-wc-webhook-data-store.php 2 years ago
class-wc-product-variable-data-store-cpt.php
1100 lines
1 <?php
2 /**
3 * File for WC Variable Product Data Store class.
4 *
5 * @package WooCommerce\Classes
6 */
7
8 use Automattic\WooCommerce\Internal\Caches\ProductVersionStringInvalidator;
9 use Automattic\WooCommerce\Enums\ProductStatus;
10 use Automattic\WooCommerce\Enums\ProductStockStatus;
11 use Automattic\WooCommerce\Utilities\CallbackUtil;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * WC Variable Product Data Store: Stored in CPT.
19 *
20 * @version 3.0.0
21 */
22 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 {
23
24 /**
25 * Cached & hashed prices array for child variations.
26 *
27 * @var array
28 */
29 protected $prices_array = array();
30
31 /**
32 * Read attributes from post meta.
33 *
34 * @param WC_Product $product Product object.
35 */
36 protected function read_attributes( &$product ) {
37 $product_id = $product->get_id();
38 $meta_attributes = get_post_meta( $product_id, '_product_attributes', true );
39
40 if ( ! empty( $meta_attributes ) && is_array( $meta_attributes ) ) {
41 $attributes = array();
42 $force_update = false;
43 foreach ( $meta_attributes as $meta_attribute_key => $meta_attribute_value ) {
44 $meta_value = array_merge(
45 array(
46 'name' => '',
47 'value' => '',
48 'position' => 0,
49 'is_visible' => 0,
50 'is_variation' => 0,
51 'is_taxonomy' => 0,
52 ),
53 (array) $meta_attribute_value
54 );
55
56 // Maintain data integrity: WordPress 4.9 changed sanitization functions, and we update the values here so variations function correctly.
57 // As per 2026, we are refactoring the updates into product-level: BC-focused (not all-in on-spot migration), optimized for performance.
58 // Use-case: `_product_attributes` has data populated on WordPress pre-4.8 and containing symbols affected by the breaking changes.
59 if ( $meta_value['is_variation'] && strstr( $meta_value['name'], '/' ) && sanitize_title( $meta_value['name'] ) !== $meta_attribute_key ) {
60 global $wpdb;
61
62 $child_ids = $product->get_children();
63 if ( ! empty( $child_ids ) ) {
64 $products_to_migrate = implode( ', ', $child_ids );
65 $old_slug = 'attribute_' . $meta_attribute_key;
66 $old_meta_rows = $wpdb->get_results(
67 $wpdb->prepare(
68 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
69 "SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN ( $products_to_migrate )",
70 $old_slug
71 )
72 );
73 if ( $old_meta_rows ) {
74 $new_slug = 'attribute_' . sanitize_title( $meta_value['name'] );
75 foreach ( $old_meta_rows as $old_meta_row ) {
76 update_post_meta( $old_meta_row->post_id, $new_slug, $old_meta_row->meta_value );
77 }
78 }
79 }
80
81 $force_update = true;
82 }
83
84 // Check if is a taxonomy attribute.
85 if ( ! empty( $meta_value['is_taxonomy'] ) ) {
86 if ( ! taxonomy_exists( $meta_value['name'] ) ) {
87 continue;
88 }
89 $id = wc_attribute_taxonomy_id_by_name( $meta_value['name'] );
90 $options = wc_get_object_terms( $product_id, $meta_value['name'], 'term_id' );
91 } else {
92 $id = 0;
93 $options = wc_get_text_attributes( $meta_value['value'] );
94 }
95
96 $attribute = new WC_Product_Attribute();
97 $attribute->set_id( $id );
98 $attribute->set_name( $meta_value['name'] );
99 $attribute->set_options( $options );
100 $attribute->set_position( $meta_value['position'] );
101 $attribute->set_visible( $meta_value['is_visible'] );
102 $attribute->set_variation( $meta_value['is_variation'] );
103
104 /**
105 * Filter product attribute after initialization.
106 *
107 * @since 10.6.0
108 *
109 * @param WC_Product_Attribute $attribute The attribute object.
110 * @param array $meta_value The meta value.
111 * @param WC_Product $product The product object.
112 */
113 $attributes[] = apply_filters( 'woocommerce_product_read_attribute', $attribute, $meta_value, $product );
114 }
115 $product->set_attributes( $attributes );
116
117 if ( $force_update ) {
118 $this->update_attributes( $product, true );
119 }
120 }
121 }
122
123 /**
124 * Read product data.
125 *
126 * @param WC_Product $product Product object.
127 *
128 * @since 3.0.0
129 */
130 protected function read_product_data( &$product ) {
131 // Prime caches to reduce future queries.
132 if ( ! wp_using_ext_object_cache() ) {
133 $product_id = $product->get_id();
134 wp_prime_option_caches(
135 array(
136 '_transient_wc_var_prices_' . $product_id,
137 '_transient_timeout_wc_var_prices_' . $product_id,
138 '_transient_wc_product_children_' . $product_id,
139 '_transient_timeout_wc_product_children_' . $product_id,
140 )
141 );
142 }
143
144 parent::read_product_data( $product );
145
146 // Make sure data which does not apply to variables is unset.
147 $product->set_regular_price( '' );
148 $product->set_sale_price( '' );
149 }
150
151 /**
152 * Loads variation child IDs.
153 *
154 * @param WC_Product $product Product object.
155 * @param bool $force_read True to bypass the transient.
156 *
157 * @return array
158 */
159 public function read_children( &$product, $force_read = false ) {
160 $children_transient_name = 'wc_product_children_' . $product->get_id();
161 $children = get_transient( $children_transient_name );
162 if ( empty( $children ) || ! is_array( $children ) ) {
163 $children = array();
164 }
165
166 $transient_version = WC_Cache_Helper::get_transient_version( 'product' );
167
168 if ( ! $force_read && $children ) {
169 // Validate the children data.
170 if ( ! $this->validate_children_data( $children, $transient_version ) ) {
171 $children = array();
172 $force_read = true;
173 }
174 }
175
176 if ( ! isset( $children['all'] ) || ! isset( $children['visible'] ) || $force_read ) {
177 $all_args = array(
178 'post_parent' => $product->get_id(),
179 'post_type' => 'product_variation',
180 'orderby' => array(
181 'menu_order' => 'ASC',
182 'ID' => 'ASC',
183 ),
184 'fields' => 'ids',
185 'post_status' => array( ProductStatus::PUBLISH, ProductStatus::PRIVATE ),
186 'numberposts' => -1, // phpcs:ignore WordPress.VIP.PostsPerPage.posts_per_page_numberposts
187 );
188
189 $visible_only_args = $all_args;
190 $visible_only_args['post_status'] = ProductStatus::PUBLISH;
191
192 if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
193 $visible_only_args['tax_query'][] = array(
194 'taxonomy' => 'product_visibility',
195 'field' => 'name',
196 'terms' => ProductStockStatus::OUT_OF_STOCK,
197 'operator' => 'NOT IN',
198 );
199 }
200
201 /**
202 * Filters the query arguments used to retrieve variation children of a variable product.
203 *
204 * @since 3.0.0
205 *
206 * @param array $args Query arguments for get_posts().
207 * @param WC_Product $product The variable product object.
208 * @param bool $visible_only True when retrieving only visible variations, false for all variations.
209 */
210 $children['all'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $all_args, $product, false ) );
211
212 // phpcs:disable WooCommerce.Commenting.CommentHooks
213 $children['visible'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $visible_only_args, $product, true ) );
214
215 // Validate the children data before storing it in the transient.
216 if ( $this->validate_children_data( $children, $transient_version ) ) {
217 set_transient( $children_transient_name, $children, DAY_IN_SECONDS * 30 );
218 }
219 }
220
221 $children['all'] = wp_parse_id_list( (array) $children['all'] );
222 $children['visible'] = wp_parse_id_list( (array) $children['visible'] );
223
224 return $children;
225 }
226
227 /**
228 * Loads an array of attributes used for variations, as well as their possible values.
229 *
230 * @param WC_Product $product Product object.
231 *
232 * @return array
233 */
234 public function read_variation_attributes( &$product ) {
235 global $wpdb;
236
237 $variation_attributes = array();
238 $attributes = $product->get_attributes();
239 $child_ids = $product->get_children();
240 $cache_key = WC_Cache_Helper::get_cache_prefix( 'product_' . $product->get_id() ) . 'product_variation_attributes_' . $product->get_id();
241 $cache_group = 'products';
242 $cached_data = wp_cache_get( $cache_key, $cache_group );
243
244 if ( false !== $cached_data ) {
245 return $cached_data;
246 }
247
248 if ( ! empty( $attributes ) ) {
249 foreach ( $attributes as $attribute ) {
250 if ( empty( $attribute['is_variation'] ) ) {
251 continue;
252 }
253
254 // Get possible values for this attribute, for only visible variations.
255 if ( ! empty( $child_ids ) ) {
256 $format = array_fill( 0, count( $child_ids ), '%d' );
257 $query_in = '(' . implode( ',', $format ) . ')';
258 $query_args = array( 'attribute_name' => wc_variation_attribute_name( $attribute['name'] ) ) + $child_ids;
259 $values = array_unique(
260 $wpdb->get_col(
261 $wpdb->prepare(
262 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
263 "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN {$query_in}", // @codingStandardsIgnoreLine.
264 $query_args
265 )
266 )
267 );
268 } else {
269 $values = array();
270 }
271
272 // Empty value indicates that all options for given attribute are available.
273 if ( in_array( null, $values, true ) || in_array( '', $values, true ) || empty( $values ) ) {
274 $values = $attribute['is_taxonomy'] ? wc_get_object_terms( $product->get_id(), $attribute['name'], 'slug' ) : wc_get_text_attributes( $attribute['value'] );
275 // Get custom attributes (non taxonomy) as defined.
276 } elseif ( ! $attribute['is_taxonomy'] ) {
277 $text_attributes = wc_get_text_attributes( $attribute['value'] );
278 $assigned_text_attributes = $values;
279 $values = array();
280
281 // Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
282 if ( version_compare( get_post_meta( $product->get_id(), '_product_version', true ), '2.4.0', '<' ) ) {
283 $assigned_text_attributes = array_map( 'sanitize_title', $assigned_text_attributes );
284 foreach ( $text_attributes as $text_attribute ) {
285 if ( in_array( sanitize_title( $text_attribute ), $assigned_text_attributes, true ) ) {
286 $values[] = $text_attribute;
287 }
288 }
289 } else {
290 foreach ( $text_attributes as $text_attribute ) {
291 if ( in_array( $text_attribute, $assigned_text_attributes, true ) ) {
292 $values[] = $text_attribute;
293 }
294 }
295 }
296 }
297 $variation_attributes[ $attribute['name'] ] = array_unique( $values );
298 }
299 }
300
301 wp_cache_set( $cache_key, $variation_attributes, $cache_group );
302
303 return $variation_attributes;
304 }
305
306 /**
307 * 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.
308 *
309 * 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.
310 * This is to ensure modified prices are not cached, unless intended.
311 *
312 * @param WC_Product $product Product object.
313 * @param bool $for_display If true, prices will be adapted for display based on the `woocommerce_tax_display_shop` setting (including or excluding taxes).
314 *
315 * @return array of prices
316 * @since 3.0.0
317 */
318 public function read_price_data( &$product, $for_display = false ) {
319 /**
320 * Transient name for storing prices for this product (note: Max transient length is 45)
321 *
322 * @since 2.5.0 a single transient is used per product for all prices, rather than many transients per product.
323 */
324 $transient_name = 'wc_var_prices_' . $product->get_id();
325 $transient_version = WC_Cache_Helper::get_transient_version( 'product' );
326 $price_hash = $this->get_price_hash( $product, $for_display );
327 $opposite_price_hash = $this->taxes_influence_price( $product ) ? null : $this->get_price_hash( $product, ! $for_display );
328
329 /**
330 * $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.
331 * If the value has already been generated, we don't need to grab the values again so just return them. They are already filtered.
332 */
333 if ( empty( $this->prices_array[ $price_hash ] ) ) {
334 $transient_cached_prices_array = array_filter( (array) json_decode( strval( get_transient( $transient_name ) ), true ) );
335
336 // If the prices are not valid, reset the transient cache.
337 if ( ! $this->validate_prices_data( $transient_cached_prices_array, $transient_version ) ) {
338 $transient_cached_prices_array = array();
339 }
340
341 // If the prices are not stored for this hash, generate them and add to the transient.
342 // Check also the opposite price hash as it may have changed (see get_price_hash).
343 if ( empty( $transient_cached_prices_array[ $price_hash ] ) ||
344 ( ! is_null( $opposite_price_hash ) && ( $opposite_price_hash !== $price_hash ) && empty( $transient_cached_prices_array[ $opposite_price_hash ] ) ) ) {
345 $prices_array = array(
346 'price' => array(),
347 'regular_price' => array(),
348 'sale_price' => array(),
349 );
350
351 $variation_ids = $product->get_visible_children();
352
353 if ( ! empty( $variation_ids ) ) {
354 // Prime caches to reduce future queries.
355 _prime_post_caches( $variation_ids );
356 }
357
358 $tax_display_mode = $for_display ? get_option( 'woocommerce_tax_display_shop' ) : null;
359 $price_decimals = wc_get_price_decimals();
360 foreach ( $variation_ids as $variation_id ) {
361 $variation = wc_get_product( $variation_id );
362
363 if ( $variation ) {
364 /**
365 * Filters the active price for a product variation before it is used in price calculations and caching.
366 *
367 * IMPORTANT: If your filter returns a price that varies by ANY factor (e.g., current user,
368 * time of day, user role, cart contents, etc.), you MUST also hook into the
369 * 'woocommerce_get_variation_prices_hash' filter to include that factor in the cache key.
370 * Otherwise, the wrong price may be cached and displayed to different users or contexts.
371 *
372 * Example: Applying a per-user discount and adapting the price hash info accordingly:
373 *
374 * class My_Custom_Pricing {
375 * public function __construct() {
376 * add_filter( 'woocommerce_variation_prices_price', array( $this, 'apply_user_discount' ), 10, 3 );
377 * add_filter( 'woocommerce_get_variation_prices_hash', array( $this, 'add_user_to_hash' ), 10, 3 );
378 * }
379 *
380 * public function apply_user_discount( $price, $variation, $product ) {
381 * return $price * $this->get_discount_for_user( get_current_user_id() );
382 * }
383 *
384 * public function add_user_to_hash( $price_hash, $product, $for_display ) {
385 * $price_hash[] = get_current_user_id();
386 * return $price_hash;
387 * }
388 * }
389 *
390 * @since 3.0.0
391 *
392 * @param string|float $price The variation's active price.
393 * @param WC_Product $variation The variation product object.
394 * @param WC_Product $product The parent variable product object.
395 */
396 $price = apply_filters( 'woocommerce_variation_prices_price', $variation->get_price( 'edit' ), $variation, $product );
397
398 // Skip empty prices.
399 if ( '' === $price ) {
400 continue;
401 }
402
403 /**
404 * Filters the regular price for a product variation before it is used in price calculations and caching.
405 *
406 * IMPORTANT: see the documentation for the 'woocommerce_variation_prices_price' filter.
407 *
408 * @since 3.0.0
409 *
410 * @param string|float $regular_price The variation's regular price.
411 * @param WC_Product $variation The variation product object.
412 * @param WC_Product $product The parent variable product object.
413 */
414 $regular_price = apply_filters( 'woocommerce_variation_prices_regular_price', $variation->get_regular_price( 'edit' ), $variation, $product );
415
416 /**
417 * Filters the sale price for a product variation before it is used in price calculations and caching.
418 *
419 * IMPORTANT: see the documentation for the 'woocommerce_variation_prices_price' filter.
420 *
421 * @since 3.0.0
422 *
423 * @param string|float $sale_price The variation's sale price.
424 * @param WC_Product $variation The variation product object.
425 * @param WC_Product $product The parent variable product object.
426 */
427 $sale_price = apply_filters( 'woocommerce_variation_prices_sale_price', $variation->get_sale_price( 'edit' ), $variation, $product );
428
429 // If sale price does not equal price, the product is not yet on sale.
430 if ( $sale_price === $regular_price || $sale_price !== $price ) {
431 $sale_price = $regular_price;
432 }
433
434 // If we are getting prices for display, we need to account for taxes.
435 if ( $for_display ) {
436 if ( 'incl' === $tax_display_mode ) {
437 $price = '' === $price ? '' : wc_get_price_including_tax(
438 $variation,
439 array(
440 'qty' => 1,
441 'price' => $price,
442 )
443 );
444 $regular_price = '' === $regular_price ? '' : wc_get_price_including_tax(
445 $variation,
446 array(
447 'qty' => 1,
448 'price' => $regular_price,
449 )
450 );
451 $sale_price = '' === $sale_price ? '' : wc_get_price_including_tax(
452 $variation,
453 array(
454 'qty' => 1,
455 'price' => $sale_price,
456 )
457 );
458 } else {
459 $price = '' === $price ? '' : wc_get_price_excluding_tax(
460 $variation,
461 array(
462 'qty' => 1,
463 'price' => $price,
464 )
465 );
466 $regular_price = '' === $regular_price ? '' : wc_get_price_excluding_tax(
467 $variation,
468 array(
469 'qty' => 1,
470 'price' => $regular_price,
471 )
472 );
473 $sale_price = '' === $sale_price ? '' : wc_get_price_excluding_tax(
474 $variation,
475 array(
476 'qty' => 1,
477 'price' => $sale_price,
478 )
479 );
480 }
481 }
482
483 $prices_array['price'][ $variation_id ] = wc_format_decimal( $price, $price_decimals );
484 $prices_array['regular_price'][ $variation_id ] = wc_format_decimal( $regular_price, $price_decimals );
485 $prices_array['sale_price'][ $variation_id ] = wc_format_decimal( $sale_price, $price_decimals );
486
487 if ( has_filter( 'woocommerce_variation_prices_array' ) ) {
488 $original_prices_array = $prices_array;
489
490 /**
491 * Filter the variation prices array before storing in transient cache.
492 *
493 * This filter allows developers to modify the variation prices array for each variation
494 * during the price calculation process. It's called for each variation individually
495 * and can be used to add custom pricing data or modify existing prices.
496 *
497 * @since 3.6.0
498 *
499 * @param array $prices_array The prices array being built. Contains 'price', 'regular_price', and 'sale_price' keys.
500 * @param WC_Product $variation The variation product object.
501 * @param bool $for_display Whether prices are for display (with tax adjustments) or for calculations.
502 */
503 $prices_array = apply_filters( 'woocommerce_variation_prices_array', $prices_array, $variation, $for_display );
504 if ( $opposite_price_hash ) {
505 // In principle, we know that prices for display and not for display are the same ones,
506 // but code hooking on woocommerce_variation_prices_array could make this different
507 // so we need to check.
508 $prices_array_hash = md5( wp_json_encode( $prices_array ) );
509 // phpcs:ignore WooCommerce.Commenting.CommentHooks
510 $opposite_prices_array = apply_filters( 'woocommerce_variation_prices_array', $original_prices_array, $variation, ! $for_display );
511 $opposite_prices_array_hash = md5( wp_json_encode( $opposite_prices_array ) );
512 if ( $opposite_prices_array_hash !== $prices_array_hash ) {
513 $opposite_price_hash = null;
514 }
515 }
516 }
517 }
518 }
519
520 // Add all pricing data to the transient array.
521 foreach ( $prices_array as $key => $values ) {
522 $transient_cached_prices_array[ $price_hash ][ $key ] = $values;
523 if ( ! is_null( $opposite_price_hash ) && $opposite_price_hash !== $price_hash ) {
524 $transient_cached_prices_array[ $opposite_price_hash ][ $key ] = $values;
525 }
526 }
527
528 // Validate the prices data before storing it in the transient.
529 if ( $this->validate_prices_data( $transient_cached_prices_array, $transient_version ) ) {
530 set_transient( $transient_name, wp_json_encode( $transient_cached_prices_array ), DAY_IN_SECONDS * 30 );
531 }
532 }
533
534 /**
535 * Filters the variation prices array for a variable product.
536 *
537 * This filter gives plugins one last chance to modify the variation prices array which has been
538 * generated and will be stored locally to the class. This value may differ from the transient cache.
539 * It is filtered once before storing locally.
540 *
541 * @since 3.0.0
542 *
543 * @param array $prices_array {
544 * Associative array of variation prices indexed by variation ID.
545 *
546 * @type array $price Array of active prices (variation_id => price).
547 * @type array $regular_price Array of regular prices (variation_id => price).
548 * @type array $sale_price Array of sale prices (variation_id => price).
549 * }
550 * @param WC_Product $product The variable product object.
551 * @param bool $for_display Whether prices are being retrieved for display.
552 */
553 $this->prices_array[ $price_hash ] = apply_filters( 'woocommerce_variation_prices', $transient_cached_prices_array[ $price_hash ], $product, $for_display );
554 if ( ! is_null( $opposite_price_hash ) && $opposite_price_hash !== $price_hash ) {
555 // phpcs:ignore WooCommerce.Commenting.CommentHooks
556 $this->prices_array[ $opposite_price_hash ] = apply_filters( 'woocommerce_variation_prices', $transient_cached_prices_array[ $opposite_price_hash ], $product, ! $for_display );
557 }
558 }
559 return $this->prices_array[ $price_hash ];
560 }
561
562 /**
563 * Check if the prices for a product will be different with or without taxes.
564 *
565 * @param WC_Product $product Product to check.
566 * @return bool True if the prices will be different with or without taxes.
567 *
568 * @since 10.4.0
569 */
570 protected function taxes_influence_price( $product ): bool {
571 if ( ! $product->is_taxable() ) {
572 return false;
573 }
574
575 if ( empty( WC_Tax::get_rates( $product->get_tax_class() ) ) ) {
576 return false;
577 }
578
579 // Taxes influence the price regardless of VAT exempt status. Even when a
580 // customer is VAT exempt, the displayed prices differ from non-exempt
581 // prices, so they need separate cache entries and the opposite_price_hash
582 // optimization should not apply. Returning false here was causing cached
583 // non-exempt prices to be served to VAT exempt customers.
584 // See: https://github.com/woocommerce/woocommerce/issues/63716
585
586 return true;
587 }
588
589 /**
590 * Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters.
591 * DEVELOPERS should filter this hash if offering conditional pricing to keep it unique.
592 *
593 * @param WC_Product $product Product object.
594 * @param bool $for_display If taxes should be calculated or not.
595 *
596 * @since 3.0.0
597 * @return string
598 */
599 protected function get_price_hash( &$product, $for_display = false ) {
600 $price_hash = array( false );
601
602 if ( $for_display && wc_tax_enabled() ) {
603 $price_hash = array(
604 get_option( 'woocommerce_tax_display_shop', 'excl' ),
605 WC_Tax::get_rates(),
606 empty( WC()->customer ) ? false : WC()->customer->is_vat_exempt(),
607 );
608 }
609
610 $filter_names = array( 'woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price' );
611
612 /**
613 * Filters whether to use the legacy callback serialization algorithm.
614 *
615 * By default, WooCommerce will use the legacy algorithm to get the callback signatures
616 * for variation price hash calculation. This algorithm serializes the entire callback
617 * array as it comes from $wp_filter, which means that for callbacks that are class methods
618 * the entire object will be serialized, including the current values of the class variables.
619 * This implies that a change in these variables will change the price hash,
620 * even if they do not affect the price calculation.
621 *
622 * This filter allows using CallbackUtil instead, which generates a more stable signature
623 * that does not depend on the internal state of objects, but only on the method names and
624 * class names. This results in a more consistent and reliable price hash, reducing unnecessary
625 * cache misses; but can cause compatibility issues with plugins that rely on the legacy behavior.
626 *
627 * IMPORTANT: see also the documentation for the 'woocommerce_variation_prices_price' filter.
628 *
629 * @since 10.5.0
630 *
631 * @param bool $use_legacy True to use the legacy algorithm (default), false to use CallbackUtil
632 * @param WC_Product $product The product object.
633 * @param bool $for_display If taxes should be calculated or not.
634 */
635 $use_legacy_algorithm = apply_filters( 'woocommerce_use_legacy_get_variations_price_hash', true, $product, $for_display );
636
637 if ( $use_legacy_algorithm ) {
638 global $wp_filter;
639
640 foreach ( $filter_names as $filter_name ) {
641 if ( ! empty( $wp_filter[ $filter_name ] ) ) {
642 $price_hash[ $filter_name ] = array();
643
644 foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) {
645 $price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) );
646 }
647 }
648 }
649 } else {
650 foreach ( $filter_names as $filter_name ) {
651 $signatures = CallbackUtil::get_hook_callback_signatures( $filter_name );
652 if ( ! empty( $signatures ) ) {
653 $price_hash[ $filter_name ] = $signatures;
654 }
655 }
656 }
657
658 /**
659 * Filters the hash used for caching variation prices.
660 *
661 * IMPORTANT: see the documentation for the 'woocommerce_variation_prices_price' filter.
662 *
663 * @since 2.5.0
664 *
665 * @param array $price_hash Array of factors used to generate the cache key hash.
666 * @param WC_Product $product The variable product object.
667 * @param bool $for_display Whether prices are for display (with tax adjustments) or calculations.
668 */
669 $price_hash = apply_filters( 'woocommerce_get_variation_prices_hash', $price_hash, $product, $for_display );
670
671 return md5( wp_json_encode( $price_hash ) );
672 }
673
674 /**
675 * Does a child have a weight set?
676 *
677 * @param WC_Product $product Product object.
678 *
679 * @since 3.0.0
680 * @return boolean
681 */
682 public function child_has_weight( $product ) {
683 global $wpdb;
684 $children = $product->get_visible_children();
685 if ( ! $children ) {
686 return false;
687 }
688
689 $format = array_fill( 0, count( $children ), '%d' );
690 $query_in = '(' . implode( ',', $format ) . ')';
691
692 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
693 return null !== $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_weight' AND meta_value > 0 AND post_id IN {$query_in}", $children ) );
694 }
695
696 /**
697 * Does a child have dimensions set?
698 *
699 * @param WC_Product $product Product object.
700 *
701 * @since 3.0.0
702 * @return boolean
703 */
704 public function child_has_dimensions( $product ) {
705 global $wpdb;
706 $children = $product->get_visible_children();
707 if ( ! $children ) {
708 return false;
709 }
710
711 $format = array_fill( 0, count( $children ), '%d' );
712 $query_in = '(' . implode( ',', $format ) . ')';
713
714 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
715 return null !== $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key IN ( '_length', '_width', '_height' ) AND meta_value > 0 AND post_id IN {$query_in}", $children ) );
716 }
717
718 /**
719 * Is a child in stock?
720 *
721 * @param WC_Product $product Product object.
722 *
723 * @since 3.0.0
724 * @return boolean
725 */
726 public function child_is_in_stock( $product ) {
727 return $this->child_has_stock_status( $product, ProductStockStatus::IN_STOCK );
728 }
729
730 /**
731 * Does a child have a stock status?
732 *
733 * @param WC_Product $product Product object.
734 * @param string $status 'instock', 'outofstock', or 'onbackorder'.
735 *
736 * @since 3.3.0
737 * @return boolean
738 */
739 public function child_has_stock_status( $product, $status ) {
740 global $wpdb;
741
742 $children = $product->get_children();
743
744 if ( $children ) {
745 $format = array_fill( 0, count( $children ), '%d' );
746 $query_in = '(' . implode( ',', $format ) . ')';
747 $query_args = array( 'stock_status' => $status ) + $children;
748 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
749 if ( get_option( 'woocommerce_product_lookup_table_is_generating' ) ) {
750 $query = "SELECT COUNT( post_id ) FROM {$wpdb->postmeta} WHERE meta_key = '_stock_status' AND meta_value = %s AND post_id IN {$query_in}";
751 } else {
752 $query = "SELECT COUNT( product_id ) FROM {$wpdb->wc_product_meta_lookup} WHERE stock_status = %s AND product_id IN {$query_in}";
753 }
754 $children_with_status = $wpdb->get_var(
755 $wpdb->prepare(
756 $query,
757 $query_args
758 )
759 );
760 // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
761 } else {
762 $children_with_status = 0;
763 }
764
765 return (bool) $children_with_status;
766 }
767
768 /**
769 * Syncs all variation names if the parent name is changed.
770 *
771 * @param WC_Product $product Product object.
772 * @param string $previous_name Variation previous name.
773 * @param string $new_name Variation new name.
774 *
775 * @since 3.0.0
776 */
777 public function sync_variation_names( &$product, $previous_name = '', $new_name = '' ) {
778 if ( $new_name !== $previous_name ) {
779 global $wpdb;
780
781 $wpdb->query(
782 $wpdb->prepare(
783 "UPDATE {$wpdb->posts}
784 SET post_title = REPLACE( post_title, %s, %s )
785 WHERE post_type = 'product_variation'
786 AND post_parent = %d",
787 $previous_name ? $previous_name : 'AUTO-DRAFT',
788 $new_name,
789 $product->get_id()
790 )
791 );
792
793 $invalidator = wc_get_container()->get( ProductVersionStringInvalidator::class );
794 $children = $product->get_children();
795 foreach ( $children as $child_id ) {
796 $invalidator->invalidate( $child_id );
797 }
798 $invalidator->invalidate( $product->get_id() );
799 }
800 }
801
802 /**
803 * Stock managed at the parent level - update children being managed by this product.
804 * This sync function syncs downwards (from parent to child) when the variable product is saved.
805 *
806 * @param WC_Product $product Product object.
807 *
808 * @since 3.0.0
809 */
810 public function sync_managed_variation_stock_status( &$product ) {
811 global $wpdb;
812
813 if ( $product->get_manage_stock() ) {
814 $children = $product->get_children();
815 $changed = false;
816 $invalidator = wc_get_container()->get( ProductVersionStringInvalidator::class );
817
818 if ( $children ) {
819 $status = $product->get_stock_status();
820 $format = array_fill( 0, count( $children ), '%d' );
821 $query_in = '(' . implode( ',', $format ) . ')';
822 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
823 $managed_children = array_unique( $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_manage_stock' AND meta_value != 'yes' AND post_id IN {$query_in}", $children ) ) );
824 foreach ( $managed_children as $managed_child ) {
825 if ( update_post_meta( $managed_child, '_stock_status', $status ) ) {
826 $this->update_lookup_table( $managed_child, 'wc_product_meta_lookup' );
827 $changed = true;
828
829 $invalidator->invalidate( $managed_child );
830 }
831 }
832 }
833
834 if ( $changed ) {
835 $children = $this->read_children( $product, true );
836 $product->set_children( $children['all'] );
837 $product->set_visible_children( $children['visible'] );
838
839 $invalidator->invalidate( $product->get_id() );
840 }
841 }
842 }
843
844 /**
845 * Sync variable product prices with children.
846 *
847 * @param WC_Product $product Product object.
848 *
849 * @since 3.0.0
850 */
851 public function sync_price( &$product ) {
852 global $wpdb;
853
854 $children = $product->get_visible_children();
855 if ( $children ) {
856 $format = array_fill( 0, count( $children ), '%d' );
857 $query_in = '(' . implode( ',', $format ) . ')';
858 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
859 $prices = array_unique( $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_price' AND post_id IN {$query_in}", $children ) ) );
860 } else {
861 $prices = array();
862 }
863
864 delete_post_meta( $product->get_id(), '_price' );
865 delete_post_meta( $product->get_id(), '_sale_price' );
866 delete_post_meta( $product->get_id(), '_regular_price' );
867
868 if ( $prices ) {
869 sort( $prices, SORT_NUMERIC );
870 // To allow sorting and filtering by multiple values, we have no choice but to store child prices in this manner.
871 foreach ( $prices as $price ) {
872 if ( is_null( $price ) || '' === $price ) {
873 continue;
874 }
875 add_post_meta( $product->get_id(), '_price', $price, false );
876 }
877 }
878
879 $this->update_lookup_table( $product->get_id(), 'wc_product_meta_lookup' );
880
881 /**
882 * Fire an action for this direct update so it can be detected by other code.
883 *
884 * @since 3.6
885 * @param int $product_id Product ID that was updated directly.
886 */
887 do_action( 'woocommerce_updated_product_price', $product->get_id() );
888 }
889
890 /**
891 * Sync variable product stock status with children.
892 * Change does not persist unless saved by caller.
893 *
894 * @param WC_Product $product Product object.
895 *
896 * @since 3.0.0
897 */
898 public function sync_stock_status( &$product ) {
899 if ( $product->child_is_in_stock() ) {
900 $product->set_stock_status( ProductStockStatus::IN_STOCK );
901 } elseif ( $product->child_is_on_backorder() ) {
902 $product->set_stock_status( ProductStockStatus::ON_BACKORDER );
903 } else {
904 $product->set_stock_status( ProductStockStatus::OUT_OF_STOCK );
905 }
906 }
907
908 /**
909 * Delete variations of a product.
910 *
911 * @param int $product_id Product ID.
912 * @param bool $force_delete False to trash.
913 *
914 * @since 3.0.0
915 */
916 public function delete_variations( $product_id, $force_delete = false ) {
917 if ( ! is_numeric( $product_id ) || 0 >= $product_id ) {
918 return;
919 }
920
921 $variation_ids = wp_parse_id_list(
922 get_posts(
923 array(
924 'post_parent' => $product_id,
925 'post_type' => 'product_variation',
926 'fields' => 'ids',
927 'post_status' => array( 'any', ProductStatus::TRASH, ProductStatus::AUTO_DRAFT ),
928 'numberposts' => -1, // phpcs:ignore WordPress.VIP.PostsPerPage.posts_per_page_numberposts
929 )
930 )
931 );
932
933 if ( ! empty( $variation_ids ) ) {
934 foreach ( $variation_ids as $variation_id ) {
935 if ( $force_delete ) {
936 /**
937 * Fires before a product variation is permanently deleted.
938 *
939 * @since 3.0.0
940 *
941 * @param int $variation_id The variation ID being deleted.
942 */
943 do_action( 'woocommerce_before_delete_product_variation', $variation_id );
944 wp_delete_post( $variation_id, true );
945
946 /**
947 * Fires after a product variation has been permanently deleted.
948 *
949 * @since 3.0.0
950 *
951 * @param int $variation_id The variation ID that was deleted.
952 */
953 do_action( 'woocommerce_delete_product_variation', $variation_id );
954 } else {
955 wp_trash_post( $variation_id );
956
957 /**
958 * Fires after a product variation has been trashed.
959 *
960 * @since 3.0.0
961 *
962 * @param int $variation_id The variation ID that was trashed.
963 */
964 do_action( 'woocommerce_trash_product_variation', $variation_id );
965 }
966 }
967 }
968
969 delete_transient( 'wc_product_children_' . $product_id );
970 }
971
972 /**
973 * Untrash variations.
974 *
975 * @param int $product_id Product ID.
976 */
977 public function untrash_variations( $product_id ) {
978 $variation_ids = wp_parse_id_list(
979 get_posts(
980 array(
981 'post_parent' => $product_id,
982 'post_type' => 'product_variation',
983 'fields' => 'ids',
984 'post_status' => 'trash',
985 'numberposts' => -1, // phpcs:ignore WordPress.VIP.PostsPerPage.posts_per_page_numberposts
986 )
987 )
988 );
989
990 if ( ! empty( $variation_ids ) ) {
991 foreach ( $variation_ids as $variation_id ) {
992 wp_untrash_post( $variation_id );
993 }
994 }
995
996 delete_transient( 'wc_product_children_' . $product_id );
997 }
998
999 /**
1000 * Validate the children data by checking the structure and type of the data.
1001 *
1002 * @param array $children The children data.
1003 * @param string $deprecated Was the current transient version, unused since 10.3.0.
1004 * @return bool True if valid, false otherwise.
1005 */
1006 protected function validate_children_data( $children, $deprecated ) {
1007 if ( ! is_array( $children ) ) {
1008 return false;
1009 }
1010
1011 // Basic structure checks.
1012 if ( empty( $children['all'] ) || ! isset( $children['visible'] ) ) {
1013 return false;
1014 }
1015
1016 if ( ! is_array( $children['all'] ) || ! is_array( $children['visible'] ) ) {
1017 return false;
1018 }
1019
1020 foreach ( $children['all'] as $id ) {
1021 if ( ! is_numeric( $id ) ) {
1022 return false;
1023 }
1024 }
1025
1026 foreach ( $children['visible'] as $id ) {
1027 if ( ! is_numeric( $id ) ) {
1028 return false;
1029 }
1030 }
1031
1032 return true;
1033 }
1034
1035 /**
1036 * Validate the prices data by checking the structure and type of the data.
1037 *
1038 * @param array $prices_array The prices data.
1039 * @param string $deprecated Was the current transient version, unused since 10.3.0.
1040 * @return bool True if valid, false otherwise.
1041 */
1042 protected function validate_prices_data( $prices_array, $deprecated ) {
1043 if ( ! is_array( $prices_array ) ) {
1044 return false;
1045 }
1046
1047 // Fail if array is empty - we want to rebuild in this case.
1048 if ( empty( $prices_array ) ) {
1049 return false;
1050 }
1051
1052 $price_data_is_empty = true;
1053
1054 foreach ( $prices_array as $price_data ) {
1055 if ( ! is_array( $price_data ) ) {
1056 return false;
1057 }
1058
1059 $required_types = array( 'price', 'regular_price', 'sale_price' );
1060
1061 foreach ( $required_types as $type ) {
1062 // If all 'price' fields are empty, we want to track that so we can rebuild the data.
1063 if ( 'price' === $type && ! empty( $price_data[ $type ] ) && $price_data_is_empty ) {
1064 $price_data_is_empty = false;
1065 }
1066
1067 if ( ! isset( $price_data[ $type ] ) || ! is_array( $price_data[ $type ] ) ) {
1068 return false;
1069 }
1070 }
1071
1072 $variation_ids = array_keys( $price_data['price'] );
1073
1074 foreach ( $variation_ids as $variation_id ) {
1075 if ( ! is_numeric( $variation_id ) ) {
1076 return false;
1077 }
1078
1079 foreach ( $required_types as $type ) {
1080 if ( ! array_key_exists( $variation_id, $price_data[ $type ] ) ) {
1081 return false;
1082 }
1083
1084 $type_price = $price_data[ $type ][ $variation_id ];
1085 if ( ! is_numeric( $type_price ) && '' !== $type_price ) {
1086 return false;
1087 }
1088 }
1089 }
1090 }
1091
1092 // If price is empty, we want to rebuild the data.
1093 if ( $price_data_is_empty ) {
1094 return false;
1095 }
1096
1097 return true;
1098 }
1099 }
1100