<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Savings_Calculator { public function __construct() { $enabled = get_option( 'gtbogo_sp_savings_calculator_enabled', 0 ); $intensity = get_option( 'gtbogo_sp_savings_calculator_intensity', 'subtle' ); if ( ! $enabled || $intensity === 'off' ) return; add_action( 'woocommerce_cart_totals_after_order_total', [ $this, 'render_savings' ] ); add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] ); } private function get_intensity_styles() { $intensity = get_option( 'gtbogo_sp_savings_calculator_intensity', 'subtle' ); $map = [ 'subtle' => [ 'font_size' => '0.85em', 'padding' => '12px 16px', 'opacity' => '0.75' ], 'moderate' => [ 'font_size' => '0.9em', 'padding' => '16px 20px', 'opacity' => '0.9' ], 'bold' => [ 'font_size' => '1em', 'padding' => '20px 24px', 'opacity' => '1.0' ], ]; return $map[ $intensity ] ?? $map['subtle']; } public function enqueue_styles() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-savings-row td{padding-top:0;padding-bottom:0} .gtbogo-savings-wrap{font-family:inherit;color:inherit;border-top:1px solid currentColor} .gtbogo-savings-label{font-size:0.75em;font-weight:600;text-transform:uppercase;letter-spacing:0.08em;opacity:0.6;display:block;margin-bottom:4px} .gtbogo-savings-amount{font-weight:700;font-size:1.4em;line-height:1.2;display:block} .gtbogo-savings-sub{font-size:0.75em;opacity:0.55;display:block;margin-top:4px} ' ); } public function render_savings() { $savings = $this->calculate_savings(); if ( $savings <= 0 ) return; $s = $this->get_intensity_styles(); ?> <tr class="gtbogo-savings-row"> <td colspan="2"> <div class="gtbogo-savings-wrap" style="padding:<?php echo esc_attr($s['padding']); ?>;font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <span class="gtbogo-savings-label"><?php esc_html_e( 'Your savings', 'gt-bogo-engine' ); ?></span> <span class="gtbogo-savings-amount"><?php echo wp_kses_post( wc_price( $savings ) ); ?></span> <span class="gtbogo-savings-sub"><?php esc_html_e( 'Applied automatically at checkout', 'gt-bogo-engine' ); ?></span> </div> </td> </tr> <?php } private function calculate_savings() { $savings = 0; foreach ( WC()->cart->get_cart() as $item ) { if ( ! empty( $item['gtbogo_free'] ) ) { $savings += (float) $item['data']->get_regular_price() * (int) $item['quantity']; } } return $savings; } }