is_type( 'variable' ) ) { list( $regular, $sale ) = self::cheapestVariationRegularAndSale( $product ); } elseif ( $product->is_type( 'grouped' ) ) { return self::cheapestChildRegularAndSale( $product ); } else { $regular = (string) $product->get_regular_price(); $sale = $product->is_on_sale() ? (string) $product->get_sale_price() : ''; } if ( '' !== $sale && '' !== $regular && (float) $sale >= (float) $regular ) { $sale = ''; } return array( 'regular' => $regular, 'sale' => $sale, ); } /** * The cheapest variation's own regular and sale price. WooCommerce sorts * each of get_variation_prices()' lists on its own, so the lowest regular * price and the lowest active price can belong to two different * variations; reading them separately would describe a discount no * variation has. A variation that is not on sale carries its regular * price as its sale price, which regularAndSale() then reports as no sale. * * @param \WC_Product $product A variable product. * @return array{0: string, 1: string} Regular and sale price, '' when there is none. */ private static function cheapestVariationRegularAndSale( \WC_Product $product ): array { $prices = (array) $product->get_variation_prices(); $cheapest = null; $lowest = null; foreach ( (array) ( $prices['price'] ?? array() ) as $variation_id => $price ) { if ( '' === (string) $price ) { continue; } if ( null === $lowest || (float) $price < $lowest ) { $cheapest = $variation_id; $lowest = (float) $price; } } if ( null === $cheapest ) { return array( '', '' ); } return array( (string) ( $prices['regular_price'][ $cheapest ] ?? '' ), (string) ( $prices['sale_price'][ $cheapest ] ?? '' ), ); } /** * A grouped product's children as a shopper sees them, for its price * range, its composition and any copy that names what it includes: the * published ones. WooCommerce's grouped list and price range show a * visitor exactly these (a published child hidden from the catalog, or * out of stock, still appears in the group). Deliberately not * WC_Product::is_viewable(): that also counts a draft or private child * for whoever can edit it, so an index rebuilt or copy generated by an * admin would name it, and it only exists from WooCommerce 11.1. * * @param \WC_Product $product A grouped product (is_type('grouped') true). * @return \WC_Product[] Published children, in their stored order. */ public static function visibleGroupedChildren( \WC_Product $product ): array { $children = array(); foreach ( (array) $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); if ( $child instanceof \WC_Product && 'publish' === $child->get_status() ) { $children[] = $child; } } return $children; } /** * The cheapest visible child's own regular/sale pair, or empty when the * grouped product has no visible, priced child at all. * * @param \WC_Product $product A grouped product. * @return array{regular: string, sale: string} */ private static function cheapestChildRegularAndSale( \WC_Product $product ): array { $cheapest = null; $cheapest_price = null; foreach ( self::visibleGroupedChildren( $product ) as $child ) { $price = $child->get_price(); if ( '' === $price ) { continue; } $price_value = (float) $price; if ( null === $cheapest_price || $price_value < $cheapest_price ) { $cheapest_price = $price_value; $cheapest = $child; } } return $cheapest instanceof \WC_Product ? self::regularAndSale( $cheapest ) : array( 'regular' => '', 'sale' => '', ); } /** * Whether a variable product's active sale covers every purchasable * variation or only some of them, and -- when only some, and a single * attribute value sets those apart from the rest (e.g. one color out of * several) -- that value. Only meaningful to call once regularAndSale() * already reports a sale; a product with fewer than two purchasable * variations, or where they all agree, is never "mixed". * * A customer told a flat "yes, on sale" for an option this does not * cover would be told something the data never supported -- the caller * should hedge instead for that option, never confirm or deny it. * * @param \WC_Product $product A variable product (is_type('variable') true). * @return array{mixed: bool, cheap_label: string} */ public static function variationSaleScope( \WC_Product $product ): array { $variations = array(); foreach ( (array) $product->get_available_variations( 'objects' ) as $variation ) { if ( $variation instanceof \WC_Product && $variation->is_purchasable() ) { $variations[] = $variation; } } if ( count( $variations ) < 2 ) { return array( 'mixed' => false, 'cheap_label' => '', ); } $on_sale = array(); $off_sale = array(); foreach ( $variations as $variation ) { if ( $variation->is_on_sale() ) { $on_sale[] = $variation; } else { $off_sale[] = $variation; } } if ( empty( $on_sale ) || empty( $off_sale ) ) { return array( 'mixed' => false, 'cheap_label' => '', ); } return array( 'mixed' => true, 'cheap_label' => self::sharedAttributeLabel( $on_sale, $off_sale ), ); } /** * The one attribute value shared by every on-sale variation and absent * from every off-sale one (e.g. "Heather Grey" when only that color is * discounted), in its display form. Empty when no single attribute * value marks the sale group apart -- several attributes vary together, * or the split does not line up with any one attribute's values. * * @param \WC_Product[] $on_sale_variations Purchasable variations on sale. * @param \WC_Product[] $off_sale_variations Purchasable variations not on sale. * @return string */ private static function sharedAttributeLabel( array $on_sale_variations, array $off_sale_variations ): string { $on_sale_common = self::commonRawAttributes( $on_sale_variations ); $off_sale_common = self::commonRawAttributes( $off_sale_variations ); foreach ( $on_sale_common as $attribute => $raw_value ) { if ( isset( $off_sale_common[ $attribute ] ) && $off_sale_common[ $attribute ] === $raw_value ) { continue; } if ( ! taxonomy_exists( $attribute ) ) { return (string) $raw_value; } $variation = reset( $on_sale_variations ); return is_callable( array( $variation, 'get_attribute' ) ) ? (string) $variation->get_attribute( $attribute ) : (string) $raw_value; } return ''; } /** * Attribute name => raw stored value, for every attribute that has the * SAME value across all of the given variations (an attribute that * varies between them is left out). Raw as WooCommerce stores it on a * variation -- a taxonomy attribute's value is its term slug, not its * display name. * * @param \WC_Product[] $variations Variations to intersect. * @return array */ private static function commonRawAttributes( array $variations ): array { if ( empty( $variations ) ) { return array(); } $first = reset( $variations ); $common = array(); foreach ( $first->get_attributes() as $name => $value ) { if ( '' !== $value ) { $common[ $name ] = $value; } } foreach ( $variations as $variation ) { $attributes = $variation->get_attributes(); foreach ( $common as $name => $value ) { if ( ! isset( $attributes[ $name ] ) || $attributes[ $name ] !== $value ) { unset( $common[ $name ] ); } } } return $common; } }