<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Bundle_Spotlight { private $id = 'bundle_spotlight'; public function __construct() { if ( is_admin() && ! defined('DOING_AJAX') ) return; if ( ! $this->enabled() ) return; add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); } 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.84em', 'opacity' => '0.80' ], 'moderate' => [ 'font_size' => '0.9em', 'opacity' => '0.90' ], 'bold' => [ 'font_size' => '0.96em', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } public function enqueue() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-spotlight{font-family:inherit;color:inherit;border:1px solid currentColor;border-radius:6px;padding:20px;margin:24px 0} .gtbogo-spotlight-title{font-weight:700;margin:0 0 16px;font-size:1em} .gtbogo-spotlight-row{display:flex;align-items:center;gap:16px;flex-wrap:wrap;margin-bottom:16px} .gtbogo-spotlight-item{display:flex;flex-direction:column;align-items:center;gap:6px;flex:1;min-width:100px;max-width:140px;text-align:center} .gtbogo-spotlight-item img{width:80px;height:80px;object-fit:cover;border-radius:4px} .gtbogo-spotlight-item-name{font-size:0.85em;font-weight:600;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;max-width:120px} .gtbogo-spotlight-item-price{font-size:0.8em;opacity:0.6} .gtbogo-spotlight-plus{font-size:1.4em;opacity:0.3;font-weight:300;align-self:center;flex-shrink:0} .gtbogo-spotlight-savings{font-size:0.88em;font-weight:700;padding:8px 14px;border-left:3px solid currentColor;margin-bottom:14px} .gtbogo-spotlight-cta{display:inline-block;padding:10px 22px;border:1px solid currentColor;border-radius:4px;font-family:inherit;font-weight:700;font-size:0.88em;color:inherit;text-decoration:none;cursor:pointer;background:none} .gtbogo-spotlight-cta:hover{opacity:0.75} ' ); } public function render() { global $product; if ( ! $product ) return; $related_ids = array_merge( $product->get_upsell_ids(), wc_get_related_products( $product->get_id(), 3 ) ); $related_ids = array_unique( array_slice( $related_ids, 0, 3 ) ); if ( empty( $related_ids ) ) return; $rule_buy = 1; $rule_get = 1; $disc = 50; $type = 'percent'; if ( function_exists( 'gt_bogo_has_pro' ) && gt_bogo_has_pro() ) { global $wpdb; $row = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}gtbogo_rules WHERE status='active' ORDER BY priority ASC LIMIT 1", ARRAY_A ); if ( $row ) { $rule_buy = (int)$row['buy_quantity']; $rule_get = (int)$row['get_quantity']; $disc = (float)$row['discount_value']; $type = $row['discount_type']; } } $total_price = (float) $product->get_price(); $items = [ $product ]; foreach ( array_slice( $related_ids, 0, $rule_buy - 1 + $rule_get ) as $rid ) { $p = wc_get_product( $rid ); if ( $p ) { $items[] = $p; $total_price += (float) $p->get_price(); } } if ( count( $items ) < 2 ) return; $discount_amount = $type === 'percent' ? ( (float)$items[ count($items)-1 ]->get_price() * ( $disc / 100 ) ) : $disc; $s = $this->styles(); ?> <div class="gtbogo-spotlight" style="font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <p class="gtbogo-spotlight-title"><?php echo esc_html( GT_BOGO_Copy::get( 'spotlight_headline', $this->intensity() ) ); ?></p> <div class="gtbogo-spotlight-row"> <?php foreach ( $items as $idx => $p ) : ?> <?php if ( $idx > 0 ) echo '<span class="gtbogo-spotlight-plus">+</span>'; ?> <div class="gtbogo-spotlight-item"> <a href="<?php echo esc_url( $p->get_permalink() ); ?>"> <img src="<?php echo esc_url( wp_get_attachment_image_src( $p->get_image_id(), 'thumbnail' )[0] ?? wc_placeholder_img_src() ); ?>" alt="<?php echo esc_attr( $p->get_name() ); ?>"> </a> <span class="gtbogo-spotlight-item-name"><?php echo esc_html( $p->get_name() ); ?></span> <span class="gtbogo-spotlight-item-price"><?php echo wp_kses_post( $p->get_price_html() ); ?></span> </div> <?php endforeach; ?> </div> <div class="gtbogo-spotlight-savings"> <?php echo esc_html( GT_BOGO_Copy::format( 'spotlight_total_format', $this->intensity(), wp_strip_all_tags( wc_price( $total_price ) ), wp_strip_all_tags( wc_price( $discount_amount ) ) ) ); ?> </div> <a href="<?php echo esc_url( wc_get_page_permalink( 'shop' ) ); ?>" class="gtbogo-spotlight-cta"> <?php esc_html_e( 'Add all to cart', 'gt-bogo-engine' ); ?> </a> </div> <?php } }