category > type > WooCommerce default). * * @package Hardani\AddToCartText */ declare( strict_types=1 ); namespace Hardani\AddToCartText; defined( 'ABSPATH' ) || exit; /** * Front-end filters and the resolution logic shared with the ability. */ final class Text_Resolver { /** * Per-request cache of category resolutions, keyed by "product_id:context". * * @var array */ private array $category_cache = array(); /** * Constructor. * * @param Settings $settings Settings repository. */ public function __construct( private readonly Settings $settings ) {} /** * Registers the WooCommerce filters. * * @return void */ public function register(): void { add_filter( 'woocommerce_product_single_add_to_cart_text', $this->single_text( ... ), 20, 2 ); add_filter( 'woocommerce_booking_single_add_to_cart_text', $this->single_text( ... ), 20, 2 ); add_filter( 'woocommerce_product_add_to_cart_text', $this->archive_text( ... ), 20, 2 ); add_filter( 'wc_add_to_cart_message_html', $this->added_message( ... ), 20, 2 ); } /** * Filter: button text on single product pages. * * @param mixed $text Text from WooCommerce. * @param mixed $product Product. * @return mixed */ public function single_text( mixed $text, mixed $product = null ): mixed { return $this->filter_text( $text, $product, Context::Single ); } /** * Filter: button text in the shop, archives, blocks and the Store API. * * @param mixed $text Text from WooCommerce. * @param mixed $product Product. * @return mixed */ public function archive_text( mixed $text, mixed $product = null ): mixed { return $this->filter_text( $text, $product, Context::Archive ); } /** * Replaces the WooCommerce text when a custom text applies. * * @param mixed $text Text from WooCommerce. * @param mixed $product Product. * @param Context $context Button context. * @return mixed */ private function filter_text( mixed $text, mixed $product, Context $context ): mixed { if ( ! is_string( $text ) || ! $product instanceof \WC_Product ) { return $text; } $resolution = $this->resolve( $product, $context ); return $resolution->is_custom() ? $resolution->text : $text; } /** * Finds the custom text for a product and context. * * @param \WC_Product $product Product. * @param Context $context Button context. * @return Resolution */ public function resolve( \WC_Product $product, Context $context ): Resolution { if ( ! $this->settings->is_enabled() ) { return Resolution::keep_default(); } // A variation is added to the cart directly, so it uses the product page wording. if ( $product->is_type( array( 'variation', 'subscription_variation' ) ) ) { $context = Context::Single; } if ( $this->is_unavailable( $product ) ) { $out_of_stock = $this->settings->text( 'out_of_stock' ); // Never promise "Add to cart" for a product that cannot be bought. return '' === $out_of_stock ? Resolution::keep_default() : new Resolution( $this->apply_placeholders( $out_of_stock, $product ), Text_Source::Out_Of_Stock ); } $resolution = $this->product_resolution( $product, $context ) ?? $this->category_resolution( $product, $context ) ?? $this->type_resolution( $product, $context ) ?? Resolution::keep_default(); /** * Filters the resolved custom button text before placeholders are replaced. * * @param string $text Custom text ('' = keep the WooCommerce text). * @param \WC_Product $product Product. * @param string $context "single" or "archive". */ $filtered = apply_filters( 'adtct_button_text', $resolution->text, $product, $context->value ); $filtered = is_string( $filtered ) ? $filtered : ''; if ( $filtered !== $resolution->text ) { $resolution = '' === $filtered ? Resolution::keep_default() : new Resolution( $filtered, Text_Source::Filter ); } if ( Text_Source::Default === $resolution->source ) { return $resolution; } $text = $this->apply_placeholders( $resolution->text, $product ); return '' === $text ? Resolution::keep_default() : $resolution->with_text( $text ); } /** * Whether WooCommerce would show "Read more" because the product cannot be bought. * * @param \WC_Product $product Product. * @return bool */ private function is_unavailable( \WC_Product $product ): bool { if ( ! $product->is_in_stock() ) { return true; } // Grouped and external products are never "purchasable" themselves. if ( $product->is_type( array( 'grouped', 'external' ) ) ) { return false; } return ! $product->is_purchasable(); } /** * The product that carries meta and categories (parent for variations). * * @param \WC_Product $product Product. * @return \WC_Product */ private function base_product( \WC_Product $product ): \WC_Product { if ( ! $product->get_parent_id() ) { return $product; } $parent = wc_get_product( $product->get_parent_id() ); return $parent instanceof \WC_Product ? $parent : $product; } /** * Per-product override. * * @param \WC_Product $product Product. * @param Context $context Button context. * @return Resolution|null */ private function product_resolution( \WC_Product $product, Context $context ): ?Resolution { $base = $this->base_product( $product ); $value = $base->get_meta( $context->product_meta_key(), true ); return is_string( $value ) && '' !== $value ? new Resolution( $value, Text_Source::Product, $base->get_id() ) : null; } /** * Category override. The most specific category wins; categories inherit from their parents. * * @param \WC_Product $product Product. * @param Context $context Button context. * @return Resolution|null */ private function category_resolution( \WC_Product $product, Context $context ): ?Resolution { $product_id = $this->base_product( $product )->get_id(); $cache_key = $product_id . ':' . $context->value; if ( array_key_exists( $cache_key, $this->category_cache ) ) { return $this->category_cache[ $cache_key ]; } $best = null; $depth = -1; $terms = get_the_terms( $product_id, 'product_cat' ); foreach ( is_array( $terms ) ? $terms : array() as $term ) { $chain = array( (int) $term->term_id, ...array_map( intval( ... ), get_ancestors( $term->term_id, 'product_cat', 'taxonomy' ) ) ); foreach ( $chain as $term_id ) { $value = get_term_meta( $term_id, $context->term_meta_key(), true ); if ( ! is_string( $value ) || '' === $value ) { continue; } $term_depth = count( get_ancestors( $term_id, 'product_cat', 'taxonomy' ) ); if ( $term_depth > $depth || ( $term_depth === $depth && $term_id < (int) $best?->source_id ) ) { $best = new Resolution( $value, Text_Source::Category, $term_id ); $depth = $term_depth; } break; } } $this->category_cache[ $cache_key ] = $best; return $best; } /** * Text of the product type. * * @param \WC_Product $product Product. * @param Context $context Button context. * @return Resolution|null */ private function type_resolution( \WC_Product $product, Context $context ): ?Resolution { $value = $this->settings->type_text( Product_Type::key_for( $product ), $context ); return '' !== $value ? new Resolution( $value, Text_Source::Type ) : null; } /** * Replaces {name} and {price}. The result is always plain text. * * @param string $text Text with placeholders. * @param \WC_Product $product Product. * @return string */ private function apply_placeholders( string $text, \WC_Product $product ): string { if ( str_contains( $text, '{' ) ) { $price = ''; if ( str_contains( $text, '{price}' ) && '' !== (string) $product->get_price() ) { $price = self::plain( wc_price( (float) wc_get_price_to_display( $product ) ) ); } $text = strtr( $text, array( '{name}' => self::plain( $product->get_name() ), '{price}' => $price, ) ); } // Plain text, whatever the stored values or product data contain. return trim( wp_strip_all_tags( (string) preg_replace( '/\s{2,}/u', ' ', $text ) ) ); } /** * Converts HTML into plain text with decoded entities (never returns markup). * * @param string $html HTML. * @return string */ private static function plain( string $html ): string { $text = html_entity_decode( wp_strip_all_tags( $html ), ENT_QUOTES, get_bloginfo( 'charset' ) ); // Decoding can turn "<tag>" back into markup, so strip tags once more. return trim( wp_strip_all_tags( $text ) ); } /** * Filter: custom "added to cart" message. The "View cart" / "Continue shopping" button is kept. * * @param mixed $message Message HTML from WooCommerce. * @param mixed $products Product IDs => quantities. * @return mixed */ public function added_message( mixed $message, mixed $products = array() ): mixed { $template = $this->settings->text( 'added_message' ); if ( ! is_string( $message ) || '' === $template || ! $this->settings->is_enabled() ) { return $message; } $names = array(); $count = 0; foreach ( is_array( $products ) ? $products : array() as $product_id => $quantity ) { $product = wc_get_product( $product_id ); if ( $product instanceof \WC_Product ) { $names[] = '“' . self::plain( $product->get_name() ) . '”'; } $count += max( 1, (int) $quantity ); } $text = strtr( $template, array( '{products}' => implode( ', ', $names ), '{count}' => (string) $count, ) ); // WooCommerce puts the message text first and the button link after it. $button_position = strpos( $message, '