<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Cart_Recommendations { private $id = 'cart_recommendations'; public function __construct() { if ( is_admin() && ! defined('DOING_AJAX') ) return; if ( ! $this->enabled() ) return; add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); add_action( 'template_redirect', [ $this, 'register_hooks' ], 5 ); } public function register_hooks(): void { if ( class_exists( 'GT_BOGO_Blocks_Compat' ) && GT_BOGO_Blocks_Compat::is_blocks_cart() ) { add_action( 'wp_footer', [ $this, 'register_blocks_injection' ], 10 ); } else { add_action( 'woocommerce_after_cart_table', [ $this, 'render' ] ); } } public function register_blocks_injection(): void { if ( ! class_exists( 'GT_BOGO_Frontend_Injector' ) ) return; ob_start(); $this->render(); $html = ob_get_clean(); if ( empty( trim( $html ) ) ) return; GT_BOGO_Frontend_Injector::register( 'gtbogo-cart-recs', '.wp-block-woocommerce-cart-items-block', $html, 'after', 15 ); } private function enabled() { return (bool) get_option( 'gtbogo_sp_' . $this->id . '_enabled', 0 ) && get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ) !== 'off'; } private function intensity() { return get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ); } private function styles() { $map = [ 'subtle' => [ 'font_size' => '0.82em', 'opacity' => '0.78' ], 'moderate' => [ 'font_size' => '0.88em', 'opacity' => '0.9' ], 'bold' => [ 'font_size' => '0.95em', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } private function items_needed() { if ( ! WC()->cart ) return 0; $cart_qty = array_sum( array_column( WC()->cart->get_cart(), 'quantity' ) ); $buy = 1; if ( function_exists( 'gt_bogo_has_pro' ) && gt_bogo_has_pro() ) { global $wpdb; $rule = $wpdb->get_row( "SELECT buy_quantity FROM {$wpdb->prefix}gtbogo_rules WHERE status='active' ORDER BY priority ASC LIMIT 1", ARRAY_A ); if ( $rule ) $buy = max( 1, (int) $rule['buy_quantity'] ); } else { $buy = max( 1, (int) get_option( 'gt_bogo_free_rule_buy_qty', 1 ) ); } $group = $buy + 1; $position = $cart_qty % $group; return $position > 0 ? $buy - $position : 0; } private function get_suggestions( $limit = 4 ) { $in_cart = array_column( WC()->cart->get_cart(), 'product_id' ); return wc_get_products( [ 'status' => 'publish', 'limit' => $limit + count( $in_cart ), 'orderby' => 'popularity', 'exclude' => $in_cart, 'visibility' => 'catalog', ] ); } public function enqueue() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-recs{font-family:inherit;color:inherit;margin-top:20px;border-top:1px solid currentColor;padding-top:16px} .gtbogo-recs-title{font-weight:700;margin:0 0 12px;font-size:1em} .gtbogo-recs-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(140px,1fr));gap:12px} .gtbogo-rec-item{text-align:center;font-size:0.9em} .gtbogo-rec-item img{width:100%;aspect-ratio:1;object-fit:cover;border-radius:4px;display:block;margin-bottom:6px} .gtbogo-rec-name{font-weight:600;margin-bottom:3px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap} .gtbogo-rec-price{opacity:0.65;font-size:0.88em;margin-bottom:6px} .gtbogo-rec-add{display:block;width:100%;padding:7px 0;border:1px solid currentColor;border-radius:3px;background:none;font-family:inherit;font-size:0.85em;font-weight:600;cursor:pointer;color:inherit;text-align:center;text-decoration:none} .gtbogo-rec-add:hover{opacity:0.75} ' ); } public function render() { if ( ! is_cart() ) return; $needed = $this->items_needed(); $prods = $this->get_suggestions( 4 ); if ( empty( $prods ) ) return; $s = $this->styles(); ?> <div class="gtbogo-recs" style="font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <p class="gtbogo-recs-title"> <?php if ( $needed > 0 ) { echo esc_html( GT_BOGO_Copy::format( 'recs_needed_format', $this->intensity(), $needed ) ); } else { echo esc_html( GT_BOGO_Copy::get( 'recs_browse', $this->intensity() ) ); } ?> </p> <div class="gtbogo-recs-grid"> <?php foreach ( array_slice( $prods, 0, 4 ) as $p ) : ?> <div class="gtbogo-rec-item"> <a href="<?php echo esc_url( $p->get_permalink() ); ?>"> <?php $img = wp_get_attachment_image_src( $p->get_image_id(), 'woocommerce_thumbnail' ); $src = $img ? $img[0] : wc_placeholder_img_src(); ?> <img src="<?php echo esc_url($src); ?>" alt="<?php echo esc_attr($p->get_name()); ?>" loading="lazy"> </a> <div class="gtbogo-rec-name"><?php echo esc_html( $p->get_name() ); ?></div> <div class="gtbogo-rec-price"><?php echo wp_kses_post( $p->get_price_html() ); ?></div> <a href="<?php echo esc_url( $p->add_to_cart_url() ); ?>" class="gtbogo-rec-add" data-product_id="<?php echo esc_attr( $p->get_id() ); ?>" data-quantity="1" aria-label="<?php echo esc_attr( $p->add_to_cart_text() ); ?>"> <?php esc_html_e( 'Add to cart', 'gt-bogo-engine' ); ?> </a> </div> <?php endforeach; ?> </div> </div> <?php } }