From 71b6e74615136f5c336c8c75d8051d4d297af463 Mon Sep 17 00:00:00 2001 From: Kazuto Takeshita Date: Thu, 15 Feb 2024 11:58:21 +0900 Subject: [PATCH] Make the structured data price of group products refer to the latest child product price (#42808) * Changed Make the structured data price of group products refer to the latest child product price. * Add changelog * PHPCS fixes --------- Co-authored-by: Jorge Torres --- plugins/woocommerce/changelog/issue-27941 | 4 +++ .../includes/class-wc-structured-data.php | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 plugins/woocommerce/changelog/issue-27941 diff --git a/plugins/woocommerce/changelog/issue-27941 b/plugins/woocommerce/changelog/issue-27941 new file mode 100644 index 00000000000..e3cb0c6ce7c --- /dev/null +++ b/plugins/woocommerce/changelog/issue-27941 @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add structured metadata to grouped products. diff --git a/plugins/woocommerce/includes/class-wc-structured-data.php b/plugins/woocommerce/includes/class-wc-structured-data.php index a2cabfa47c8..213fb9788f3 100644 --- a/plugins/woocommerce/includes/class-wc-structured-data.php +++ b/plugins/woocommerce/includes/class-wc-structured-data.php @@ -241,6 +241,36 @@ class WC_Structured_Data { 'offerCount' => count( $product->get_children() ), ); } + } elseif ( $product->is_type( 'grouped' ) ) { + if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) { + $price_valid_until = gmdate( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() ); + } + + $tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); + $children = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' ); + $price_function = 'incl' === $tax_display_mode ? 'wc_get_price_including_tax' : 'wc_get_price_excluding_tax'; + + foreach ( $children as $child ) { + if ( '' !== $child->get_price() ) { + $child_prices[] = $price_function( $child ); + } + } + if ( empty( $child_prices ) ) { + $min_price = 0; + } else { + $min_price = min( $child_prices ); + } + + $markup_offer = array( + '@type' => 'Offer', + 'price' => wc_format_decimal( $min_price, wc_get_price_decimals() ), + 'priceValidUntil' => $price_valid_until, + 'priceSpecification' => array( + 'price' => wc_format_decimal( $min_price, wc_get_price_decimals() ), + 'priceCurrency' => $currency, + 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false', + ), + ); } else { if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) { $price_valid_until = gmdate( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() );