|null>> */ private $variations = array(); /** * Constructor. * * @param PriceLog $log Price log. */ public function __construct( PriceLog $log ) { $this->log = $log; } /** * Adds the hooks. */ public function register() { add_filter( 'woocommerce_get_price_html', array( $this, 'filter_struck_price' ), 5, 2 ); add_filter( 'woocommerce_get_price_html', array( $this, 'filter_price_html' ), 20, 2 ); add_filter( 'woocommerce_sale_flash', array( $this, 'filter_sale_flash' ), 5, 3 ); add_filter( 'woocommerce_sale_flash', array( $this, 'hide_sale_flash' ), 99, 3 ); add_filter( 'woocommerce_sale_badge_text', array( $this, 'filter_badge_text' ), 5, 2 ); add_filter( 'render_block_woocommerce/product-sale-badge', array( $this, 'filter_badge_block' ), 10, 3 ); add_filter( 'woocommerce_available_variation', array( $this, 'filter_variation' ), 20, 3 ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_shortcode( self::SHORTCODE, array( $this, 'shortcode' ) ); } /** * Registers the front end style and script. */ public function register_assets() { if ( wp_style_is( 'eu-check', 'registered' ) ) { return; } wp_register_style( 'eu-check', EUCHECK_URL . 'assets/css/front.min.css', array(), EUCHECK_VERSION ); wp_register_script( 'eu-check-variations', EUCHECK_URL . 'assets/js/variations.min.js', array( 'jquery' ), EUCHECK_VERSION, array( 'in_footer' => true ) ); } /** * Loads the front end style where the price filter adds lines. */ public function enqueue_assets() { $this->register_assets(); if ( ! Settings::get( 'enabled' ) || 'auto' !== Settings::get( 'placement' ) ) { return; } // Product lists can sit on any page (blocks, widgets), so with lists // on the small style loads everywhere; otherwise on product pages only. if ( Settings::get( 'archives' ) || ( function_exists( 'is_product' ) && is_product() ) ) { wp_enqueue_style( 'eu-check' ); } } /** * Appends the lines to a discounted price. * * @param string $html Price HTML. * @param mixed $product Product. * @return string */ public function filter_price_html( $html, $product ) { if ( ! Settings::get( 'enabled' ) || 'auto' !== Settings::get( 'placement' ) ) { return $html; } if ( ! $product instanceof \WC_Product || $product->is_type( array( 'variable', 'grouped', 'variation' ) ) ) { // Variations get their lines through filter_variation(), so they // follow the variation picker. return $html; } if ( ! $this->in_context( $product ) ) { return $html; } $lines = $this->lines( $product ); return '' === $lines ? $html : $html . ' ' . $lines; } /** * Makes the prior price the crossed-out price (setting "struck"), the * way Directive 98/6/EC article 6a reads: a reduction is announced from * the lowest price of the 30 days before it. Runs early, so filters * after it still see WooCommerce's markup shape. * * @param string $html Price HTML. * @param mixed $product Product. * @return string */ public function filter_struck_price( $html, $product ) { if ( ! $product instanceof \WC_Product || ! $this->strikes_prior() || $product->is_type( 'grouped' ) ) { return $html; } if ( $product instanceof \WC_Product_Variable ) { return $this->variable_struck_price( $html, $product ); } $amounts = $this->amounts( $product ); if ( null === $amounts ) { // Not on sale, or no proof of the prior price: WooCommerce's markup stays. return $html; } if ( $amounts['prior'] <= $amounts['current'] ) { // The sale price is not below the prior price: no reduction to announce. return wc_price( $amounts['current'] ) . $product->get_price_suffix(); } if ( $amounts['prior'] === $amounts['regular'] ) { return $html; } return wc_format_sale_price( $amounts['prior'], $amounts['current'] ) . $product->get_price_suffix(); } /** * The crossed-out price of a variable product whose variations all cost * the same, the only case where WooCommerce crosses one out. It must be * the prior price of every variation; when they differ, the price shows * alone and the picker shows each variation's own. * * @param string $html Price HTML. * @param \WC_Product_Variable $product Variable product. * @return string */ private function variable_struck_price( $html, \WC_Product_Variable $product ) { if ( ! $product->is_on_sale() ) { return $html; } $prices = $product->get_variation_prices( true ); if ( empty( $prices['price'] ) ) { return $html; } $min = current( $prices['price'] ); $max = end( $prices['price'] ); $min_regular = current( $prices['regular_price'] ); $max_regular = end( $prices['regular_price'] ); if ( $min !== $max || $min_regular !== $max_regular ) { // A price range: WooCommerce crosses nothing out. return $html; } $priors = $this->shared_priors( $product ); if ( null === $priors ) { return $html; } $decimals = wc_get_price_decimals(); $current = round( (float) $min, $decimals ); $suffix = $product->get_price_suffix(); if ( 1 !== count( $priors ) || $priors[0] <= $current ) { return wc_price( $current ) . $suffix; } if ( round( (float) $max_regular, $decimals ) === $priors[0] ) { return $html; } return wc_format_sale_price( $priors[0], $current ) . $suffix; } /** * The distinct displayed prior prices of a variable product's variations * on sale, or null when none of them has a known prior price. When some * are known and others are not, null is added to the list, so the * variations count as differing: the parent then crosses nothing out and * each variation shows its own price (Codex review, 2026-11-12). * * @param \WC_Product_Variable $product Variable product. * @return array|null */ private function shared_priors( \WC_Product_Variable $product ) { $amounts = $this->variation_amounts( $product ); $known = array_filter( $amounts, static function ( $amount ) { return null !== $amount; } ); if ( ! $known ) { return null; } $priors = array_values( array_unique( array_column( $known, 'prior' ), SORT_REGULAR ) ); if ( count( $known ) < count( $amounts ) ) { $priors[] = null; } return $priors; } /** * Displayed amounts of a variable product's variations on sale, by * variation ID (null for one whose prior price is not known). Loads the * variations and their logs in one go, since product lists ask for many. * * @param \WC_Product_Variable $product Variable product. * @return array|null> */ private function variation_amounts( \WC_Product_Variable $product ) { $id = $product->get_id(); if ( isset( $this->variations[ $id ] ) ) { return $this->variations[ $id ]; } $prices = $product->get_variation_prices( true ); $ids = empty( $prices['price'] ) ? array() : array_map( 'intval', array_keys( $prices['price'] ) ); if ( $ids ) { _prime_post_caches( $ids ); /** * Fires before the display reads the price logs of several * products at once, so a filter of eucheck_history_id can load * what it needs in one go. * * @param int[] $ids Product or variation IDs. * @param PriceLog $log The price log. */ do_action( 'eucheck_prime', $ids, $this->log ); $this->log->prime( array_map( array( $this, 'history_id' ), $ids ) ); } $amounts = array(); foreach ( $ids as $variation_id ) { $variation = wc_get_product( $variation_id ); if ( $variation instanceof \WC_Product && $variation->is_on_sale() ) { $amounts[ $variation_id ] = $this->amounts( $variation ); } } $this->variations[ $id ] = $amounts; return $amounts; } /** * Forgets what this request worked out about variable products. */ public function reset() { $this->variations = array(); } /** * WooCommerce's sale badge with the discount in percent, measured from * the prior price (setting "badge"). A badge a theme builds itself is * left alone. * * @param string $html Badge HTML. * @param mixed $post Post. * @param mixed $product Product. * @return string */ public function filter_sale_flash( $html, $post, $product ) { $percent = $product instanceof \WC_Product ? $this->percent( $product ) : null; if ( null === $percent || ! is_string( $html ) || ! preg_match( '#^[^<]*$#', $html ) ) { return $html; } return '' . esc_html( self::percent_text( $percent ) ) . ''; } /** * No sale badge when there is no reduction from the prior price. * * @param string $html Badge HTML. * @param mixed $post Post. * @param mixed $product Product. * @return string */ public function hide_sale_flash( $html, $post, $product ) { return ( $product instanceof \WC_Product && $this->no_reduction( $product ) ) ? '' : $html; } /** * The Product Sale Badge block's text, in percent from the prior price. * * @param string $text Badge text. * @param mixed $product Product. * @return string */ public function filter_badge_text( $text, $product ) { $percent = $product instanceof \WC_Product ? $this->percent( $product ) : null; return null === $percent ? $text : self::percent_text( $percent ); } /** * No Product Sale Badge block when there is no reduction from the prior price. * * @param string $content Block HTML. * @param array $block Parsed block. * @param mixed $instance Block instance. * @return string */ public function filter_badge_block( $content, $block, $instance ) { $post_id = ( $instance instanceof \WP_Block && isset( $instance->context['postId'] ) ) ? (int) $instance->context['postId'] : 0; $product = $post_id ? wc_get_product( $post_id ) : null; return ( $product instanceof \WC_Product && $this->no_reduction( $product ) ) ? '' : $content; } /** * The badge text for a discount in percent. * * @param int $percent Discount in percent. * @return string */ public static function percent_text( $percent ) { /* translators: %d: the discount in percent, measured from the lowest price of the 30 days before it. */ return sprintf( __( '-%d%%', 'eu-check' ), (int) $percent ); } /** * Adds the lines to a variation's data, read by the variation picker. * WooCommerce leaves a variation's price empty when every variation * costs the same; when their prior prices differ, the parent shows no * crossed-out price, so each variation gets its own. * * @param array $data Variation data. * @param mixed $variable Variable product. * @param mixed $variation Variation. * @return array */ public function filter_variation( $data, $variable, $variation ) { if ( ! is_array( $data ) || ! Settings::get( 'enabled' ) || ! $variation instanceof \WC_Product ) { return $data; } if ( empty( $data['price_html'] ) && $variable instanceof \WC_Product_Variable && $this->strikes_prior() && $variation->is_on_sale() ) { $priors = $this->shared_priors( $variable ); if ( null !== $priors && count( $priors ) > 1 ) { $data['price_html'] = '' . $variation->get_price_html() . ''; } } $lines = $this->lines( $variation ); $data['eucheck_html'] = $lines; if ( '' !== $lines && 'auto' === Settings::get( 'placement' ) ) { $data['price_html'] = ( isset( $data['price_html'] ) ? $data['price_html'] . ' ' : '' ) . $lines; } return $data; } /** * The [eu_check_lowest_price] shortcode. * * @param array|string $atts Attributes: id (product ID, the current product by default). * @return string */ public function shortcode( $atts ) { $atts = shortcode_atts( array( 'id' => 0 ), $atts, self::SHORTCODE ); return $this->box( self::product( absint( $atts['id'] ) ) ); } /** * The lines in a box, for the shortcode and the block. A variable * product's box is filled by the variation picker. * * @param \WC_Product|null $product Product. * @param string $attrs The block's wrapper attributes, already * escaped and holding the eucheck-box class * (get_block_wrapper_attributes()); empty * for the shortcode. * @return string */ public function box( $product, $attrs = '' ) { $attrs = '' === $attrs ? 'class="eucheck-box"' : $attrs; if ( ! $product instanceof \WC_Product || ! Settings::get( 'enabled' ) ) { return ''; } if ( ! wp_style_is( 'eu-check', 'registered' ) ) { $this->register_assets(); } wp_enqueue_style( 'eu-check' ); if ( $product->is_type( 'variable' ) ) { wp_enqueue_script( 'eu-check-variations' ); return sprintf( '
', $product->get_id(), $attrs ); } $lines = $this->lines( $product ); if ( '' === $lines ) { return ''; } return sprintf( '
%s
', $attrs, $lines ); } /** * The lines for one discounted product or variation, or an empty string. * * @param \WC_Product $product Product or variation. * @return string */ public function lines( \WC_Product $product ) { if ( ! $product->is_on_sale() || $this->no_reduction( $product ) ) { return ''; } $reference = $this->reference( $product ); if ( ! $reference['claim'] ) { return ''; } $prior = $reference['prior']; $lowest = $reference['price']; $parts = array(); if ( null !== $lowest ) { $price = wc_price( wc_get_price_to_display( $product, array( 'price' => (float) $lowest ) ) ); $parts[] = '' . str_replace( '{price}', $price, esc_html( Wording::lowest() ) ) . ''; } if ( Settings::get( 'start_date' ) ) { $start = $prior['start_known'] ? $prior['start'] : null; if ( null === $start ) { $from = $product->get_date_on_sale_from(); $start = $from ? $from->getTimestamp() : null; } if ( null !== $start ) { $date = wp_date( get_option( 'date_format' ), $start ); $parts[] = '' . str_replace( '{date}', esc_html( (string) $date ), esc_html( Wording::since() ) ) . ''; } } if ( ! $parts ) { return ''; } $html = '' . implode( '', $parts ) . ''; /** * Filters the HTML added under a discounted price. * * @param string $html Lines HTML. * @param \WC_Product $product Product or variation. * @param array $prior PriorPrice::compute() result. */ return (string) apply_filters( 'eucheck_lines_html', $html, $product, $prior ); } /** * The prior price of a discounted product or variation, before tax * display: the lowest price of the window, the regular price when the * log has none and the settings say so, or null. * * A window the log does not cover in full (a sale that started less than * the window's days after the log began) counts as no history: the * lowest price since the log began may be higher than a price applied * before it, and showing it could announce a bigger reduction than the * real one. * * In another currency than the base one the log says nothing by itself: * only a filter that knows what was offered in that currency (EU Check * Pro) can answer, and without one no claim is made at all (claim false). * * @param \WC_Product $product Product or variation. * @return array{price: float|null, prior: array, claim: bool} */ private function reference( \WC_Product $product ) { $days = (int) apply_filters( 'eucheck_window_days', 30 ); if ( $this->foreign() ) { /** * Filters the prior price of a product shown in another currency * than the shop's base currency: null to make no claim in that * currency, or an array with price (the prior price in the shown * currency, before tax display rules apply) and prior (in the * shape of PriorPrice::compute()). * * @param array|null $reference Null. * @param \WC_Product $product Product or variation. * @param int $days Window length in days. */ $foreign = apply_filters( 'eucheck_foreign_reference', null, $product, $days ); if ( is_array( $foreign ) && isset( $foreign['price'], $foreign['prior'] ) && is_array( $foreign['prior'] ) ) { return array( 'price' => (float) $foreign['price'], 'prior' => $foreign['prior'], 'claim' => true, ); } return array( 'price' => null, 'prior' => PriorPrice::compute( array(), time(), $days, Settings::chain() ), 'claim' => false, ); } $prior = PriorPrice::compute( $this->log->rows( $this->history_id( $product->get_id() ) ), time(), $days, Settings::chain() ); $lowest = $prior['complete'] ? $prior['lowest'] : null; if ( null === $lowest && 'regular' === Settings::get( 'no_history' ) ) { $regular = $product->get_regular_price(); $lowest = '' === $regular ? null : (float) $regular; } /** * Filters the prior price shown, before tax display rules apply. * * @param float|null $lowest Lowest price, null to show none. * @param \WC_Product $product Product or variation. * @param array $prior PriorPrice::compute() result. */ $lowest = apply_filters( 'eucheck_prior_price', $lowest, $product, $prior ); return array( 'price' => null === $lowest ? null : (float) $lowest, 'prior' => $prior, 'claim' => true, ); } /** * Whether prices are shown in another currency than the shop's base * currency (a currency switcher at work). The log holds base amounts, so * EU Check makes no claim then unless eucheck_foreign_reference answers: * no line, no struck prior price, no percentage, and WooCommerce's own * markup stays. * * @return bool */ private function foreign() { return get_woocommerce_currency() !== PriceLog::currency(); } /** * The product whose price log speaks for this one: itself, unless an * extension maps a translation that shares its prices by construction * to the product it was translated from. * * @param int $id Product or variation ID. * @return int */ public function history_id( $id ) { /** * Filters the product whose price log is read for a product. * * @param int $id Product or variation ID. */ $history = (int) apply_filters( 'eucheck_history_id', (int) $id ); return $history > 0 ? $history : (int) $id; } /** * Whether the crossed-out price is the prior price here: the setting, * on the shop's front end (admin screens keep WooCommerce's own). * * @return bool */ private function strikes_prior() { return Settings::get( 'enabled' ) && 'lowest' === Settings::get( 'struck' ) && ( ! is_admin() || wp_doing_ajax() ); } /** * Whether a discounted product has no reduction to announce: the prior * price is crossed out and the sale price is not below it. A variable * product has none when no variation on sale has one. * * @param \WC_Product $product Product or variation. * @return bool */ private function no_reduction( \WC_Product $product ) { if ( ! $this->strikes_prior() || $product->is_type( 'grouped' ) || ! $product->is_on_sale() ) { return false; } if ( $product instanceof \WC_Product_Variable ) { $amounts = $this->variation_amounts( $product ); if ( ! $amounts ) { return false; } foreach ( $amounts as $amount ) { if ( null === $amount || $amount['prior'] > $amount['current'] ) { return false; } } return true; } $amounts = $this->amounts( $product ); return null !== $amounts && $amounts['prior'] <= $amounts['current']; } /** * The prior, current and regular prices of a discounted product or * variation as shoppers see them (tax display applied, rounded to the * shop's decimals), or null when it is not on sale or the prior price is * not known. Every comparison uses these, so a difference that vanishes * in display never announces a reduction. * * @param \WC_Product $product Product or variation. * @return array{prior: float, current: float, regular: float}|null */ private function amounts( \WC_Product $product ) { if ( '' === $product->get_price() || ! $product->is_on_sale() ) { return null; } $reference = $this->reference( $product )['price']; if ( null === $reference ) { return null; } $decimals = wc_get_price_decimals(); return array( 'prior' => round( (float) wc_get_price_to_display( $product, array( 'price' => $reference ) ), $decimals ), 'current' => round( (float) wc_get_price_to_display( $product ), $decimals ), 'regular' => round( (float) wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), $decimals ), ); } /** * The discount in percent from the prior price, rounded down so it never * claims more than it is, or null when the badge keeps its own text. * * @param \WC_Product $product Product or variation. * @return int|null */ private function percent( \WC_Product $product ) { if ( ! Settings::get( 'enabled' ) || 'percent' !== Settings::get( 'badge' ) || ( is_admin() && ! wp_doing_ajax() ) ) { return null; } if ( $product->is_type( array( 'variable', 'grouped' ) ) ) { return null; } $amounts = $this->amounts( $product ); if ( null === $amounts || $amounts['prior'] <= 0 ) { return null; } $percent = (int) floor( ( $amounts['prior'] - $amounts['current'] ) / $amounts['prior'] * 100 + 1e-9 ); return $percent >= 1 ? $percent : null; } /** * Whether the price being printed is one the settings show lines for. * * @param \WC_Product $product Product. * @return bool */ private function in_context( \WC_Product $product ) { if ( is_admin() && ! wp_doing_ajax() ) { return false; } if ( Settings::get( 'archives' ) ) { return true; } return function_exists( 'is_product' ) && is_product() && (int) get_queried_object_id() === $product->get_id(); } /** * The product to show: by ID, or the current one. * * A product named by ID (shortcode, block attribute) shows only when the * visitor could see it anyway, so a draft or private product's prices * never leak through a post that names it. * * @param int $id Product ID, 0 for the current product. * @return \WC_Product|null */ public static function product( $id = 0 ) { if ( $id ) { $product = wc_get_product( $id ); return ( $product && self::viewable( $product ) ) ? $product : null; } global $product; if ( $product instanceof \WC_Product ) { return $product; } $post_id = get_the_ID(); if ( $post_id && 'product' === get_post_type( $post_id ) ) { $found = wc_get_product( $post_id ); return $found ? $found : null; } return null; } /** * Whether the visitor may see this product: published, or readable by * the current user (a shop manager previewing a draft), and not behind a * password the visitor has not entered. A variation also needs its parent. * * @param \WC_Product $product Product or variation. * @return bool */ private static function viewable( \WC_Product $product ) { $ids = array( $product->get_id() ); if ( $product->get_parent_id() ) { $ids[] = $product->get_parent_id(); } foreach ( $ids as $post_id ) { if ( 'publish' !== get_post_status( $post_id ) && ! current_user_can( 'read_post', $post_id ) ) { return false; } if ( post_password_required( $post_id ) ) { return false; } } return true; } }