<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Progress_Bar { private $id = 'progress_bar'; public function __construct() { if ( ! $this->enabled() ) return; add_action( 'woocommerce_before_cart_totals', [ $this, 'render' ] ); add_action( 'wp_footer', [ $this, 'output_styles' ] ); } private function enabled() { if ( get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ) === 'off' ) return false; return (bool) get_option( 'gtbogo_sp_' . $this->id . '_enabled', 0 ); } private function intensity() { return get_option( 'gtbogo_sp_' . $this->id . '_intensity', 'subtle' ); } private function animate() { return (bool) get_option( 'gtbogo_sp_' . $this->id . '_anim', 0 ); } public function render() { if ( ! class_exists( 'WooCommerce' ) ) return; $rule_enabled = (bool) get_option( 'gt_bogo_free_rule_enabled', false ); if ( ! $rule_enabled ) return; if ( ! WC()->cart ) return; $buy_qty = max( 1, (int) get_option( 'gt_bogo_free_rule_buy_qty', 1 ) ); $get_qty = max( 1, (int) get_option( 'gt_bogo_free_rule_get_qty', 1 ) ); $disc_val = (float) get_option( 'gt_bogo_free_rule_discount_value', 50 ); $disc_type = get_option( 'gt_bogo_free_rule_discount_type', 'percent' ); $cart_qty = 0; foreach ( WC()->cart->get_cart() as $item ) { $cart_qty += (int) $item['quantity']; } $group_size = $buy_qty + $get_qty; $current = min( $buy_qty, $cart_qty % $group_size ); $pct = min( 100, round( ( $current / $buy_qty ) * 100 ) ); $label = $disc_type === 'percent' ? $disc_val . '% off' : wc_price( $disc_val ) . ' off'; $achieved = $cart_qty >= $group_size; $intensity = $this->intensity(); $animate = $this->animate(); $padding = $intensity === 'bold' ? '18px 20px' : ( $intensity === 'moderate' ? '16px 18px' : '14px 16px' ); $bar_h = $intensity === 'bold' ? '10px' : ( $intensity === 'moderate' ? '9px' : '8px' ); echo '<div class="gt-progress-bar-wrap gtbogo-sp-pb gtbogo-intensity-' . esc_attr( $intensity ) . ( $animate ? ' gtbogo-animated' : '' ) . '">'; echo '<div class="gt-progress-bar-header">'; if ( $achieved ) { echo '<span class="gt-pb-title">' . esc_html__( 'Complimentary item applied', 'gt-bogo-engine' ) . '</span>'; echo '<span class="gt-pb-badge">' . esc_html( $label ) . ' ' . esc_html__( 'applied', 'gt-bogo-engine' ) . '</span>'; } else { $needed = $buy_qty - $current; echo '<span class="gt-pb-title">🎯 ' . sprintf( esc_html__( 'Add %d more to unlock BOGO', 'gt-bogo-engine' ), (int) $needed ) . '</span>'; echo '<span class="gt-pb-badge">' . esc_html( $label ) . '</span>'; } echo '</div>'; echo '<div class="gt-progress-bar-track">'; echo '<div class="gt-progress-bar-fill ' . ( $achieved ? 'gt-pb-done' : '' ) . '" style="width:' . esc_attr( $pct ) . '%"></div>'; echo '</div>'; echo '</div>'; } public function output_styles() { if ( ! is_cart() && ! is_checkout() ) return; echo '<style> .gtbogo-sp-pb{border-radius:10px;padding:14px 16px;margin-bottom:16px;border:1px solid rgba(0,0,0,0.08)} .gt-progress-bar-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:10px;font-size:13px;font-weight:600} .gt-pb-title{color:inherit} .gt-pb-badge{padding:2px 8px;border-radius:50px;font-size:11px;font-weight:700;opacity:0.85} .gt-progress-bar-track{height:8px;background:rgba(0,0,0,0.08);border-radius:50px;overflow:hidden} .gt-progress-bar-fill{height:100%;background:currentColor;border-radius:50px;transition:width 0.5s ease} .gtbogo-animated .gt-progress-bar-fill{transition:width 0.8s cubic-bezier(.4,0,.2,1)} .gt-pb-done{opacity:0.7} .gtbogo-intensity-bold .gt-progress-bar-track{height:10px} .gtbogo-intensity-moderate .gt-progress-bar-track{height:9px} </style>'; } }