<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Deal_Comparison { private $id = 'deal_comparison'; 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.78em', 'opacity' => '0.7' ], 'moderate' => [ 'font_size' => '0.85em', 'opacity' => '0.85' ], 'bold' => [ 'font_size' => '0.9em', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } private function get_deal_data() { global $wpdb; $rule = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}gtbogo_rules WHERE status='active' ORDER BY priority ASC LIMIT 1", ARRAY_A ); if ( ! $rule ) return null; return [ 'buy' => (int) ( $rule['buy_quantity'] ?? 1 ), 'get' => (int) ( $rule['get_quantity'] ?? 1 ), 'discount' => (float) ( $rule['discount_value'] ?? 50 ), 'type' => $rule['discount_type'] ?? 'percent', ]; } public function enqueue() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-deal-comp{font-family:inherit;color:inherit;margin:6px 0;display:flex;align-items:center;gap:8px;flex-wrap:wrap} .gtbogo-deal-comp-original{text-decoration:line-through;opacity:0.45;font-size:0.9em} .gtbogo-deal-comp-effective{font-weight:700} .gtbogo-deal-comp-badge{font-size:0.75em;font-weight:700;padding:2px 7px;border:1px solid currentColor;border-radius:3px;opacity:0.8;text-transform:uppercase;letter-spacing:0.05em} .gtbogo-deal-comp-note{font-size:0.78em;opacity:0.55;width:100%;margin-top:2px} ' ); } private function render( $context = 'loop' ) { global $product; if ( ! $product ) return; $deal = $this->get_deal_data(); if ( ! $deal ) return; $price = (float) $product->get_price(); if ( $price <= 0 ) return; $total_items = $deal['buy'] + $deal['get']; $discount_pct = $deal['discount']; $free_discount = ( $price * $deal['get'] * ( $discount_pct / 100 ) ); $effective = $total_items > 0 ? ( ( $price * $deal['buy'] + $price * $deal['get'] - $free_discount ) / $total_items ) : $price; $saving_pct = $price > 0 ? round( ( ( $price - $effective ) / $price ) * 100 ) : 0; if ( $saving_pct <= 0 ) return; $s = $this->styles(); ?> <div class="gtbogo-deal-comp" style="font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <span class="gtbogo-deal-comp-original"><?php echo wp_kses_post( wc_price( $price ) ); ?></span> <span class="gtbogo-deal-comp-effective"><?php echo wp_kses_post( wc_price( $effective ) ); ?></span> <span class="gtbogo-deal-comp-badge"><?php echo esc_html( $saving_pct ); ?>% <?php echo esc_html( GT_BOGO_Copy::get( 'comparison_off', $this->intensity() ) ); ?></span> <?php if ( $context === 'single' ) : ?> <span class="gtbogo-deal-comp-note"> <?php printf( esc_html( GT_BOGO_Copy::get( 'comparison_note_format', $this->intensity() ) ), esc_html( $total_items ) ); ?> </span> <?php endif; ?> </div> <?php } public function render_loop() { $this->render( 'loop' ); } public function render_single() { $this->render( 'single' ); } }