get_settings(); if ( empty( $s['enabled'] ) ) { return; } add_filter( 'woocommerce_product_single_add_to_cart_text', array( $this, 'single_add_to_cart_text' ), 99, 2 ); add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'shop_add_to_cart_text' ), 99, 2 ); add_action( 'wp_head', array( $this, 'emit_frontend_config' ), 1 ); } public function emit_frontend_config() { if ( is_admin() ) { return; } $s = $this->get_settings(); if ( empty( $s['enabled'] ) ) { return; } $per_variation = $this->is_per_variation_tracking_enabled(); $cfg = array( 'enabled' => true, 'showQty' => ! empty( $s['show_in_cart_qty'] ), 'simple' => array( 'singleDefault' => ! empty( $s['simple_product_custom_single'] ) ? (string) $s['simple_product_single_label'] : '', 'singleInCart' => ! empty( $s['simple_product_in_cart_label_enabled'] ) ? (string) $s['simple_product_in_cart_single_label'] : '', 'shopDefault' => (string) $s['simple_product_shop_label'], 'shopInCart' => ! empty( $s['simple_product_in_cart_label_enabled'] ) ? (string) $s['simple_product_in_cart_shop_label'] : '', ), 'variable' => array( 'singleDefault' => ! empty( $s['variable_product_custom_single'] ) ? (string) $s['variable_product_single_label'] : '', 'singleInCart' => ! empty( $s['variable_product_in_cart_label_enabled'] ) ? (string) $s['variable_product_in_cart_single_label'] : '', 'shopDefault' => (string) $s['variable_product_shop_label'], 'shopInCart' => ! empty( $s['variable_product_in_cart_label_enabled'] ) ? (string) $s['variable_product_in_cart_shop_label'] : '', ), 'perVariation' => $per_variation, ); if ( $per_variation ) { $map = $this->build_pdp_variation_qty_map(); if ( ! empty( $map ) ) { $cfg['qtyByVariation'] = $map; } } echo "\n"; } public function get_settings( $raw = false ) { $stored = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } $legacy = get_option( 'om_ppe_add-to-cart-text', array() ); if ( ! is_array( $legacy ) ) { $legacy = array(); } $merged = array_merge( array( 'enabled' => false, 'simple_product_shop_label' => '', 'simple_product_single_label' => '', 'simple_product_custom_single' => false, 'simple_product_in_cart_label_enabled' => false, 'simple_product_in_cart_shop_label' => '', 'simple_product_in_cart_single_label' => '', 'variable_product_shop_label' => '', 'variable_product_single_label' => '', 'variable_product_custom_single' => false, 'variable_product_in_cart_label_enabled' => false, 'variable_product_in_cart_shop_label' => '', 'variable_product_in_cart_single_label' => '', 'out_of_stock_label' => '', 'out_of_stock_custom_label_enabled' => false, 'show_in_cart_qty' => true, ), $legacy, $stored ); return $raw ? $merged : self::fill_i18n_defaults( $merged ); } private static function i18n_labels() { $add_to_cart = __( 'Add to cart', 'ai-eshop-optimizer' ); $add_another = __( 'Add another', 'ai-eshop-optimizer' ); return array( 'simple_product_shop_label' => $add_to_cart, 'simple_product_single_label' => $add_to_cart, 'simple_product_in_cart_shop_label' => $add_another, 'simple_product_in_cart_single_label' => $add_another, 'variable_product_shop_label' => __( 'Select options', 'ai-eshop-optimizer' ), 'variable_product_single_label' => $add_to_cart, 'variable_product_in_cart_shop_label' => $add_another, 'variable_product_in_cart_single_label' => $add_another, 'out_of_stock_label' => __( 'Out of stock', 'ai-eshop-optimizer' ), ); } private static function fill_i18n_defaults( array $s ) { if ( ! did_action( 'init' ) ) { return $s; } foreach ( self::i18n_labels() as $k => $v ) { if ( ! isset( $s[ $k ] ) || '' === trim( (string) $s[ $k ] ) ) { $s[ $k ] = $v; } } return $s; } public function ajax_save() { AIEO_DMM::verify_ajax_request(); $bool = function ( $key ) { return ! empty( $_POST[ $key ] ); }; $text = function ( $key, $default ) { return isset( $_POST[ $key ] ) ? sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) : $default; }; update_option( self::OPTION_KEY, array( 'enabled' => $bool( 'enabled' ), 'simple_product_shop_label' => $text( 'simple_product_shop_label', '' ), 'simple_product_single_label' => $text( 'simple_product_single_label', '' ), 'simple_product_custom_single' => $bool( 'simple_product_custom_single' ), 'simple_product_in_cart_label_enabled' => $bool( 'simple_product_in_cart_label_enabled' ), 'simple_product_in_cart_shop_label' => $text( 'simple_product_in_cart_shop_label', '' ), 'simple_product_in_cart_single_label' => $text( 'simple_product_in_cart_single_label', '' ), 'variable_product_shop_label' => $text( 'variable_product_shop_label', '' ), 'variable_product_single_label' => $text( 'variable_product_single_label', '' ), 'variable_product_custom_single' => $bool( 'variable_product_custom_single' ), 'variable_product_in_cart_label_enabled' => $bool( 'variable_product_in_cart_label_enabled' ), 'variable_product_in_cart_shop_label' => $text( 'variable_product_in_cart_shop_label', '' ), 'variable_product_in_cart_single_label' => $text( 'variable_product_in_cart_single_label', '' ), 'out_of_stock_label' => $text( 'out_of_stock_label', '' ), 'out_of_stock_custom_label_enabled' => $bool( 'out_of_stock_custom_label_enabled' ), 'show_in_cart_qty' => $bool( 'show_in_cart_qty' ), ), false ); wp_send_json_success( array( 'message' => __( 'Add to Cart Text settings saved.', 'ai-eshop-optimizer' ) ) ); } private function get_product_override( $product_id, $key ) { $aieo = (string) get_post_meta( $product_id, '_aieo_add_to_cart_' . $key, true ); if ( $aieo !== '' ) { return $aieo; } if ( $key === 'single_label' || $key === 'shop_label' ) { return (string) get_post_meta( $product_id, '_om_ppe_add_to_cart_' . $key, true ); } return ''; } private function is_in_cart( $product ) { if ( ! function_exists( 'WC' ) || ! WC()->cart ) { return false; } $pid = (int) $product->get_id(); foreach ( WC()->cart->get_cart() as $item ) { $ipid = (int) ( $item['product_id'] ?? 0 ); $ivid = (int) ( $item['variation_id'] ?? 0 ); if ( $ipid === $pid || $ivid === $pid ) { if ( (int) ( $item['quantity'] ?? 0 ) > 0 ) { return true; } } } return false; } private function build_pdp_variation_qty_map() { if ( ! function_exists( 'is_product' ) || ! is_product() ) { return array(); } if ( ! function_exists( 'WC' ) || ! WC()->cart ) { return array(); } $qo = function_exists( 'get_queried_object' ) ? get_queried_object() : null; if ( ! ( $qo instanceof WP_Post ) || $qo->post_type !== 'product' ) { return array(); } $product = function_exists( 'wc_get_product' ) ? wc_get_product( $qo->ID ) : null; if ( ! $product || ! $product->is_type( 'variable' ) ) { return array(); } $map = array(); foreach ( (array) $product->get_children() as $vid ) { $map[ (int) $vid ] = 0; } $parent_id = (int) $product->get_id(); foreach ( WC()->cart->get_cart() as $item ) { $ipid = (int) ( $item['product_id'] ?? 0 ); $ivid = (int) ( $item['variation_id'] ?? 0 ); if ( $ipid !== $parent_id || $ivid <= 0 ) { continue; } $map[ $ivid ] = ( isset( $map[ $ivid ] ) ? $map[ $ivid ] : 0 ) + max( 0, (int) ( $item['quantity'] ?? 0 ) ); } return $map; } private function is_per_variation_tracking_enabled() { $swatches = get_option( 'aieo_dmm_swatches_settings', array() ); if ( ! is_array( $swatches ) ) { $swatches = array(); } return ! empty( $swatches['track_per_variation_atc'] ); } public function single_add_to_cart_text( $text, $product ) { if ( ! is_a( $product, 'WC_Product' ) ) { return $text; } $pid = (int) $product->get_id(); if ( $this->is_in_cart( $product ) ) { $override = $this->get_product_override( $pid, 'in_cart_single_label' ); if ( $override !== '' ) { return esc_html( $override ); } } $override = $this->get_product_override( $pid, 'single_label' ); if ( $override !== '' ) { return esc_html( $override ); } $s = $this->get_settings(); if ( ! function_exists( 'is_product' ) || ! is_product() ) { return $text; } $is_simple = $product->is_type( 'simple' ); $is_variable = $product->is_type( 'variable' ); if ( ! $is_simple && ! $is_variable ) { return $text; } $prefix = $is_simple ? 'simple_product_' : 'variable_product_'; $in_cart = $this->is_in_cart( $product ); $ic_enabled = ! empty( $s[ $prefix . 'in_cart_label_enabled' ] ); if ( $in_cart && $ic_enabled ) { return esc_html( (string) $s[ $prefix . 'in_cart_single_label' ] ); } if ( ! empty( $s[ $prefix . 'custom_single' ] ) ) { return esc_html( (string) $s[ $prefix . 'single_label' ] ); } return $text; } public function shop_add_to_cart_text( $text, $product ) { if ( ! is_a( $product, 'WC_Product' ) ) { return $text; } $s = $this->get_settings(); if ( ! $product->is_in_stock() && ! empty( $s['out_of_stock_custom_label_enabled'] ) ) { return esc_html( (string) $s['out_of_stock_label'] ); } $pid = (int) $product->get_id(); if ( $this->is_in_cart( $product ) ) { $override = $this->get_product_override( $pid, 'in_cart_shop_label' ); if ( $override !== '' ) { return esc_html( $override ); } } $override = $this->get_product_override( $pid, 'shop_label' ); if ( $override !== '' ) { return esc_html( $override ); } $is_simple = $product->is_type( 'simple' ); $is_variable = $product->is_type( 'variable' ); if ( ! $is_simple && ! $is_variable ) { return $text; } $prefix = $is_simple ? 'simple_product_' : 'variable_product_'; $in_cart = $this->is_in_cart( $product ); $ic_enabled = ! empty( $s[ $prefix . 'in_cart_label_enabled' ] ); if ( $in_cart && $ic_enabled ) { return esc_html( (string) $s[ $prefix . 'in_cart_shop_label' ] ); } return esc_html( (string) $s[ $prefix . 'shop_label' ] ); } public function render_subtab() { $s = $this->get_settings( true ); $s_hint = self::i18n_labels(); $switch = static function ( $name, $text, $label_hidden = false ) use ( $s ) { return \Rooster\Admin\Ui::toggle( $name, ! empty( $s[ $name ] ), array( 'id' => 'aieo-actt-' . str_replace( '_', '-', $name ), 'hidden' => false, 'label' => $text, 'label_hidden' => $label_hidden ) ); }; $text = static function ( $name ) use ( $s, $s_hint ) { return \Rooster\Admin\Ui::input( $name, (string) $s[ $name ], array( 'id' => 'aieo-actt-' . str_replace( '_', '-', $name ), 'medium' => true, 'placeholder' => isset( $s_hint[ $name ] ) ? $s_hint[ $name ] : '' ) ); }; $row = static function ( $name, $label, $control, $help = '' ) { return \Rooster\Admin\Ui::field_row( array( 'id' => 'aieo-actt-' . str_replace( '_', '-', $name ), 'label' => $label, 'control' => $control, 'help' => $help ) ); }; $help_shop = __( 'The button\'s words on the product cards of the shop and category pages. Leave it empty for the words shown in grey.', 'ai-eshop-optimizer' ); $help_single = __( 'Used only while the custom single-page label is on; otherwise the product page keeps WooCommerce\'s own words.', 'ai-eshop-optimizer' ); $help_in_shop = __( 'Replaces the shop page label once the shopper has this product in the cart, while the "In cart" label is on.', 'ai-eshop-optimizer' ); $help_in_one = __( 'Replaces the single page label once the shopper has this product in the cart, while the "In cart" label is on.', 'ai-eshop-optimizer' ); $card = static function ( $anchor, $icon, $title, $col, $fields, $extra = array() ) { $form_id = 'aieo-card-' . $anchor . '-form'; list( $open, $close ) = AIEO_DMM_UI::card( array( 'anchor' => $anchor, 'icon' => $icon, 'title' => $title, 'col' => $col, 'footer' => class_exists( '\Rooster\Admin\States' ) ? \Rooster\Admin\States::save_button( $form_id ) : '', ) + $extra ); echo $open; echo AIEO_DMM_UI::card_form( 'aieo_dmm_add_to_cart_text_save', 'add_to_cart_text', $form_id, '