get_module('brand_gifts'); if ($mod instanceof self) { self::$instance = $mod; } } if (self::$instance === null) { self::$instance = new self(); } } return self::$instance; } public function __construct() { if (self::$instance === null) { self::$instance = $this; } } private static $is_internal_add = false; public function register_hooks() { self::register_eligibility(); add_action('aieo_dmm_date_boundary', array(__CLASS__, 'on_date_boundary'), 10, 1); add_action('wp_ajax_aieo_dmm_brand_gift_save', array($this, 'ajax_save')); add_action('wp_ajax_aieo_dmm_brand_gift_delete', array($this, 'ajax_delete')); add_action('wp_ajax_aieo_dmm_search_products', array($this, 'ajax_search_products')); add_action('wp_ajax_aieo_dmm_search_need_terms', array($this, 'ajax_search_need_terms')); add_action('wp_ajax_aieo_dmm_brand_gift_pick', array($this, 'ajax_pick_gift')); add_action('wp_ajax_nopriv_aieo_dmm_brand_gift_pick', array($this, 'ajax_pick_gift')); add_action('wp_ajax_aieo_dmm_brand_gift_variations', array($this, 'ajax_get_variations')); add_action('wp_ajax_nopriv_aieo_dmm_brand_gift_variations', array($this, 'ajax_get_variations')); add_filter('woocommerce_is_purchasable', array($this, 'filter_sample_unpurchasable'), 10, 2); add_action('woocommerce_product_query', array($this, 'exclude_samples_from_archive')); add_action('pre_get_posts', array($this, 'exclude_samples_from_search')); add_action('woocommerce_update_product', array($this, 'flush_sample_ids_cache')); add_action('woocommerce_process_product_meta', array($this, 'sync_sample_visibility'), 50); add_action('woocommerce_update_product', array($this, 'sync_sample_visibility'), 20); add_action('woocommerce_new_product', array($this, 'sync_sample_visibility'), 20); add_action('admin_init', array($this, 'migrate_sample_visibility')); add_action('woocommerce_product_options_general_product_data', array($this, 'render_gift_type_field')); add_action('woocommerce_process_product_meta', array($this, 'save_gift_type_field')); if (self::switched_on()) { if ( ! defined( 'AIEO_RUNTIME_OWNS_AJAX' ) ) { if (self::the_pass_answers()) { add_action('woocommerce_cart_loaded_from_session', array('AIEO_DMM_Cart_Pass', 'run'), 99); add_action('woocommerce_add_to_cart', array('AIEO_DMM_Cart_Pass', 'run_deferred'), 99); add_action('woocommerce_cart_item_removed', array('AIEO_DMM_Cart_Pass', 'run_deferred'), 99); add_action('woocommerce_after_cart_item_quantity_update', array('AIEO_DMM_Cart_Pass', 'run_deferred'), 99); } else { add_action('woocommerce_cart_loaded_from_session', array($this, 'sync_gifts'), 99); add_action('woocommerce_add_to_cart', array($this, 'sync_gifts_deferred'), 99); add_action('woocommerce_cart_item_removed', array($this, 'sync_gifts_deferred'), 99); add_action('woocommerce_after_cart_item_quantity_update', array($this, 'sync_gifts_deferred'), 99); add_action('woocommerce_before_calculate_totals', array($this, 'set_gift_prices'), 99); } add_filter('woocommerce_product_get_price', array($this, 'zero_gift_line_price'), 9999, 2); add_filter('woocommerce_product_get_sale_price', array($this, 'zero_gift_line_price'), 9999, 2); add_filter('woocommerce_product_variation_get_price', array($this, 'zero_gift_line_price'), 9999, 2); add_filter('woocommerce_product_variation_get_sale_price', array($this, 'zero_gift_line_price'), 9999, 2); add_filter('woocommerce_cart_item_quantity', array($this, 'lock_gift_quantity'), 10, 3); add_filter('woocommerce_cart_item_name', array($this, 'add_gift_badge'), 10, 3); add_filter('woocommerce_cart_item_remove_link', array($this, 'hide_gift_remove_link'), 10, 2); add_action('woocommerce_after_cart_item_quantity_update', array($this, 'keep_gift_quantity'), 5, 4); add_filter('woocommerce_store_api_product_quantity_minimum', array($this, 'gift_quantity_limit'), 10, 3); add_filter('woocommerce_store_api_product_quantity_maximum', array($this, 'gift_quantity_limit'), 10, 3); add_filter('woocommerce_store_api_product_quantity_editable', array($this, 'gift_quantity_limit'), 10, 3); } add_action('woocommerce_checkout_create_order_line_item', array($this, 'stamp_gift_order_line'), 20, 3); add_filter('woocommerce_cart_item_is_purchasable', array($this, 'keep_sample_gift_line'), 10, 4); $is_block_theme = wp_is_block_theme(); $use_wc_hook = (bool) apply_filters('aieo_dmm_brand_gifts_fse_use_hook', ! $is_block_theme); if ($use_wc_hook) { add_action('woocommerce_single_product_summary', array($this, 'render_single_product_section'), 25); add_filter('render_block_woocommerce/add-to-cart-form', array($this, 'inject_single_product_section_into_block'), 12, 2); } add_action('woocommerce_before_shop_loop_item_title', array($this, 'render_archive_badge'), 12); if (class_exists('AIEO_DMM_Badge_Renderer') && method_exists('AIEO_DMM_Badge_Renderer', 'picture_label')) { AIEO_DMM_Badge_Renderer::picture_label(16, array($this, 'archive_badge_html')); } add_action('woocommerce_archive_description', array($this, 'render_archive_banner'), 5); add_filter('render_block_core/term-description', array($this, 'inject_archive_banner_into_block'), 5, 2); add_filter('render_block_woocommerce/product-collection', array($this, 'inject_archive_banner_into_block'), 5, 2); add_action('wp_enqueue_scripts', array($this, 'enqueue_frontend_assets')); } elseif ( ! defined( 'AIEO_RUNTIME_OWNS_AJAX' ) ) { add_action('woocommerce_cart_loaded_from_session', array($this, 'remove_gift_lines'), 99); } if ( ! defined( 'AIEO_RUNTIME_OWNS_AJAX' ) ) { add_filter('woocommerce_order_again_cart_item_data', array($this, 'mark_order_again_gift_line'), 10, 2); add_filter('woocommerce_pre_remove_cart_item_from_session', array($this, 'drop_order_again_gift_line'), 10, 3); if (self::the_pass_answers()) { add_action('woocommerce_before_calculate_totals', array('AIEO_DMM_Cart_Pass', 'price_lines'), 20); add_action('woocommerce_cart_loaded_from_session', array('AIEO_DMM_Cart_Pass', 'price_loaded'), 100); add_filter('woocommerce_get_item_data', array('AIEO_DMM_Cart_Pass', 'item_note'), 20, 2); } } } public static function the_pass_answers() { return class_exists('AIEO_DMM_Price_Filter') && AIEO_DMM_Price_Filter::resolves() && class_exists('AIEO_DMM_Cart_Pass'); } public static function register_eligibility() { if (!(class_exists('AIEO_DMM_Price_Filter') && AIEO_DMM_Price_Filter::resolves())) { add_filter('aieo_eligibility_compute', array(__CLASS__, 'add_eligibility_row'), 10, 3); } } public function enqueue_frontend_assets() { $css_rel = 'assets/css/aieo-brand-gifts.css'; $js_rel = 'assets/js/aieo-brand-gifts.js'; $css_path = dirname(AIEO_FILE) . '/' . $css_rel; $js_path = dirname(AIEO_FILE) . '/' . $js_rel; $css_ver = file_exists($css_path) ? filemtime($css_path) : AIEO_VERSION; $js_ver = file_exists($js_path) ? filemtime($js_path) : AIEO_VERSION; wp_enqueue_style('aieo-brand-gifts', plugins_url($css_rel, AIEO_FILE), array(), $css_ver); wp_enqueue_script('aieo-brand-gifts', plugins_url($js_rel, AIEO_FILE), array('jquery'), $js_ver, array('in_footer' => true, 'strategy' => 'defer')); wp_localize_script('aieo-brand-gifts', 'aieoBrandGifts', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('aieo_side_cart'), 'i18n' => array( 'pickVariation' => __('— Pick variation —', 'ai-eshop-optimizer'), 'noVariations' => __('No in-stock variations available.', 'ai-eshop-optimizer'), 'claimed' => __('Gift added to your cart.', 'ai-eshop-optimizer'), 'error' => __('Could not update — please try again.', 'ai-eshop-optimizer'), ), )); } public function render_subtab() { $this->render_template('brand-gifts.php', array( 'offers' => $this->fetch_all_offers(true), 'module' => $this, )); } public function fetch_all_offers($with_labels = false) { global $wpdb; $table = $wpdb->prefix . 'aieo_dmm_brand_gift_offers'; if (!aieo_dmm_table_exists($table)) { return array(); } $rows = $wpdb->get_results("SELECT * FROM {$table} ORDER BY enabled DESC, id DESC", ARRAY_A); $out = array(); if (is_array($rows)) { foreach ($rows as $row) { if ($with_labels) { $resolved = $this->fetch_offer_with_labels((int) $row['id']); if ($resolved) { $out[] = $resolved; continue; } } $row['brand_term_ids'] = json_decode($row['brand_term_ids'] ?? '[]', true) ?: array(); $row['gift_product_ids'] = json_decode($row['gift_product_ids'] ?? '[]', true) ?: array(); $row['category_term_ids'] = json_decode($row['category_term_ids'] ?? '[]', true) ?: array(); $row['need_term_ids'] = json_decode($row['need_term_ids'] ?? '[]', true) ?: array(); $row['rule_operator'] = (!empty($row['rule_operator']) && strtoupper($row['rule_operator']) === 'OR') ? 'OR' : 'AND'; $out[] = $row; } } return $out; } public function get_active_offers() { if (class_exists('AIEO_DMM_Offer_Store') && AIEO_DMM_Offer_Store::reads_unified()) { return $this->active_offers_of_the_one_table(); } return AIEO_DMM_Cache::remember('gifts', 'active_offers', function () { $all = $this->fetch_all_offers(); $today = current_time('Y-m-d'); $active = array(); foreach ($all as $offer) { if (empty($offer['enabled'])) { continue; } if (!empty($offer['date_from']) && $offer['date_from'] > $today) { continue; } if (!empty($offer['date_to']) && $offer['date_to'] < $today) { continue; } $active[] = $offer; } return $active; }, AIEO_DMM_Cache::TTL_SHORT); } private function active_offers_of_the_one_table() { $running = self::running_gift_ids(); if (!$running) { return array(); } $out = array(); foreach (AIEO_DMM_Offer_Matcher::active_offers('gift') as $row) { if (!isset($running[(int) $row['id']])) { continue; } $offer = AIEO_DMM_Offer_Store::legacy_row($row); if (!is_array($offer)) { continue; } foreach (array('brand_term_ids', 'gift_product_ids', 'category_term_ids', 'need_term_ids') as $list) { $decoded = isset($offer[$list]) && is_string($offer[$list]) ? json_decode($offer[$list], true) : (isset($offer[$list]) ? $offer[$list] : array()); $offer[$list] = is_array($decoded) ? $decoded : array(); } $offer['id'] = (int) $row['id']; $offer['_legacy_id'] = 'aieo_dmm_brand_gift_offers' === (string) ($row['legacy_table'] ?? '') ? (int) $row['legacy_id'] : 0; $offer['_definition'] = isset($row['_definition']) ? $row['_definition'] : null; $offer['_needs'] = isset($row['_needs']) ? $row['_needs'] : null; $offer['_pinned'] = AIEO_DMM_Offer_Store::is_pinned($row); $out[] = $offer; } return $out; } private static function running_gift_ids() { if (!class_exists('AIEO_DMM_Offers_Now')) { require_once dirname(__DIR__) . '/services/class-aieo-dmm-offers-now.php'; } $snapshot = AIEO_DMM_Offers_Now::get(); if ((int) ($snapshot['n'] ?? 0) <= 0) { return array(); } return array_fill_keys(AIEO_DMM_Offers_Now::cart_offer_ids('gift'), true); } private function gift_line_offer_id($item) { $id = is_array($item) && !empty($item['_aieo_dmm_gift_offer_id']) ? (int) $item['_aieo_dmm_gift_offer_id'] : 0; if ($id <= 0 || !class_exists('AIEO_DMM_Offer_Store')) { return $id; } $of_the_one_table = isset($item['_aieo_offer_store']) && 'of' === $item['_aieo_offer_store']; if (AIEO_DMM_Offer_Store::reads_unified()) { return $of_the_one_table ? $id : (int) AIEO_DMM_Offer_Ids::resolve('go', $id); } if (!$of_the_one_table) { return $id; } static $old_ids = array(); if (!isset($old_ids[$id])) { global $wpdb; $old_ids[$id] = (int) $wpdb->get_var($wpdb->prepare("SELECT legacy_id FROM {$wpdb->prefix}aieo_dmm_offers WHERE id = %d AND legacy_table = %s", $id, 'aieo_dmm_brand_gift_offers')); } return $old_ids[$id]; } public static function is_spent_by_the_visitor($offer_id) { $offer_id = (int) $offer_id; if ($offer_id <= 0 || !class_exists('AIEO_DMM_Offer_Store') || !AIEO_DMM_Offer_Store::reads_unified() || !class_exists('AIEO_DMM_Price_Resolver')) { return false; } $spent = AIEO_DMM_Price_Resolver::spent(AIEO_DMM_Price_Resolver::customer((int) get_current_user_id())); return isset($spent[$offer_id]); } private static function gift_line($html) { return method_exists('AIEO_Icons', 'line') ? AIEO_Icons::line('gift', $html) : AIEO_Icons::lead_html($html); } public static function spent_words() { return __('You have already received this gift.', 'ai-eshop-optimizer'); } public function ajax_save() { AIEO_DMM::verify_ajax_request(); global $wpdb; $table = $wpdb->prefix . 'aieo_dmm_brand_gift_offers'; if (!aieo_dmm_table_exists($table)) { wp_send_json_error(array('message' => __('Table missing.', 'ai-eshop-optimizer'))); } $id = isset($_POST['id']) ? absint(wp_unslash($_POST['id'])) : 0; $is_update = $id > 0; $need_ids = isset($_POST['need_term_ids']) && is_array($_POST['need_term_ids']) ? array_values(array_filter(array_map('absint', wp_unslash($_POST['need_term_ids'])))) : array(); $brand_ids = isset($_POST['brand_term_ids']) && is_array($_POST['brand_term_ids']) ? array_map('absint', wp_unslash($_POST['brand_term_ids'])) : array(); $category_ids = isset($_POST['category_term_ids']) && is_array($_POST['category_term_ids']) ? array_map('absint', wp_unslash($_POST['category_term_ids'])) : array(); $gift_ids = isset($_POST['gift_product_ids']) && is_array($_POST['gift_product_ids']) ? array_map('absint', wp_unslash($_POST['gift_product_ids'])) : array(); $brand_ids = array_values(array_filter($brand_ids)); $category_ids = array_values(array_filter($category_ids)); $excluded_brand_ids = isset($_POST['excluded_brand_ids']) && is_array($_POST['excluded_brand_ids']) ? array_values(array_unique(array_filter(array_map('absint', wp_unslash($_POST['excluded_brand_ids']))))) : array(); $excluded_category_ids = isset($_POST['excluded_category_ids']) && is_array($_POST['excluded_category_ids']) ? array_values(array_unique(array_filter(array_map('absint', wp_unslash($_POST['excluded_category_ids']))))) : array(); $excluded_need_ids = isset($_POST['excluded_need_ids']) && is_array($_POST['excluded_need_ids']) ? array_values(array_unique(array_filter(array_map('absint', wp_unslash($_POST['excluded_need_ids']))))) : array(); $product_ids = AIEO_DMM_Product_Picker::roll_up_to_parents( AIEO_DMM_Product_Picker::collect_ids('product_ids') ); $excluded_product_ids = AIEO_DMM_Product_Picker::roll_up_to_parents( AIEO_DMM_Product_Picker::collect_ids('excluded_product_ids') ); $code_patterns = AIEO_DMM_Pattern_Match::collect('code_patterns'); $excluded_code_patterns = AIEO_DMM_Pattern_Match::collect('excluded_code_patterns'); $rule_operator = isset($_POST['rule_operator']) && sanitize_key(wp_unslash($_POST['rule_operator'])) === 'or' ? 'OR' : 'AND'; $min_subtotal = isset($_POST['min_subtotal']) ? (float) $_POST['min_subtotal'] : 0.0; if (empty($brand_ids) && empty($category_ids) && empty($need_ids) && $min_subtotal <= 0) { wp_send_json_error(array('message' => __('Pick at least one brand, category, need, or set a minimum subtotal.', 'ai-eshop-optimizer'))); } if (empty($gift_ids)) { wp_send_json_error(array('message' => __('Pick at least one gift product.', 'ai-eshop-optimizer'))); } $date_from_raw = isset($_POST['date_from']) ? sanitize_text_field(wp_unslash($_POST['date_from'])) : ''; $date_to_raw = isset($_POST['date_to']) ? sanitize_text_field(wp_unslash($_POST['date_to'])) : ''; $window_error = AIEO_DMM_Schedule::window_error($date_from_raw, $date_to_raw); if ('' !== $window_error) { wp_send_json_error(array('message' => $window_error)); } $date_from = AIEO_DMM_Schedule::clean_date($date_from_raw); $date_to = AIEO_DMM_Schedule::clean_date($date_to_raw); $gift_kind_in = isset($_POST['gift_kind']) ? sanitize_key(wp_unslash($_POST['gift_kind'])) : 'product'; if (!in_array($gift_kind_in, array('product', 'sample', 'mixed'), true)) { $gift_kind_in = 'product'; } $pres_mode_in = isset($_POST['presentation_mode']) ? sanitize_key(wp_unslash($_POST['presentation_mode'])) : 'auto'; if (!in_array($pres_mode_in, array('list', 'dropdown', 'auto'), true)) { $pres_mode_in = 'auto'; } $data = array( 'enabled' => !empty($_POST['enabled']) ? 1 : 0, 'label' => isset($_POST['label']) ? sanitize_text_field(wp_unslash($_POST['label'])) : '', 'brand_term_ids' => wp_json_encode($brand_ids), 'category_term_ids' => wp_json_encode($category_ids), 'need_term_ids' => wp_json_encode($need_ids), 'rule_operator' => $rule_operator, 'min_subtotal' => $min_subtotal, 'date_from' => '' !== $date_from ? $date_from : null, 'date_to' => '' !== $date_to ? $date_to : null, 'gift_product_ids' => wp_json_encode(array_values(array_filter($gift_ids))), 'before_text' => isset($_POST['before_text']) ? wp_kses_post(wp_unslash($_POST['before_text'])) : '', 'after_text' => isset($_POST['after_text']) ? wp_kses_post(wp_unslash($_POST['after_text'])) : '', 'before_image_id' => isset($_POST['before_image_id']) ? absint(wp_unslash($_POST['before_image_id'])) : null, 'after_image_id' => isset($_POST['after_image_id']) ? absint(wp_unslash($_POST['after_image_id'])) : null, 'bar_bg_before' => sanitize_hex_color(wp_unslash($_POST['bar_bg_before'] ?? '')) ?: null, 'bar_bg_after' => sanitize_hex_color(wp_unslash($_POST['bar_bg_after'] ?? '')) ?: null, 'bar_fg_before' => sanitize_hex_color(wp_unslash($_POST['bar_fg_before'] ?? '')) ?: null, 'bar_fg_after' => sanitize_hex_color(wp_unslash($_POST['bar_fg_after'] ?? '')) ?: null, 'gift_item_bg' => sanitize_hex_color(wp_unslash($_POST['gift_item_bg'] ?? '')) ?: null, 'show_banner' => !empty($_POST['show_banner']) ? 1 : 0, 'show_rewards' => !empty($_POST['show_rewards']) ? 1 : 0, 'gift_kind' => $gift_kind_in, 'presentation_mode' => $pres_mode_in, 'show_when_pending' => !empty($_POST['show_when_pending']) ? 1 : 0, 'headline_pending' => isset($_POST['headline_pending']) ? sanitize_text_field(wp_unslash($_POST['headline_pending'])) : '', 'headline_qualified' => isset($_POST['headline_qualified']) ? sanitize_text_field(wp_unslash($_POST['headline_qualified'])) : '', 'pill_locked' => isset($_POST['pill_locked']) ? sanitize_text_field(wp_unslash($_POST['pill_locked'])) : '', 'pill_available' => isset($_POST['pill_available']) ? sanitize_text_field(wp_unslash($_POST['pill_available'])) : '', 'pill_claimed' => isset($_POST['pill_claimed']) ? sanitize_text_field(wp_unslash($_POST['pill_claimed'])) : '', 'excluded_brand_ids' => wp_json_encode($excluded_brand_ids), 'excluded_category_ids' => wp_json_encode($excluded_category_ids), 'excluded_need_ids' => wp_json_encode($excluded_need_ids), 'product_ids' => wp_json_encode($product_ids), 'excluded_product_ids' => wp_json_encode($excluded_product_ids), 'code_patterns' => AIEO_DMM_Pattern_Match::to_json($code_patterns), 'excluded_code_patterns' => AIEO_DMM_Pattern_Match::to_json($excluded_code_patterns), ); $formats = array( '%d', '%s', '%s', '%s', '%s', '%s', '%f', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', ); require_once dirname(__DIR__) . '/services/class-aieo-dmm-offer-forms.php'; if (AIEO_DMM_Offer_Forms::previewing()) { AIEO_DMM_Offer_Forms::answer_preview('aieo_dmm_brand_gift_offers', array(($is_update ? array('id' => $id) : array()) + $data)); } if ($is_update) { $this->write_offer($data, $formats, $id); } else { $dup_id = (int) $wpdb->get_var($wpdb->prepare( "SELECT id FROM {$table} WHERE COALESCE(brand_term_ids,'[]') = %s AND COALESCE(category_term_ids,'[]') = %s AND COALESCE(need_term_ids,'[]') = %s AND rule_operator = %s AND COALESCE(min_subtotal,0) = %f AND COALESCE(gift_product_ids,'[]') = %s AND COALESCE(date_from,'') = %s AND COALESCE(date_to,'') = %s LIMIT 1", $data['brand_term_ids'], $data['category_term_ids'], $data['need_term_ids'], $data['rule_operator'], $data['min_subtotal'], $data['gift_product_ids'], (string) ($data['date_from'] ?? ''), (string) ($data['date_to'] ?? '') )); if ($dup_id > 0 && $this->same_times_as($dup_id)) { $id = $dup_id; } else { $id = $this->write_offer($data, $formats); } } if (!$this->writer_wrote) { AIEO_DMM_Cache::bump_version('gifts'); } self::prune_eligibility($id, true); AIEO_DMM_Offer_Pages::purge('gift'); $row = $this->fetch_offer_with_labels($id); wp_send_json_success(array( 'id' => $id, 'is_update' => $is_update, 'row' => $row, 'message' => __('Gift offer saved.', 'ai-eshop-optimizer'), )); } private $writer_wrote = false; private function same_times_as($dup_id) { require_once dirname(__DIR__) . '/services/class-aieo-dmm-offer-forms.php'; return AIEO_DMM_Offer_Forms::same_times('aieo_dmm_brand_gift_offers', (int) $dup_id); } private function write_offer(array $data, array $formats, $id = 0) { global $wpdb; require_once dirname(__DIR__) . '/services/class-aieo-dmm-offer-forms.php'; $id = (int) $id; if (!AIEO_DMM_Offer_Forms::through_the_writer_for('aieo_dmm_brand_gift_offers', $id)) { $table = $wpdb->prefix . 'aieo_dmm_brand_gift_offers'; if ($id > 0) { $wpdb->update($table, $data, array('id' => $id), $formats, array('%d')); } else { $wpdb->insert($table, $data, $formats); $id = (int) $wpdb->insert_id; } if ($id > 0) { AIEO_DMM_Offer_Forms::copy('aieo_dmm_brand_gift_offers', $id); } return $id; } $saved = AIEO_DMM_Offer_Forms::save_old_row('aieo_dmm_brand_gift_offers', ($id > 0 ? array('id' => $id) : array()) + $data, 'the gift offers form'); if (is_wp_error($saved)) { AIEO_DMM_Offer_Forms::refuse($saved); } $this->writer_wrote = true; return (int) $saved['old_id']; } private function fetch_offer_with_labels($id) { global $wpdb; $table = $wpdb->prefix . 'aieo_dmm_brand_gift_offers'; $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", (int) $id), ARRAY_A); if (!$row) { return null; } $brand_ids = json_decode($row['brand_term_ids'] ?? '[]', true) ?: array(); $category_ids = json_decode($row['category_term_ids'] ?? '[]', true) ?: array(); $need_ids = json_decode($row['need_term_ids'] ?? '[]', true) ?: array(); $gift_ids = json_decode($row['gift_product_ids'] ?? '[]', true) ?: array(); $excl_brand_ids = json_decode($row['excluded_brand_ids'] ?? '[]', true) ?: array(); $excl_cat_ids = json_decode($row['excluded_category_ids'] ?? '[]', true) ?: array(); $excl_need_ids = json_decode($row['excluded_need_ids'] ?? '[]', true) ?: array(); $product_ids_pos = json_decode($row['product_ids'] ?? '[]', true) ?: array(); $product_ids_neg = json_decode($row['excluded_product_ids'] ?? '[]', true) ?: array(); $brand_tax = class_exists('AIEO_DMM_Brand_Discounts') ? AIEO_DMM_Brand_Discounts::get_brand_taxonomy() : 'product_brand'; if (!$brand_tax) { $brand_tax = 'product_brand'; } $resolve_term_names = function ($ids, $tax) { $out = array(); foreach ($ids as $tid) { $t = get_term((int) $tid, $tax); $out[] = ($t && !is_wp_error($t)) ? (string) $t->name : ('#' . (int) $tid); } return $out; }; $resolve_product_names = function ($ids) { $out = array(); foreach ($ids as $pid) { $title = get_the_title((int) $pid); $out[] = ($title !== '' && $title !== null) ? (string) $title : ('#' . (int) $pid); } return $out; }; $row['brand_term_ids'] = array_map('intval', $brand_ids); $row['category_term_ids'] = array_map('intval', $category_ids); $row['need_term_ids'] = array_map('intval', $need_ids); $row['gift_product_ids'] = array_map('intval', $gift_ids); $row['brand_term_names'] = $resolve_term_names($brand_ids, $brand_tax); $row['category_term_names'] = $resolve_term_names($category_ids, 'product_cat'); $row['need_term_names'] = $resolve_term_names($need_ids, 'pa_need'); $row['gift_product_names'] = $resolve_product_names($gift_ids); $row['rule_operator'] = (!empty($row['rule_operator']) && strtoupper($row['rule_operator']) === 'OR') ? 'OR' : 'AND'; $row['excluded_brand_ids'] = array_map('intval', $excl_brand_ids); $row['excluded_category_ids'] = array_map('intval', $excl_cat_ids); $row['excluded_need_ids'] = array_map('intval', $excl_need_ids); $row['excluded_brand_names'] = $resolve_term_names($excl_brand_ids, $brand_tax); $row['excluded_category_names'] = $resolve_term_names($excl_cat_ids, 'product_cat'); $row['excluded_need_names'] = $resolve_term_names($excl_need_ids, 'pa_need'); $row['product_ids'] = array_map('intval', $product_ids_pos); $row['excluded_product_ids'] = array_map('intval', $product_ids_neg); $row['product_tokens_text'] = AIEO_DMM_Offer_Matcher::format_product_tokens($product_ids_pos); $row['excluded_product_tokens_text'] = AIEO_DMM_Offer_Matcher::format_product_tokens($product_ids_neg); $row['product_products'] = AIEO_DMM_Product_Picker::hydrate_ids($product_ids_pos); $row['excluded_products'] = AIEO_DMM_Product_Picker::hydrate_ids($product_ids_neg); $row['code_patterns'] = AIEO_DMM_Pattern_Match::parse(json_decode($row['code_patterns'] ?? '[]', true) ?: array()); $row['excluded_code_patterns'] = AIEO_DMM_Pattern_Match::parse(json_decode($row['excluded_code_patterns'] ?? '[]', true) ?: array()); foreach (array('before_image_id', 'after_image_id') as $key) { $aid = isset($row[$key]) ? (int) $row[$key] : 0; $url = ''; if ($aid > 0) { $img = wp_get_attachment_image_src($aid, 'medium'); if ($img && !empty($img[0])) { $url = (string) $img[0]; } } $row[str_replace('_id', '_url', $key)] = $url; } foreach (array('bar_bg_before','bar_bg_after','bar_fg_before','bar_fg_after','gift_item_bg') as $ck) { $row[$ck] = isset($row[$ck]) ? (string) $row[$ck] : ''; } $row['show_banner'] = isset($row['show_banner']) && $row['show_banner'] !== null ? (int) $row['show_banner'] : 1; $row['show_rewards'] = isset($row['show_rewards']) && $row['show_rewards'] !== null ? (int) $row['show_rewards'] : 1; return $row; } public function enqueue_admin_assets() { wp_enqueue_media(); wp_enqueue_style('wp-color-picker'); wp_enqueue_script('wp-color-picker'); wp_add_inline_script('wp-color-picker', 'jQuery(function($){$(".aieo-dmm-bg-color").wpColorPicker();});' ); $css = 'assets/css/aieo-brand-gifts-admin.css'; wp_enqueue_style('aieo-brand-gifts-admin', AIEO_URI . $css, array(), file_exists(AIEO_DIR . $css) ? (string) filemtime(AIEO_DIR . $css) : '1'); } public function ajax_delete() { AIEO_DMM::verify_ajax_request(); $id = isset($_POST['id']) ? absint(wp_unslash($_POST['id'])) : 0; if ($id <= 0) { wp_send_json_error(array('message' => __('Invalid id.', 'ai-eshop-optimizer'))); } global $wpdb; require_once dirname(__DIR__) . '/services/class-aieo-dmm-offer-forms.php'; if (!AIEO_DMM_Offer_Forms::archive_old_row('aieo_dmm_brand_gift_offers', $id, 'deleted in the gift offers form')) { $wpdb->delete($wpdb->prefix . 'aieo_dmm_brand_gift_offers', array('id' => $id), array('%d')); AIEO_DMM_Offer_Forms::copy('aieo_dmm_brand_gift_offers', $id); AIEO_DMM_Cache::bump_version('gifts'); } self::prune_eligibility($id); AIEO_DMM_Offer_Pages::purge('gift'); wp_send_json_success(); } public function ajax_search_need_terms() { AIEO_DMM::verify_ajax_request(); $q = isset($_GET['q']) ? sanitize_text_field(wp_unslash($_GET['q'])) : ''; if (!taxonomy_exists('pa_need')) { wp_send_json(array('results' => array())); } $args = array('taxonomy' => 'pa_need', 'hide_empty' => false, 'number' => 50); if ($q !== '') { $args['name__like'] = $q; } $terms = get_terms($args); $results = array(); if (is_array($terms)) { foreach ($terms as $t) { $results[] = array( 'id' => (int) $t->term_id, 'text' => $t->name . ' (' . (int) $t->count . ')', ); } } wp_send_json(array('results' => $results)); } public function ajax_search_products() { AIEO_DMM::verify_ajax_request(); $q = isset($_GET['q']) ? sanitize_text_field(wp_unslash($_GET['q'])) : ''; $limit = 25; if (!function_exists('wc_get_products')) { wp_send_json(array('results' => array())); } $products = wc_get_products(array( 'status' => array('publish', 'private'), 'limit' => $limit, 's' => $q, 'orderby' => 'title', 'order' => 'ASC', )); $results = array(); foreach ($products as $product) { $results[] = array( 'id' => (int) $product->get_id(), 'text' => $product->get_name() . ($product->get_sku() ? ' [' . $product->get_sku() . ']' : ''), ); } wp_send_json(array('results' => $results)); } public function sync_gifts_deferred() { if (function_exists('WC') && WC()->cart && 'store-api' === (WC()->cart->cart_context ?? '')) { $this->sync_gifts(); return; } add_action('woocommerce_calculated_shipping', array($this, 'sync_gifts')); add_action('shutdown', array($this, 'sync_gifts')); } public function sync_gifts() { if ($this->is_syncing) { return; } if (!function_exists('WC') || !WC()->cart) { return; } if (self::the_pass_answers()) { AIEO_DMM_Cart_Pass::run(WC()->cart); return; } if (!did_action('wp_loaded')) { return; } $this->is_syncing = true; try { $offers = $this->get_active_offers(); $subtotals = empty($offers) ? array() : $this->compute_brand_subtotals($offers); $this->hold_gift_lines($offers, $subtotals); } finally { $this->is_syncing = false; } } public function hold_pass_gifts(array $counts) { if ($this->is_syncing || !function_exists('WC') || !WC()->cart) { return; } $this->is_syncing = true; try { $this->hold_gift_lines($this->get_active_offers(), $counts); } finally { $this->is_syncing = false; } } private function hold_gift_lines(array $offers, array $subtotals) { if (!did_action('wp_loaded')) { return; } foreach (WC()->cart->get_cart() as $item_key => $item) { if (empty($item['_aieo_dmm_gift_offer_id'])) { continue; } $offer_id = $this->gift_line_offer_id($item); if (!isset($subtotals[$offer_id]) || $subtotals[$offer_id]['qualified'] === false || self::is_spent_by_the_visitor($offer_id)) { WC()->cart->remove_cart_item($item_key); } } if (empty($offers)) { return; } foreach ($offers as $offer) { $offer_id = (int) $offer['id']; if (empty($subtotals[$offer_id]) || empty($subtotals[$offer_id]['qualified'])) { continue; } if (self::is_spent_by_the_visitor($offer_id)) { continue; } $choice = $this->gift_choice($offer); if (null === $choice) { continue; } list($target, $target_vid) = $choice; if (self::waits_for_variation($choice)) { $existing_key = $this->find_gift_cart_key($offer_id); if ($existing_key) { WC()->cart->remove_cart_item($existing_key); } continue; } $existing_key = $this->find_gift_cart_key($offer_id); if ($existing_key) { $existing = WC()->cart->get_cart_item($existing_key); $existing_pid = isset($existing['product_id']) ? (int) $existing['product_id'] : 0; $existing_vid = isset($existing['variation_id']) ? (int) $existing['variation_id'] : 0; if ($existing_pid === $target && $existing_vid === $target_vid) { if ((float) ($existing['quantity'] ?? 0) !== (float) self::quantity_of_counts($subtotals[$offer_id])) { WC()->cart->set_quantity($existing_key, self::quantity_of_counts($subtotals[$offer_id]), false); } continue; } WC()->cart->remove_cart_item($existing_key); } $variation_attrs = array(); if ($target_vid > 0) { $variation = wc_get_product($target_vid); if ($variation && $variation->is_type('variation')) { $variation_attrs = $variation->get_variation_attributes(); } } $gift_data = array('_aieo_dmm_gift_offer_id' => $offer_id); if (class_exists('AIEO_DMM_Offer_Store') && AIEO_DMM_Offer_Store::reads_unified()) { $gift_data['_aieo_offer_store'] = 'of'; } self::$is_internal_add = true; try { WC()->cart->add_to_cart($target, self::quantity_of_counts($subtotals[$offer_id]), $target_vid, $variation_attrs, $gift_data); } finally { self::$is_internal_add = false; } } } public function gives_gift(array $offer) { $choice = $this->gift_choice($offer); if (null === $choice) { return '' !== $this->find_gift_cart_key((int) $offer['id']); } return !self::waits_for_variation($choice); } private function gift_choice(array $offer) { $gift_ids = isset($offer['gift_product_ids']) ? (array) $offer['gift_product_ids'] : array(); $valid = $gift_ids ? $this->filter_addable_gifts($gift_ids) : array(); if (empty($valid)) { return null; } $offer_id = (int) $offer['id']; $picked = $this->get_picked_gift($offer_id); $picked_vid = $this->get_picked_variation($offer_id); if ($picked > 0 && !in_array($picked, $valid, true)) { $picked = 0; $picked_vid = 0; } $target = $picked > 0 ? $picked : (int) $valid[0]; return array($target, $target === $picked ? $picked_vid : 0); } private static function waits_for_variation(array $choice) { if ((int) $choice[1] > 0) { return false; } $product = wc_get_product((int) $choice[0]); return $product && $product->is_type('variable'); } public function remove_gift_lines() { if (!function_exists('WC') || !WC()->cart) { return; } foreach (WC()->cart->get_cart_contents() as $item_key => $item) { if (!empty($item['_aieo_dmm_gift_offer_id'])) { WC()->cart->remove_cart_item($item_key); } } } private function compute_brand_subtotals(array $offers) { $by_definition = array(); $by_columns = array(); foreach ($offers as $offer) { if (!empty($offer['_definition']) && empty($offer['_pinned'])) { $by_definition[] = $offer; } else { $by_columns[] = $offer; } } $out = $by_columns ? $this->compute_brand_subtotals_by_columns($by_columns) : array(); if (!$by_definition) { return $out; } $cart_items = WC()->cart->get_cart(); $cart_total = 0.0; foreach ($cart_items as $_item) { if (!empty($_item['_aieo_dmm_gift_offer_id'])) { continue; } if (!isset($_item['data']) || !is_object($_item['data'])) { continue; } $cart_total += (float) $_item['data']->get_price() * (int) ($_item['quantity'] ?? 0); } $answered = array(); foreach (AIEO_DMM_Offer_Matcher::for_cart($cart_items, $by_definition, array('sum' => 'price')) as $answer) { $answered[(int) $answer['id']] = $answer; } foreach ($by_definition as $offer) { $offer_id = (int) $offer['id']; $answer = isset($answered[$offer_id]) ? $answered[$offer_id] : null; $subtotal = $answer ? (float) $answer['_targeted_subtotal'] : 0.0; $out[$offer_id] = array( 'subtotal' => $subtotal, 'cart_total' => $cart_total, 'qualified' => $answer && !empty($answer['_qualified']) && $subtotal > 0, ); } return $out; } public function pass_gift_counts(array $amounts, $customer = null) { $offers = $this->get_active_offers(); if (!$offers || !function_exists('WC') || !WC()->cart) { return array('offers' => array(), 'counts' => array()); } $by_definition = array(); $by_columns = array(); foreach ($offers as $offer) { if (!empty($offer['_definition']) && empty($offer['_pinned'])) { $by_definition[] = $offer; } else { $by_columns[] = $offer; } } $out = $by_columns ? $this->compute_brand_subtotals_by_columns($by_columns, $amounts) : array(); if ($by_definition) { $lines = array(); $cart_total = 0.0; foreach (WC()->cart->get_cart() as $key => $item) { if (!empty($item['_aieo_dmm_gift_offer_id']) || !isset($amounts[$key])) { continue; } $item['line_subtotal'] = (float) $amounts[$key]; $lines[$key] = $item; $cart_total += (float) $amounts[$key]; } $answered = array(); foreach (AIEO_DMM_Offer_Matcher::for_cart($lines, $by_definition, is_array($customer) ? array('customer' => $customer) : array()) as $answer) { $answered[(int) $answer['id']] = $answer; } foreach ($by_definition as $offer) { $offer_id = (int) $offer['id']; $answer = isset($answered[$offer_id]) ? $answered[$offer_id] : null; $subtotal = $answer ? (float) $answer['_targeted_subtotal'] : 0.0; $sets = empty($offer['_definition']['bundle']) ? null : AIEO_DMM_Cart_Pass::complete_sets((array) $offer['_definition'], $lines); $out[$offer_id] = array( 'subtotal' => $subtotal, 'cart_total' => $cart_total, 'qualified' => $answer && !empty($answer['_qualified']) && $subtotal > 0 && (null === $sets || $sets > 0), ); if (null !== $sets) { $out[$offer_id]['sets'] = $sets; } } } return array('offers' => $offers, 'counts' => $out); } private function gift_counts(array $offers) { if (!self::the_pass_answers()) { return $this->compute_brand_subtotals($offers); } $counts = AIEO_DMM_Cart_Pass::gift_counts(); $out = array(); foreach ($offers as $offer) { $offer_id = (int) $offer['id']; if (isset($counts[$offer_id])) { $out[$offer_id] = $counts[$offer_id]; } } return $out; } private function compute_brand_subtotals_by_columns(array $offers, $amounts = null) { $out = array(); $cart_items = WC()->cart->get_cart(); $taxonomy = class_exists('AIEO_DMM_Brand_Discounts') ? AIEO_DMM_Brand_Discounts::get_brand_taxonomy() : 'product_brand'; if (!$taxonomy) { $taxonomy = 'product_brand'; } $cart_total = 0.0; foreach ($cart_items as $_key => $_item) { if (!empty($_item['_aieo_dmm_gift_offer_id'])) { continue; } if (!isset($_item['data']) || !is_object($_item['data'])) { continue; } $cart_total += null === $amounts ? (float) $_item['data']->get_price() * (int) ($_item['quantity'] ?? 0) : (float) ($amounts[$_key] ?? 0); } $dec_list = static function ($v) { if (is_array($v)) { return array_values(array_filter(array_map('intval', $v))); } if (is_string($v) && $v !== '') { $d = json_decode($v, true); return is_array($d) ? array_values(array_filter(array_map('intval', $d))) : array(); } return array(); }; foreach ($offers as $offer) { $offer_id = (int) $offer['id']; $brand_ids = is_array($offer['brand_term_ids']) ? array_map('intval', $offer['brand_term_ids']) : array(); $category_ids = is_array($offer['category_term_ids']) ? array_map('intval', $offer['category_term_ids']) : array(); $need_ids = is_array($offer['need_term_ids'] ?? null) ? array_map('intval', $offer['need_term_ids']) : array(); $operator = (isset($offer['rule_operator']) && strtoupper($offer['rule_operator']) === 'OR') ? 'OR' : 'AND'; $excl_brand = $dec_list($offer['excluded_brand_ids'] ?? null); $excl_cat = $dec_list($offer['excluded_category_ids'] ?? null); $excl_need = $dec_list($offer['excluded_need_ids'] ?? null); $excl_prod = $dec_list($offer['excluded_product_ids'] ?? null); $req_prod = $dec_list($offer['product_ids'] ?? null); $no_targeting = empty($brand_ids) && empty($category_ids) && empty($need_ids); $subtotal = 0.0; foreach ($cart_items as $item_key => $item) { if (!empty($item['_aieo_dmm_gift_offer_id'])) { continue; } $pid = !empty($item['variation_id']) ? (int) $item['variation_id'] : (int) $item['product_id']; $matches = $no_targeting; if (!$no_targeting) { $brand_terms = wp_get_object_terms($pid, $taxonomy, array('fields' => 'ids')); if (is_wp_error($brand_terms)) { $brand_terms = array(); } $cat_terms = wp_get_object_terms($pid, 'product_cat', array('fields' => 'ids')); if (is_wp_error($cat_terms)) { $cat_terms = array(); } $need_terms = array(); if (!empty($need_ids) && taxonomy_exists('pa_need')) { $need_terms = wp_get_object_terms($pid, 'pa_need', array('fields' => 'ids')); if (is_wp_error($need_terms)) { $need_terms = array(); } } if ($operator === 'OR') { $matches = (!empty($brand_ids) && !empty($brand_terms) && array_intersect($brand_terms, $brand_ids)) || (!empty($category_ids) && !empty($cat_terms) && array_intersect($cat_terms, $category_ids)) || (!empty($need_ids) && !empty($need_terms) && array_intersect($need_terms, $need_ids)); } else { $brand_match = empty($brand_ids) || (!empty($brand_terms) && array_intersect($brand_terms, $brand_ids)); $cat_match = empty($category_ids) || (!empty($cat_terms) && array_intersect($cat_terms, $category_ids)); $need_match = empty($need_ids) || (!empty($need_terms) && array_intersect($need_terms, $need_ids)); $matches = $brand_match && $cat_match && $need_match; } } if ($matches) { $unit_price = (isset($item['data']) && is_object($item['data'])) ? (float) $item['data']->get_price() : 0.0; $qty = (int) ($item['quantity'] ?? 0); $subtotal += null === $amounts ? $unit_price * $qty : (float) ($amounts[$item_key] ?? 0); } } $vetoed = false; $req_met = empty($req_prod); if (!empty($excl_brand) || !empty($excl_cat) || !empty($excl_need) || !empty($excl_prod) || !empty($req_prod)) { foreach ($cart_items as $xitem) { if (!empty($xitem['_aieo_dmm_gift_offer_id'])) { continue; } $xpid = !empty($xitem['variation_id']) ? (int) $xitem['variation_id'] : (int) ($xitem['product_id'] ?? 0); $xparent = (int) ($xitem['product_id'] ?? $xpid); $xself = array($xpid, $xparent); if (!empty($req_prod) && array_intersect($xself, $req_prod)) { $req_met = true; } if (!empty($excl_prod) && array_intersect($xself, $excl_prod)) { $vetoed = true; break; } if (!empty($excl_brand)) { $xb = wp_get_object_terms($xparent, $taxonomy, array('fields' => 'ids')); if (!is_wp_error($xb) && array_intersect(array_map('intval', (array) $xb), $excl_brand)) { $vetoed = true; break; } } if (!empty($excl_cat)) { $xc = wp_get_object_terms($xparent, 'product_cat', array('fields' => 'ids')); if (!is_wp_error($xc) && array_intersect(array_map('intval', (array) $xc), $excl_cat)) { $vetoed = true; break; } } if (!empty($excl_need) && taxonomy_exists('pa_need')) { $xn = wp_get_object_terms($xparent, 'pa_need', array('fields' => 'ids')); if (!is_wp_error($xn) && array_intersect(array_map('intval', (array) $xn), $excl_need)) { $vetoed = true; break; } } } } $threshold = (float) $offer['min_subtotal']; $has_match = ($subtotal > 0); if ($operator === 'OR') { $price_or = ($threshold > 0 && $cart_total >= $threshold); $qualified = $has_match || $price_or; if (!$has_match && !$price_or) { $qualified = false; } } else { if ($no_targeting) { $qualified = ($subtotal >= $threshold) && ($subtotal > 0); } else { $qualified = $has_match && ($subtotal >= $threshold); } } if ($qualified && ($vetoed || !$req_met)) { $qualified = false; } $out[$offer_id] = array( 'subtotal' => $subtotal, 'cart_total' => $cart_total, 'qualified' => $qualified, ); } return $out; } private function cart_has_gift_for_offer($offer_id) { foreach (WC()->cart->get_cart() as $item) { if (!empty($item['_aieo_dmm_gift_offer_id']) && $this->gift_line_offer_id($item) === (int) $offer_id) { return true; } } return false; } private function gift_text($offer, $key, $default_template, $tokens, $plain_remaining = false) { $template = isset($offer[$key]) ? trim((string) $offer[$key]) : ''; if ($template === '') { $template = $default_template; } $remaining = isset($tokens['remaining']) ? (float) $tokens['remaining'] : 0.0; $brand = isset($tokens['brand']) ? (string) $tokens['brand'] : ''; $price_str = wc_price($remaining); if ($plain_remaining) { $price_str = wp_strip_all_tags($price_str); } return strtr($template, array( '{remaining}' => $price_str, '{brand}' => $brand, )); } public function set_gift_prices($cart) { if (!is_a($cart, 'WC_Cart')) { return; } foreach ($cart->get_cart() as $item) { if (!empty($item['_aieo_dmm_gift_offer_id']) && isset($item['data']) && is_object($item['data'])) { $item['data']->set_price(0); } } } public function zero_gift_line_price($price, $product = null) { return $this->is_gift_line_product($product) ? 0.0 : $price; } private function is_sample_product($product_or_id) { if (is_numeric($product_or_id)) { $pid = (int) $product_or_id; } elseif (is_object($product_or_id) && method_exists($product_or_id, 'get_id')) { $pid = (int) $product_or_id->get_id(); } else { return false; } if ($pid <= 0) { return false; } return get_post_meta($pid, '_aieo_gift_type', true) === 'sample'; } public function filter_sample_unpurchasable($purchasable, $product) { if (!$purchasable) { return $purchasable; } if (self::$is_internal_add) { return $purchasable; } if ($this->is_sample_product($product) && !$this->is_gift_line_product($product)) { return false; } return $purchasable; } private function is_gift_line_product($product) { if (!is_object($product) || !function_exists('WC') || !WC()->cart) { return false; } foreach (WC()->cart->get_cart_contents() as $item) { if (!empty($item['_aieo_dmm_gift_offer_id']) && isset($item['data']) && $item['data'] === $product) { return true; } } return false; } public function keep_sample_gift_line($purchasable, $key = '', $values = array(), $product = null) { if ($purchasable || !is_array($values) || empty($values['_aieo_dmm_gift_offer_id'])) { return $purchasable; } if (!is_object($product) || !method_exists($product, 'is_purchasable') || !$this->is_sample_product($product)) { return $purchasable; } $was = self::$is_internal_add; self::$is_internal_add = true; try { return (bool) $product->is_purchasable(); } finally { self::$is_internal_add = $was; } } public function exclude_samples_from_archive($q) { if (!($q instanceof WP_Query)) { return; } $post_in = $q->get('post__in'); if (!empty($post_in)) { return; } if (function_exists('doing_action') && doing_action('dynamic_sidebar')) { return; } $sample_ids = $this->sample_product_ids(); if (!empty($sample_ids)) { $not_in = array_map('intval', (array) $q->get('post__not_in')); $q->set('post__not_in', array_values(array_unique(array_merge($not_in, $sample_ids)))); } } public function sample_product_ids() { $ids = get_transient('aieo_bg_sample_ids'); if (is_array($ids)) { return $ids; } global $wpdb; $ids = $wpdb->get_col($wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %s", '_aieo_gift_type', 'sample' )); $ids = array_map('intval', (array) $ids); set_transient('aieo_bg_sample_ids', $ids, 6 * HOUR_IN_SECONDS); return $ids; } public function flush_sample_ids_cache() { delete_transient('aieo_bg_sample_ids'); } public function exclude_samples_from_search($q) { if (is_admin() || !($q instanceof WP_Query) || !$q->is_main_query() || !$q->is_search()) { return; } $this->exclude_samples_from_archive($q); } public function render_gift_type_field() { global $post; $current = $post ? (string) get_post_meta($post->ID, '_aieo_gift_type', true) : ''; woocommerce_wp_select(array( 'id' => '_aieo_gift_type', 'label' => __('Free gift type', 'ai-eshop-optimizer'), 'description' => __('"Sample" hides this product from archives + search and prevents direct purchase. The Free Gifts module can still add it to the cart automatically when offer conditions are met.', 'ai-eshop-optimizer'), 'desc_tip' => true, 'value' => $current, 'options' => array( '' => __('Regular product', 'ai-eshop-optimizer'), 'sample' => __('Sample (gift only — not purchasable)', 'ai-eshop-optimizer'), ), )); } public function save_gift_type_field($post_id) { $val = isset($_POST['_aieo_gift_type']) ? sanitize_key(wp_unslash($_POST['_aieo_gift_type'])) : ''; if ($val === 'sample') { update_post_meta($post_id, '_aieo_gift_type', 'sample'); } else { delete_post_meta($post_id, '_aieo_gift_type'); } } public function sync_sample_visibility($product_id) { static $busy = false; if ($busy || !function_exists('wc_get_product')) { return; } $product = wc_get_product((int) $product_id); if (!$product || $product->is_type('variation')) { return; } $pid = (int) $product->get_id(); $current = $product->get_catalog_visibility(); $prev = (string) get_post_meta($pid, '_aieo_sample_prev_visibility', true); $busy = true; if ($this->is_sample_product($pid)) { if ('hidden' !== $current) { update_post_meta($pid, '_aieo_sample_prev_visibility', $current); $product->set_catalog_visibility('hidden'); $product->save(); } } elseif ('' !== $prev) { delete_post_meta($pid, '_aieo_sample_prev_visibility'); if ('hidden' === $current) { $product->set_catalog_visibility($prev); $product->save(); } } $busy = false; } public function migrate_sample_visibility() { if (get_option('aieo_bg_sample_visibility_v1')) { return; } update_option('aieo_bg_sample_visibility_v1', 1, false); delete_transient('aieo_bg_sample_ids'); foreach ($this->sample_product_ids() as $pid) { $this->sync_sample_visibility($pid); } } public function stamp_gift_order_line($item, $cart_item_key, $values) { if (!is_array($values) || empty($values['_aieo_dmm_gift_offer_id'])) { return; } $offer_id = (int) $values['_aieo_dmm_gift_offer_id']; if ($offer_id <= 0 || !is_object($item) || !method_exists($item, 'add_meta_data') || !method_exists($item, 'get_meta')) { return; } if (!empty($values['_aieo_offer_store']) && '' === (string) $item->get_meta('_aieo_offer_store', true)) { $item->add_meta_data('_aieo_offer_store', sanitize_key((string) $values['_aieo_offer_store']), true); } if ((int) $item->get_meta('_aieo_dmm_gift_offer_id', true) > 0) { return; } $item->add_meta_data('_aieo_dmm_gift_offer_id', $offer_id, true); } public function mark_order_again_gift_line($cart_item_data, $item = null) { if (!is_object($item) || !method_exists($item, 'get_meta')) { return $cart_item_data; } $offer_id = (int) $item->get_meta('_aieo_dmm_gift_offer_id', true); if ($offer_id <= 0) { return $cart_item_data; } $cart_item_data = is_array($cart_item_data) ? $cart_item_data : array(); $cart_item_data[self::ORDER_AGAIN_GIFT] = $offer_id; return $cart_item_data; } public function drop_order_again_gift_line($remove, $key = '', $values = array()) { return $remove || (is_array($values) && !empty($values[self::ORDER_AGAIN_GIFT])); } public function keep_gift_quantity($cart_item_key, $quantity = 0, $old_quantity = 0, $cart = null) { if (!is_a($cart, 'WC_Cart')) { $cart = function_exists('WC') ? WC()->cart : null; } if (!$cart || !is_string($cart_item_key) || empty($cart->cart_contents[$cart_item_key]['_aieo_dmm_gift_offer_id'])) { return; } $wanted = $this->gift_line_quantity($cart->cart_contents[$cart_item_key]); if ((float) $cart->cart_contents[$cart_item_key]['quantity'] !== (float) $wanted) { $cart->cart_contents[$cart_item_key]['quantity'] = $wanted; } } private static function quantity_of_counts($counts) { $sets = is_array($counts) && isset($counts['sets']) ? (int) $counts['sets'] : 1; return self::GIFT_QUANTITY * max(1, $sets); } public function gift_line_quantity($item) { if (!self::the_pass_answers()) { return self::GIFT_QUANTITY; } $counts = AIEO_DMM_Cart_Pass::gift_counts(); $id = $this->gift_line_offer_id($item); return self::quantity_of_counts(isset($counts[$id]) ? $counts[$id] : null); } public function gift_quantity_limit($value, $product = null, $cart_item = null) { if (!is_array($cart_item) || empty($cart_item['_aieo_dmm_gift_offer_id'])) { return $value; } return 'woocommerce_store_api_product_quantity_editable' === current_filter() ? false : $this->gift_line_quantity($cart_item); } public function lock_gift_quantity($qty_html, $item_key, $item) { if (!empty($item['_aieo_dmm_gift_offer_id'])) { return (string) $this->gift_line_quantity($item); } return $qty_html; } public function add_gift_badge($name, $item, $item_key) { if (!empty($item['_aieo_dmm_gift_offer_id'])) { $badge = '' . esc_html__('Free Gift', 'ai-eshop-optimizer') . ' '; return $badge . $name; } return $name; } public function hide_gift_remove_link($link_html, $item_key) { if (!function_exists('WC') || !WC()->cart) { return $link_html; } $item = WC()->cart->get_cart_item($item_key); if (!empty($item['_aieo_dmm_gift_offer_id'])) { return ''; } return $link_html; } private function pick_session_key($offer_id) { return 'aieo_dmm_gift_pick_' . (int) $offer_id; } public function get_picked_gift($offer_id) { $entry = $this->get_picked_entry($offer_id); return (int) $entry['product_id']; } public function get_picked_variation($offer_id) { $entry = $this->get_picked_entry($offer_id); return (int) $entry['variation_id']; } private function get_picked_entry($offer_id) { $default = array('product_id' => 0, 'variation_id' => 0); if (!function_exists('WC') || !WC()->session) { return $default; } $v = WC()->session->get($this->pick_session_key($offer_id), 0); if (empty($v) && class_exists('AIEO_DMM_Offer_Store') && AIEO_DMM_Offer_Store::reads_unified()) { foreach ($this->get_active_offers() as $offer) { if ((int) $offer['id'] === (int) $offer_id && !empty($offer['_legacy_id'])) { $v = WC()->session->get($this->pick_session_key((int) $offer['_legacy_id']), 0); break; } } } if (is_array($v)) { return array( 'product_id' => isset($v['product_id']) ? (int) $v['product_id'] : 0, 'variation_id' => isset($v['variation_id']) ? (int) $v['variation_id'] : 0, ); } return array('product_id' => (int) $v, 'variation_id' => 0); } public function set_picked_gift($offer_id, $product_id, $variation_id = 0) { if (!function_exists('WC') || !WC()->session) { return; } $pick = array( 'product_id' => (int) $product_id, 'variation_id' => (int) $variation_id, ); WC()->session->set($this->pick_session_key($offer_id), $pick); if (class_exists('AIEO_DMM_Offer_Store') && AIEO_DMM_Offer_Store::reads_unified()) { foreach ($this->get_active_offers() as $offer) { if ((int) $offer['id'] === (int) $offer_id && !empty($offer['_legacy_id'])) { WC()->session->set($this->pick_session_key((int) $offer['_legacy_id']), $pick); break; } } } } public function get_offers_for_product($product_id) { $product_id = (int) $product_id; if ($product_id <= 0) { return array(); } if (class_exists('AIEO_Catalogue_Mode') && AIEO_Catalogue_Mode::cart_off($product_id)) { return array(); } $offers = $this->shown_offers($this->get_active_offers(), false); if (empty($offers)) { return array(); } return AIEO_DMM_Offer_Matcher::for_product($product_id, $offers); } private function cart_bar_offers(array $offers) { if (!class_exists('AIEO_DMM_Offer_Display')) { return $offers; } return array_values(array_filter($offers, static function ($offer) { return !is_array($offer) || !isset($offer['_definition']) || !is_array($offer['_definition']) || AIEO_DMM_Offer_Display::shows($offer, 'cart_bar'); })); } private function shown_offers(array $offers, $own) { $viewer = null; $out = array(); foreach ($offers as $offer) { if (!empty($offer['_definition']['bundle'])) { $earned = $own && class_exists('AIEO_DMM_Cart_Pass') ? AIEO_DMM_Cart_Pass::gift_counts() : array(); if (empty($earned[(int) $offer['id']]['qualified'])) { continue; } } $when = isset($offer['_definition']['when']) && is_array($offer['_definition']['when']) ? $offer['_definition']['when'] : null; if (null !== $when && AIEO_DMM_Offer_Definition::names_the_customer($when)) { if (!$own) { continue; } if (null === $viewer) { $viewer = class_exists('AIEO_DMM_Cart_Pass') ? AIEO_DMM_Cart_Pass::who() : AIEO_DMM_Offer_Matcher::customer_of((int) get_current_user_id()); } if (!AIEO_DMM_Offer_Matcher::holds_but_the_cart($when, $viewer)) { continue; } } $out[] = $offer; } return $out; } public static function add_eligibility_row($rows, $product_id, $audience_key) { if (!is_array($rows)) { $rows = array(); } $product_id = (int) $product_id; if ($product_id === 0) { return $rows; } if (!class_exists('AIEO_Effective_Eligibility')) { return $rows; } $rows[AIEO_Effective_Eligibility::SURFACE_BRAND_GIFT] = self::is_eligible_for($product_id, (string) $audience_key); return $rows; } public static function on_date_boundary($domains = array()) { self::prune_eligibility(0, in_array('gifts', (array) $domains, true)); } private static function prune_eligibility($changed_offer_id = 0, $and_ineligible = false) { if (!class_exists('AIEO_Effective_Eligibility')) { return; } $surface = AIEO_Effective_Eligibility::SURFACE_BRAND_GIFT; if ((int) $changed_offer_id > 0) { AIEO_Effective_Eligibility::delete_for_offer((int) $changed_offer_id, $surface); } $running = array(); if (self::switched_on()) { foreach ((array) self::instance()->get_active_offers() as $offer) { $running[] = isset($offer['id']) ? (int) $offer['id'] : 0; } } AIEO_Effective_Eligibility::prune($surface, $running, $and_ineligible); } public static function is_eligible_for($product_id, $audience_key = 'guest') { $product_id = (int) $product_id; if ($product_id <= 0) { return array('eligible' => false); } if (!self::switched_on()) { return array('eligible' => false); } $instance = self::instance(); if (!$instance) { return array('eligible' => false); } $matches = $instance->get_offers_for_product($product_id); if (empty($matches) || !is_array($matches)) { return array('eligible' => false); } $offer = reset($matches); if (!is_array($offer)) { return array('eligible' => false); } return array( 'eligible' => true, 'offer_id' => isset($offer['id']) ? (int) $offer['id'] : null, 'payload' => array( 'label' => isset($offer['label']) ? (string) $offer['label'] : '', 'min_subtotal' => isset($offer['min_subtotal']) ? (float) $offer['min_subtotal'] : null, ), ); } private function filter_addable_gifts(array $gift_ids) { $out = array(); foreach ($gift_ids as $pid) { $pid = (int) $pid; if ($pid <= 0) { continue; } $p = wc_get_product($pid); if (!$p || !$p->exists() || !$p->is_in_stock()) { continue; } if (in_array($p->get_type(), array('grouped', 'external'), true)) { continue; } $was = self::$is_internal_add; self::$is_internal_add = true; try { $buyable = (bool) $p->is_purchasable(); } finally { self::$is_internal_add = $was; } if (!$buyable) { continue; } $out[] = $pid; } return $out; } private function find_gift_cart_key($offer_id) { if (!function_exists('WC') || !WC()->cart) { return ''; } foreach (WC()->cart->get_cart() as $key => $item) { if (!empty($item['_aieo_dmm_gift_offer_id']) && $this->gift_line_offer_id($item) === (int) $offer_id) { return (string) $key; } } return ''; } public function ajax_pick_gift() { check_ajax_referer('aieo_side_cart', 'nonce'); $offer_id = isset($_POST['offer_id']) ? absint(wp_unslash($_POST['offer_id'])) : 0; $product_id = isset($_POST['product_id']) ? absint(wp_unslash($_POST['product_id'])) : 0; $variation_id = isset($_POST['variation_id']) ? absint(wp_unslash($_POST['variation_id'])) : 0; if ($offer_id <= 0 || $product_id <= 0) { wp_send_json_error(array('message' => __('Bad pick.', 'ai-eshop-optimizer'))); } $offers = $this->get_active_offers(); $valid_pids = array(); foreach ($offers as $o) { if ((int) $o['id'] === $offer_id) { $valid_pids = array_map('intval', $o['gift_product_ids']); break; } } if (!in_array($product_id, $valid_pids, true)) { wp_send_json_error(array('message' => __('Product not in this offer.', 'ai-eshop-optimizer'))); } $product = wc_get_product($product_id); if ($product && $product->is_type('variable')) { if ($variation_id <= 0) { $this->set_picked_gift($offer_id, $product_id, 0); wp_send_json_success(array('picked' => $product_id, 'needs_variation' => true)); } $variation = wc_get_product($variation_id); if (!$variation || !$variation->is_type('variation') || (int) $variation->get_parent_id() !== $product_id) { wp_send_json_error(array('message' => __('Invalid variation.', 'ai-eshop-optimizer'))); } } else { $variation_id = 0; } $this->set_picked_gift($offer_id, $product_id, $variation_id); $this->sync_gifts(); wp_send_json_success(array('picked' => $product_id, 'variation_id' => $variation_id)); } public function ajax_get_variations() { check_ajax_referer('aieo_side_cart', 'nonce'); $product_id = isset($_POST['product_id']) ? absint(wp_unslash($_POST['product_id'])) : 0; wp_send_json_success(array('variations' => $this->get_variations_for_product($product_id))); } public function get_variations_for_product($product_id) { $product = (int) $product_id > 0 ? wc_get_product((int) $product_id) : null; if (!$product || !$product->is_type('variable')) { return array(); } $out = array(); foreach ($product->get_available_variations() as $v) { if (empty($v['is_purchasable']) || empty($v['is_in_stock'])) { continue; } $label_parts = array(); foreach ((array) $v['attributes'] as $attr_key => $attr_val) { if ($attr_val === '' || $attr_val === null) { continue; } $taxonomy = preg_replace('/^attribute_/', '', (string) $attr_key); $is_global = (strpos($taxonomy, 'pa_') === 0); $value_display = (string) $attr_val; if ($is_global && taxonomy_exists($taxonomy)) { $term = get_term_by('slug', $attr_val, $taxonomy); if ($term && !is_wp_error($term)) { $value_display = (string) $term->name; } } $key_display = function_exists('wc_attribute_label') ? wc_attribute_label($taxonomy) : ucfirst(str_replace(array('-', '_'), ' ', $taxonomy)); $label_parts[] = $key_display . ': ' . $value_display; } $out[] = array( 'variation_id' => (int) $v['variation_id'], 'label' => implode(' / ', $label_parts) ?: ('#' . (int) $v['variation_id']), ); } return $out; } public function render_side_cart_section() { $offers = $this->cart_bar_offers($this->shown_offers($this->get_active_offers(), true)); if (empty($offers)) { return ''; } $subtotals = $this->gift_counts($offers); ob_start(); foreach ($offers as $offer) { if (isset($offer['show_banner']) && (int) $offer['show_banner'] === 0) { continue; } $offer_id = (int) $offer['id']; $info = isset($subtotals[$offer_id]) ? $subtotals[$offer_id] : array('subtotal' => 0, 'qualified' => false); $qualified = !empty($info['qualified']); $current = (float) $info['subtotal']; $threshold = (float) ($offer['min_subtotal'] ?? 0); $remaining = max(0, $threshold - $current); $percent = $threshold > 0 ? min(100, ($current / $threshold) * 100) : 0; $brand_names = $this->resolve_scope_names_html($offer); $picked = $this->get_picked_gift($offer_id); $picked_pid = $picked > 0 ? $picked : (int) (!empty($offer['gift_product_ids']) ? $offer['gift_product_ids'][0] : 0); $picked_name = ''; if ($picked_pid > 0) { $picked_p = wc_get_product($picked_pid); if ($picked_p) { $picked_name = $picked_p->get_name(); } } $status = array( 'remaining' => $remaining, 'threshold' => $threshold, 'current' => $current, 'percent' => $percent, 'qualified' => $qualified, ); $text = (string) ($qualified ? ($offer['after_text'] ?? '') : ($offer['before_text'] ?? '')); $text = $this->parse_placeholders($text, $status, $brand_names, $picked_name); $spent = self::is_spent_by_the_visitor($offer_id); if ($spent) { $text = esc_html(self::spent_words()); $qualified = false; } $img_id = $spent ? 0 : ($qualified ? (int) ($offer['after_image_id'] ?? 0) : (int) ($offer['before_image_id'] ?? 0)); $image_url = ''; if ($img_id > 0) { $image_url = (string) wp_get_attachment_image_url($img_id, 'thumbnail'); } $class = $qualified ? 'om-bg-qualified' : ''; $addable = $this->filter_addable_gifts((array) ($offer['gift_product_ids'] ?? array())); $current_pick = $picked_pid; if ($current_pick > 0 && !in_array($current_pick, $addable, true)) { $current_pick = !empty($addable) ? (int) $addable[0] : 0; } $style_parts = array(); $bar_bg = $qualified ? ($offer['bar_bg_after'] ?? '') : ($offer['bar_bg_before'] ?? ''); $bar_fg = $qualified ? ($offer['bar_fg_after'] ?? '') : ($offer['bar_fg_before'] ?? ''); if ($bar_bg) { $style_parts[] = 'background:' . $bar_bg; } if ($bar_fg) { $style_parts[] = '--om-bg-bar-fg:' . $bar_fg; } $style_attr = $style_parts ? ' style="' . esc_attr(implode(';', $style_parts)) . '"' : ''; $seen = class_exists('AIEO_Sighting') ? AIEO_Sighting::attrs('cart_bar', $offer_id, 'go', $threshold > 0 ? $percent / 100 : null) : ''; ?>
>
0 && !$spent): ?>
1 && !$rewards_shown): ?>
0) { $cp = wc_get_product($current_pick); if ($cp) { $current_label = $cp->get_name(); } } $named = $current_label !== '' && false !== stripos(wp_strip_all_tags((string) $text), $current_label); ?>
get_id())) { return; } echo $this->archive_badge_html($product); } public function archive_badge_html($product) { if (class_exists('AIEO_DMM_Badge_Renderer') && AIEO_DMM_Badge_Renderer::is_enabled()) { return ''; } if (!$product || !is_a($product, 'WC_Product')) { return ''; } $matched = $this->get_offers_for_product($product->get_id()); if (empty($matched)) { return ''; } $label = apply_filters('aieo_dmm_brand_gifts_archive_badge_label', __('+ Free gift', 'ai-eshop-optimizer')); $set_attr = class_exists('AIEO_DMM_Offer_Sets') ? AIEO_DMM_Offer_Sets::card_attr((int) $product->get_id()) : ''; return '' . ' ' . '' . esc_html($label) . '' . ''; } private static $archive_banner_rendered = false; public function render_archive_banner() { if (self::$archive_banner_rendered) { return; } $banner = $this->build_archive_banner_html(); if ($banner === '') { return; } self::$archive_banner_rendered = true; echo $banner; } public function inject_archive_banner_into_block($block_content, $block) { if (self::$archive_banner_rendered) { return $block_content; } $banner = $this->build_archive_banner_html(); if ($banner === '') { return $block_content; } self::$archive_banner_rendered = true; return $banner . $block_content; } private function build_archive_banner_html() { $term = null; $tax = ''; if (function_exists('is_tax') && is_tax(array('pa_need'))) { $term = get_queried_object(); $tax = 'pa_need'; } elseif (function_exists('is_product_category') && is_product_category()) { $term = get_queried_object(); $tax = 'product_cat'; } elseif (function_exists('is_tax')) { $brand_tax = class_exists('AIEO_DMM_Brand_Discounts') ? AIEO_DMM_Brand_Discounts::get_brand_taxonomy() : 'product_brand'; if ($brand_tax && is_tax($brand_tax)) { $term = get_queried_object(); $tax = $brand_tax; } } if (!$term || is_wp_error($term) || !isset($term->term_id) || $tax === '') { return ''; } $offers = $this->shown_offers($this->get_active_offers(), false); if (empty($offers)) { return ''; } $matched = AIEO_DMM_Offer_Matcher::for_term($tax, (int) $term->term_id, $offers); if (empty($matched)) { return ''; } $offer = $matched[0]; $offer_id = (int) ($offer['id'] ?? 0); $threshold = (float) ($offer['min_subtotal'] ?? 0); $brand_html = $this->resolve_scope_names_html($offer); $tokens = array('remaining' => $threshold, 'brand' => $brand_html); $headline = $this->gift_text($offer, 'headline_pending', '' !== $brand_html ? __('Free gift waiting — spend {remaining} more on {brand}', 'ai-eshop-optimizer') : __('Free gift waiting — spend {remaining} more', 'ai-eshop-optimizer'), $tokens); $addable = $this->filter_addable_gifts((array) ($offer['gift_product_ids'] ?? array())); $thumbs_html = ''; $shown = 0; foreach ($addable as $gpid) { if ($shown >= 3) { break; } $thumb = get_the_post_thumbnail_url($gpid, 'thumbnail'); if (!$thumb) { continue; } $thumbs_html .= ''; $shown++; } $extra = max(0, count($addable) - $shown); if ($extra > 0) { $thumbs_html .= '+' . esc_html(number_format_i18n($extra)) . ''; } $bg = (string) ($offer['bar_bg_before'] ?? ''); $style = $bg ? ' style="background:' . esc_attr($bg) . ';"' : ''; $html = '
'; $html .= '
' . self::gift_line(wp_kses_post($headline)) . '
'; if ($thumbs_html !== '') { $html .= '
' . $thumbs_html . '
'; } $html .= '
'; return $html; } private static $single_product_rendered = false; public function inject_single_product_section_into_block($block_content, $block) { ob_start(); $this->render_single_product_section(); $section_html = (string) ob_get_clean(); if ($section_html === '') { return $block_content; } return $block_content . $section_html; } public function is_active() { return self::switched_on(); } public function output() { $this->render_single_product_section(); } public function render_single_product_section() { if (self::$single_product_rendered) { return; } global $product; if (!$product || !is_a($product, 'WC_Product')) { return; } $matched = $this->get_offers_for_product($product->get_id()); if (!empty($matched) && class_exists('AIEO_DMM_Offer_Strip') && method_exists('AIEO_DMM_Offer_Strip', 'announces')) { $pid = (int) $product->get_id(); $matched = array_values(array_filter($matched, static function ($offer) use ($pid) { return !AIEO_DMM_Offer_Strip::announces($pid, (int) ($offer['id'] ?? 0)); })); } if (empty($matched)) { return; } self::$single_product_rendered = true; if (!wp_style_is('aieo-brand-gifts', 'enqueued')) { $this->enqueue_frontend_assets(); } $subtotals = $this->gift_counts($matched); echo '
'; foreach ($matched as $offer) { $offer_id = (int) $offer['id']; $info = isset($subtotals[$offer_id]) ? $subtotals[$offer_id] : array('subtotal' => 0, 'qualified' => false); $qualified = !empty($info['qualified']); $show_when_pending = isset($offer['show_when_pending']) ? (int) $offer['show_when_pending'] : 1; if (!$qualified && $show_when_pending === 0) { continue; } $current = (float) $info['subtotal']; $threshold = (float) ($offer['min_subtotal'] ?? 0); $remaining = max(0, $threshold - $current); $addable = $this->filter_addable_gifts((array) ($offer['gift_product_ids'] ?? array())); if (empty($addable)) { continue; } $picked = $this->get_picked_gift($offer_id); if ($picked > 0 && !in_array($picked, $addable, true)) { $picked = 0; } $theirs = $current > 0 || $picked > 0; if ($picked === 0) { $picked = (int) $addable[0]; } $brand_names = $this->resolve_scope_names_html($offer); $tokens = array('remaining' => $remaining, 'brand' => $brand_names); $headline = $qualified ? $this->gift_text($offer, 'headline_qualified', '' !== $brand_names ? __('You\'ve unlocked your free gift from {brand}', 'ai-eshop-optimizer') : __('You\'ve unlocked your free gift', 'ai-eshop-optimizer'), $tokens) : $this->gift_text($offer, 'headline_pending', '' !== $brand_names ? __('Spend {remaining} more on {brand} and pick a free gift', 'ai-eshop-optimizer') : __('Spend {remaining} more and pick a free gift', 'ai-eshop-optimizer'), $tokens); $spent = self::is_spent_by_the_visitor($offer_id); if ($spent) { $headline = esc_html(self::spent_words()); $qualified = false; } if (($theirs || $spent) && class_exists('AIEO_DMM_Offer_Texts') && method_exists('AIEO_DMM_Offer_Texts', 'keep_out_of_caches')) { AIEO_DMM_Offer_Texts::keep_out_of_caches(); } $bar_bg = $qualified ? ($offer['bar_bg_after'] ?? '') : ($offer['bar_bg_before'] ?? ''); $style = $bar_bg ? ' style="background:' . esc_attr($bar_bg) . ';"' : ''; ?> 1); $seen = class_exists('AIEO_Sighting') ? AIEO_Sighting::attrs('pdp', $offer_id, 'go') : ''; ?>
>
'; echo '' . esc_html__('Your gift:', 'ai-eshop-optimizer') . ' ' . esc_html($only->get_name()); echo '
'; } ?>
'; } public function render_cart_reward_cards() { $offers = $this->cart_bar_offers($this->shown_offers($this->get_active_offers(), true)); if (empty($offers)) { return ''; } $subtotals = $this->gift_counts($offers); ob_start(); foreach ($offers as $offer) { if (isset($offer['show_rewards']) && (int) $offer['show_rewards'] === 0) { continue; } $offer_id = (int) $offer['id']; $info = isset($subtotals[$offer_id]) ? $subtotals[$offer_id] : array('subtotal' => 0, 'qualified' => false); $qualified = !empty($info['qualified']); if (self::is_spent_by_the_visitor($offer_id)) { if (isset($offer['show_banner']) && (int) $offer['show_banner'] === 0) { echo $this->spent_reward_html($offer_id); } continue; } $show_when_pending = isset($offer['show_when_pending']) ? (int) $offer['show_when_pending'] : 1; if (!$qualified && $show_when_pending === 0) { continue; } $current = (float) $info['subtotal']; $threshold = (float) ($offer['min_subtotal'] ?? 0); $remaining = max(0, $threshold - $current); $addable = $this->filter_addable_gifts((array) ($offer['gift_product_ids'] ?? array())); if (empty($addable)) { continue; } $picked = $this->get_picked_gift($offer_id); $picked_vid = $this->get_picked_variation($offer_id); $brand_names_html = $this->resolve_scope_names_html($offer); $brand_names_plain = wp_strip_all_tags($brand_names_html); $headline_tokens = array('remaining' => $remaining, 'brand' => $brand_names_html); $pill_tokens = array('remaining' => $remaining, 'brand' => $brand_names_plain); $section_label = $qualified ? $this->gift_text($offer, 'headline_qualified', '' !== $brand_names_html ? __('Pick your free gift from {brand}', 'ai-eshop-optimizer') : __('Pick your free gift', 'ai-eshop-optimizer'), $headline_tokens) : $this->gift_text($offer, 'headline_pending', '' !== $brand_names_html ? __('Free gift waiting — spend {remaining} more on {brand}', 'ai-eshop-optimizer') : __('Free gift waiting — spend {remaining} more', 'ai-eshop-optimizer'), $headline_tokens); $pres_mode = isset($offer['presentation_mode']) ? (string) $offer['presentation_mode'] : 'auto'; $use_dropdown = ($pres_mode === 'dropdown') && count($addable) > 1; $seen = class_exists('AIEO_Sighting') ? AIEO_Sighting::attrs('cart_bar', $offer_id, 'go') : ''; if ($use_dropdown) { $picked_for_select = ($picked > 0 && in_array($picked, $addable, true)) ? (int) $picked : (int) $addable[0]; $picked_p = wc_get_product($picked_for_select); if (!$picked_p) { $picked_for_select = 0; } $picked_thumb = $picked_for_select > 0 ? get_the_post_thumbnail_url($picked_for_select, 'thumbnail') : ''; $picked_name = $picked_for_select > 0 ? $picked_p->get_name() : ''; $picked_type = $picked_for_select > 0 ? $picked_p->get_type() : 'simple'; ?>
>
0) : $picked_p = wc_get_product($picked_for_select); if ($picked_p && $picked_p->is_type('variable')) : $variations = $this->get_variations_for_product($picked_for_select); if (!empty($variations)) : ?>
reward_card_theme_html( $offer, $addable, (int) $picked, (int) $picked_vid, $qualified, $current, $threshold, $remaining, $brand_names_html, $pill_tokens ); continue; } ?>
>
get_name(); $is_picked = ((int) $picked === (int) $gpid); $type = $gp->get_type(); $pill_text = ''; $pill_cls = ''; if ($qualified && $is_picked) { $pill_text = $this->gift_text($offer, 'pill_claimed', __('Claimed', 'ai-eshop-optimizer'), $pill_tokens, true); $pill_cls = 'is-claimed'; } elseif ($qualified) { $pill_text = $this->gift_text($offer, 'pill_available', __('Tap to swap', 'ai-eshop-optimizer'), $pill_tokens, true); $pill_cls = 'is-available'; } else { $pill_text = $this->gift_text($offer, 'pill_locked', __('Spend {remaining} more', 'ai-eshop-optimizer'), $pill_tokens, true); $pill_cls = 'is-locked'; } ?> get_variations_for_product((int) $gpid); if (!empty($variations)) : ?>
' . '

' . AIEO_Icons::svg('gift', '16') . '' . esc_html__('Free gift', 'ai-eshop-optimizer') . '

' . '

' . $words . '

' . ''; } return '
' . $words . '
'; } private function reward_card_theme_html( array $offer, array $addable, $picked, $picked_vid, $qualified, $current, $threshold, $remaining, $brand_html, array $pill_tokens ) { $offer_id = (int) $offer['id']; $tokens = array( 'remaining' => $remaining, 'brand' => $brand_html ); $text = $qualified ? $this->gift_text( $offer, 'headline_qualified', '' !== $brand_html ? __( 'Choose your free gift from {brand}.', 'ai-eshop-optimizer' ) : __( 'Choose your free gift.', 'ai-eshop-optimizer' ), $tokens ) : $this->gift_text( $offer, 'headline_pending', '' !== $brand_html ? __( 'Add {remaining} more on {brand} and choose a free gift.', 'ai-eshop-optimizer' ) : __( 'Add {remaining} more in products and choose a free gift.', 'ai-eshop-optimizer' ), $tokens ); $text = (string) preg_replace( '/^\s*(?:\x{1F381}|\x{1F389}|\x{2728})\x{FE0F}?\s*/u', '', $text ); $percent = $threshold > 0 ? max( 0, min( 100, (int) round( $current / $threshold * 100 ) ) ) : 100; $whole = abs( $threshold - round( $threshold ) ) < 0.005; $heading = 'aieo-gift-h-' . $offer_id; $seen = class_exists( 'AIEO_Sighting' ) ? AIEO_Sighting::attrs( 'cart_bar', $offer_id, 'go' ) : ''; ob_start(); ?>
>

0 ) : ?> $whole ? 0 : wc_get_price_decimals() ) ) ); ?>

get_type(); $thumb = get_the_post_thumbnail_url( $gpid, 'thumbnail' ); $worth = (float) $gp->get_regular_price(); $worth = $worth > 0 ? $worth : (float) $gp->get_price(); if ( $qualified && $is_picked ) { $state = $this->gift_text( $offer, 'pill_claimed', __( 'Claimed', 'ai-eshop-optimizer' ), $pill_tokens, true ); } elseif ( $qualified ) { $state = $this->gift_text( $offer, 'pill_available', __( 'Tap to swap', 'ai-eshop-optimizer' ), $pill_tokens, true ); } else { $state = $this->gift_text( $offer, 'pill_locked', __( 'Spend {remaining} more', 'ai-eshop-optimizer' ), $pill_tokens, true ); } ?> get_variations_for_product( (int) $gpid ); if ( ! empty( $variations ) ) : ?>
wc_price($status['remaining']), '{threshold}' => wc_price($status['threshold']), '{current}' => wc_price($status['current']), '{brands}' => $brand_names, '{gift}' => esc_html($gift_name), ); return str_replace(array_keys($replacements), array_values($replacements), $text); } private function resolve_scope_names_html(array $offer) { $brands = $this->resolve_brand_names_html((array) ($offer['brand_term_ids'] ?? array())); if ('' !== $brands) { return $brands; } $target = isset($offer['_definition']['target']) ? $offer['_definition']['target'] : null; if (null !== $target && class_exists('AIEO_DMM_Offer_Channels') && method_exists('AIEO_DMM_Offer_Channels', 'target_names')) { $names = (string) AIEO_DMM_Offer_Channels::target_names($target); if ('' !== $names) { return esc_html($names); } } foreach (array('category_term_ids', 'need_term_ids') as $column) { $ids = array_filter(array_map('intval', (array) ($offer[$column] ?? array()))); if (!$ids) { continue; } $taxonomy = 'category_term_ids' === $column ? 'product_cat' : (string) get_option('aieo_core_attribute_need', ''); if ('' === $taxonomy || !taxonomy_exists($taxonomy)) { continue; } $names = $this->resolve_term_names_html($ids, $taxonomy); if ('' !== $names) { return $names; } } $links = array(); foreach (array_filter(array_map('intval', (array) ($offer['product_ids'] ?? array()))) as $pid) { $post = get_post($pid); if ($post instanceof WP_Post && in_array($post->post_type, array('product', 'product_variation'), true) && 'publish' === $post->post_status) { $links[] = '' . esc_html($post->post_title) . ''; } } if (count($links) > 1) { $connector = apply_filters('aieo_dmm_brand_gifts_connector', _x('or', 'joins the last two brand names: Elixir or MUA', 'ai-eshop-optimizer')); $last = array_pop($links); return implode(', ', $links) . ' ' . $connector . ' ' . $last; } return implode('', $links); } private function resolve_brand_names_html(array $brand_ids) { $tax = class_exists('AIEO_DMM_Brand_Discounts') ? AIEO_DMM_Brand_Discounts::get_brand_taxonomy() : 'product_brand'; if (!$tax) { $tax = 'product_brand'; } $links = array(); foreach ($brand_ids as $bid) { $term = get_term((int) $bid, $tax); if (!$term || is_wp_error($term)) { continue; } $url = get_term_link($term, $tax); if (!is_wp_error($url)) { $links[] = '' . esc_html($term->name) . ''; } else { $links[] = esc_html($term->name); } } if (count($links) <= 1) { return implode('', $links); } $connector = apply_filters('aieo_dmm_brand_gifts_connector', _x('or', 'joins the last two brand names: Elixir or MUA', 'ai-eshop-optimizer')); $last = array_pop($links); return implode(', ', $links) . ' ' . $connector . ' ' . $last; } private function resolve_term_names_html(array $term_ids, $taxonomy) { $links = array(); foreach ($term_ids as $tid) { $term = get_term((int) $tid, $taxonomy); if (!$term || is_wp_error($term)) { continue; } $url = get_term_link($term, $taxonomy); $links[] = is_wp_error($url) ? esc_html($term->name) : '' . esc_html($term->name) . ''; } if (count($links) <= 1) { return implode('', $links); } $connector = apply_filters('aieo_dmm_brand_gifts_connector', _x('or', 'joins the last two brand names: Elixir or MUA', 'ai-eshop-optimizer')); $last = array_pop($links); return implode(', ', $links) . ' ' . $connector . ' ' . $last; } }