<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Savings_Calculator { private $id = 'savings_calculator'; public function __construct() { if ( is_admin() && ! defined('DOING_AJAX') ) return; if ( ! $this->enabled() ) return; add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] ); add_filter( 'woocommerce_add_to_cart_fragments', [ $this, 'cart_fragment' ] ); add_action( 'template_redirect', [ $this, 'register_hooks' ], 5 ); } 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', 'padding' => '10px 14px', 'opacity' => '0.78' ], 'moderate' => [ 'font_size' => '0.88em', 'padding' => '12px 16px', 'opacity' => '0.9' ], 'bold' => [ 'font_size' => '0.95em', 'padding' => '14px 18px', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } public function enqueue() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-savings-calc{font-family:inherit;color:inherit;border-left:3px solid currentColor} .gtbogo-sc-row{display:flex;justify-content:space-between;align-items:center;margin-bottom:5px;font-size:0.95em} .gtbogo-sc-row:last-child{margin-bottom:0;font-weight:700;padding-top:5px;border-top:1px solid currentColor;opacity:0.5} .gtbogo-sc-label{opacity:0.65} .gtbogo-sc-value{font-weight:600} .gtbogo-sc-saved{font-weight:700} ' ); } public function register_hooks() { if ( class_exists( 'GT_BOGO_Blocks_Compat' ) && GT_BOGO_Blocks_Compat::is_blocks_page() ) { add_action( 'wp_footer', [ $this, 'register_blocks_injection' ], 10 ); } else { add_action( 'woocommerce_cart_totals_before_order_total', [ $this, 'render_cart' ] ); add_action( 'woocommerce_review_order_before_order_total', [ $this, 'render_cart' ] ); } } public function register_blocks_injection() { if ( ! class_exists( 'GT_BOGO_Frontend_Injector' ) ) return; ob_start(); $this->render_cart(); $html = ob_get_clean(); if ( empty( trim( $html ) ) ) return; $selector = GT_BOGO_Blocks_Compat::is_blocks_checkout() ? '.wc-block-components-totals-footer-item' : '.wc-block-cart__totals-title'; GT_BOGO_Frontend_Injector::register( 'gtbogo-savings-calculator', $selector, $html, 'before', 10 ); } public function cart_fragment( $frags ) { ob_start(); $this->render_cart(); $frags['.gtbogo-savings-calc'] = ob_get_clean(); return $frags; } public function render_cart() { if ( ! WC()->cart ) return; $discount = (float) WC()->cart->get_discount_total(); if ( $discount < 0.01 ) { echo '<div class="gtbogo-savings-calc" style="display:none"></div>'; return; } $subtotal = (float) WC()->cart->get_cart_contents_total(); $s = $this->styles(); ?> <div class="gtbogo-savings-calc" style="padding:<?php echo esc_attr($s['padding']); ?>;font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>;margin-bottom:10px"> <div class="gtbogo-sc-row"> <span class="gtbogo-sc-label"><?php esc_html_e( 'Cart subtotal', 'gt-bogo-engine' ); ?></span> <span class="gtbogo-sc-value"><?php echo wp_kses_post( wc_price( $subtotal ) ); ?></span> </div> <div class="gtbogo-sc-row"> <span class="gtbogo-sc-label"><?php esc_html_e( 'Deal discount', 'gt-bogo-engine' ); ?></span> <span class="gtbogo-sc-saved">&minus;<?php echo wp_kses_post( wc_price( $discount ) ); ?></span> </div> <div class="gtbogo-sc-row"> <span class="gtbogo-sc-label"><?php esc_html_e( 'You save', 'gt-bogo-engine' ); ?></span> <span class="gtbogo-sc-saved"><?php echo wp_kses_post( wc_price( $discount ) ); ?></span> </div> </div> <?php } public function render_product() { global $product; if ( ! $product ) return; $rule = null; if ( function_exists( 'gt_bogo_has_pro' ) && gt_bogo_has_pro() ) { 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; $price = (float) $product->get_price(); if ( $price <= 0 ) return; $buy = (int) $rule['buy_quantity']; $get = (int) $rule['get_quantity']; $disc = (float) $rule['discount_value']; $type = $rule['discount_type'] ?? 'percent'; $save = $type === 'percent' ? $price * $get * ( $disc / 100 ) : min( $disc * $get, $price * $get ); if ( $save < 0.01 ) return; $s = $this->styles(); ?> <div class="gtbogo-savings-calc" style="padding:<?php echo esc_attr($s['padding']); ?>;font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>;margin:10px 0"> <div class="gtbogo-sc-row"> <span class="gtbogo-sc-label"> <?php printf( esc_html__( 'Buy %1$d Get %2$d deal saves you', 'gt-bogo-engine' ), (int) $buy, (int) $get ); ?> </span> <span class="gtbogo-sc-saved"><?php echo wp_kses_post( wc_price( $save ) ); ?></span> </div> </div> <?php } }