<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Dynamic_Pricing { private $id = 'dynamic_pricing'; public function __construct() { 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.82em', 'opacity' => '0.75' ], 'moderate' => [ 'font_size' => '0.88em', 'opacity' => '0.88' ], 'bold' => [ 'font_size' => '0.95em', 'opacity' => '1.0' ], ]; return $map[ $this->intensity() ] ?? $map['subtle']; } private function get_rule() { static $rule = null; if ( $rule !== null ) return $rule; 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 ); return $rule; } $start_date = get_option( 'gt_bogo_free_rule_start_date', '' ); $end_date = get_option( 'gt_bogo_free_rule_end_date', '' ); $now = current_time( 'timestamp' ); $sched_ok = ( empty( $start_date ) || strtotime( $start_date ) <= $now ) && ( empty( $end_date ) || strtotime( $end_date ) >= $now ); if ( get_option( 'gt_bogo_free_rule_enabled', false ) && $sched_ok ) { $rule = [ 'buy_quantity' => get_option( 'gt_bogo_free_rule_buy_qty', 1 ), 'get_quantity' => get_option( 'gt_bogo_free_rule_get_qty', 1 ), 'discount_value' => get_option( 'gt_bogo_free_rule_discount_value', 50 ), 'discount_type' => get_option( 'gt_bogo_free_rule_discount_type', 'percent' ), ]; } return $rule; } private function effective_price( $price, $rule ) { $buy = max( 1, (int) $rule['buy_quantity'] ); $get = max( 1, (int) $rule['get_quantity'] ); $disc = (float) $rule['discount_value']; $type = $rule['discount_type'] ?? 'percent'; $total_paid = $price * $buy; $discount = $type === 'percent' ? $price * $get * ( $disc / 100 ) : min( $disc * $get, $price * $get ); $total_items = $buy + $get; return $total_items > 0 ? ( $total_paid + $price * $get - $discount ) / $total_items : $price; } public function enqueue() { wp_add_inline_style( 'woocommerce-general', ' .gtbogo-dp-wrap{margin:4px 0;font-family:inherit;color:inherit;display:flex;align-items:baseline;gap:8px;flex-wrap:wrap} .gtbogo-dp-effective{font-weight:700} .gtbogo-dp-per{font-size:0.78em;opacity:0.55} .gtbogo-dp-note{font-size:0.75em;opacity:0.55;width:100%;margin-top:2px} ' ); } public function filter_price( $html, $product ) { if ( is_admin() ) return $html; $rule = $this->get_rule(); if ( ! $rule ) return $html; $price = (float) $product->get_price(); if ( $price <= 0 ) return $html; $eff = $this->effective_price( $price, $rule ); if ( abs( $eff - $price ) < 0.01 ) return $html; $s = $this->styles(); return $html . '<span class="gtbogo-dp-wrap" style="font-size:' . esc_attr($s['font_size']) . ';opacity:' . esc_attr($s['opacity']) . '">' . '<span class="gtbogo-dp-effective">' . wp_kses_post( wc_price( $eff ) ) . '</span>' . '<span class="gtbogo-dp-per">' . GT_BOGO_Copy::get( 'dp_per_item', $this->intensity() ) . '</span>' . '</span>'; } public function render_deal_price() { global $product; if ( ! $product ) return; $rule = $this->get_rule(); if ( ! $rule ) return; $price = (float) $product->get_price(); if ( $price <= 0 ) return; $eff = $this->effective_price( $price, $rule ); $buy = (int) $rule['buy_quantity']; $get = (int) $rule['get_quantity']; $s = $this->styles(); ?> <div class="gtbogo-dp-wrap" style="font-size:<?php echo esc_attr($s['font_size']); ?>;opacity:<?php echo esc_attr($s['opacity']); ?>"> <span class="gtbogo-dp-effective"><?php echo wp_kses_post( wc_price( $eff ) ); ?></span> <span class="gtbogo-dp-per"><?php echo esc_html( GT_BOGO_Copy::get( 'dp_per_item', $this->intensity() ) ); ?></span> <span class="gtbogo-dp-note"> <?php printf( esc_html( GT_BOGO_Copy::get( 'dp_note_format', $this->intensity() ) ), esc_html( $buy ), esc_html( $get ) ); ?> </span> </div> <?php } }