<?php if (!defined('ABSPATH')) { exit; } class GT_BOGO_Cart_Recommendations { public static function init() { add_action('woocommerce_after_cart_table', array(__CLASS__, 'render_recommendations')); } private static function get_recommendations() { if (!WC()->cart || WC()->cart->is_empty()) { return []; } $cart_total = WC()->cart->get_subtotal(); $cart_items = WC()->cart->get_cart(); $recommendations = []; $activated_packs = GT_BOGO_Pack_Manager::get_activated_packs(); foreach ($activated_packs as $pack_id) { $pack_data = GT_BOGO_Pack_Manager::get_pack_data($pack_id); if ($pack_data && !empty($pack_data['rules'])) { foreach ($pack_data['rules'] as $rule) { if (empty($rule['enabled'])) { continue; } $cart_rec = $rule['superpowers']['cart_recommendations'] ?? []; if (empty($cart_rec['enabled'])) { continue; } $threshold = floatval($rule['conditions']['min_cart_value'] ?? 0); if ($threshold > 0 && $cart_total < $threshold && $cart_total >= ($threshold * 0.6)) { $needed = $threshold - $cart_total; $args = [ 'post_type' => 'product', 'posts_per_page' => 4, 'meta_query' => [ [ 'key' => '_price', 'value' => [$needed * 0.5, $needed * 1.5], 'type' => 'NUMERIC', 'compare' => 'BETWEEN' ] ], 'orderby' => 'rand' ]; if (!empty($rule['categories'])) { $args['tax_query'] = [ [ 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $rule['categories'] ] ]; } $products = new WP_Query($args); if ($products->have_posts()) { $recommendations[] = [ 'reason' => 'threshold', 'title' => $rule['title'] ?? 'Complete the Set', 'needed' => $needed, 'threshold' => $threshold, 'products' => $products, 'message' => str_replace('{amount}', wc_price($needed), $cart_rec['message_template'] ?? 'Add {amount} more to unlock this deal:') ]; } wp_reset_postdata(); } } } } return $recommendations; } public static function render_recommendations() { $recommendations = self::get_recommendations(); if (empty($recommendations)) { return; } foreach ($recommendations as $rec) { ?> <div class="gtbogo-cart-recommendations" style=" background: var(--color-secondary, transparent); padding: 25px; margin: 20px 0; border-radius: 8px; border-left: 4px solid currentColor; "> <h3 style="color: inherit; margin: 0 0 10px 0; font-size: 1.1em;"> ⭐ <?php echo esc_html($rec['message']); ?> </h3> <p style="opacity: 0.8; margin: 0 0 20px 0; font-size: 0.875em;"> Unlock <strong><?php echo esc_html($rec['title']); ?></strong> by adding one of these: </p> <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 15px;"> <?php while ($rec['products']->have_posts()) { $rec['products']->the_post(); $product = wc_get_product(get_the_ID()); ?> <div style="background: var(--color-secondary, transparent); padding: 15px; border-radius: 6px; text-align: center;"> <a href="<?php the_permalink(); ?>"> <?php echo $product->get_image('thumbnail'); ?> <div style="margin: 10px 0; font-size: 0.875em; font-weight: 600;"> <?php the_title(); ?> </div> <div style="color: inherit; font-size: 1em; font-weight: 600;"> <?php echo $product->get_price_html(); ?> </div> </a> <a href="<?php echo esc_url('?add-to-cart=' . $product->get_id()); ?>" class="button" style="margin-top: 10px; width: 100%; background: var(--wc-highlight, currentColor); border: none;"> Add to Cart </a> </div> <?php } wp_reset_postdata(); ?> </div> </div> <?php } } } GT_BOGO_Cart_Recommendations::init();