<?php if ( ! defined( 'ABSPATH' ) ) exit; class GT_BOGO_Nudges { public function init() { if ( ! class_exists( 'WooCommerce' ) ) return; add_action( 'woocommerce_before_cart', [ $this, 'render_nudge' ] ); add_action( 'woocommerce_before_shop_loop', [ $this, 'render_shop_nudge' ] ); } public function render_nudge() { $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; $items_in_group = $cart_qty % $group_size; $needed = $buy_qty - $items_in_group; if ( $items_in_group > $buy_qty ) { $label = $disc_type === 'percent' ? $disc_val . '% off' : wc_price( $disc_val ) . ' off'; echo '<div class="gt-bogo-nudge gt-bogo-nudge--active" style="background:rgba(34,197,94,0.08);border:1px solid rgba(34,197,94,0.25);padding:14px 18px;border-radius:10px;margin-bottom:16px;font-size:14px;font-weight:600;color:#86efac;display:flex;align-items:center;gap:10px">' . '<span>' . sprintf( esc_html__( 'BOGO deal applied! Your %1$s items get %2$s.', 'gt-bogo-engine' ), (int) $get_qty, esc_html( $label ) ) . '</span>' . '</div>'; } elseif ( $needed > 0 && $needed <= $buy_qty ) { $label = $disc_type === 'percent' ? $disc_val . '% off' : wc_price( $disc_val ) . ' off'; echo '<div class="gt-bogo-nudge" style="background:rgba(225,0,0,0.06);border:1px solid rgba(225,0,0,0.2);padding:14px 18px;border-radius:10px;margin-bottom:16px;font-size:14px;font-weight:600;color:#fca5a5;display:flex;align-items:center;gap:10px">' . '<span style="font-size:20px">🎯</span>' . '<span>' . sprintf( esc_html__( 'Add %1$d more item(s) to unlock your Buy %2$d Get %3$d at %4$s deal!', 'gt-bogo-engine' ), (int) $needed, (int) $buy_qty, (int) $get_qty, esc_html( $label ) ) . '</span>' . '</div>'; } } public function render_shop_nudge() { $rule_enabled = (bool) get_option( 'gt_bogo_free_rule_enabled', false ); if ( ! $rule_enabled ) return; $buy_qty = (int) get_option( 'gt_bogo_free_rule_buy_qty', 1 ); $get_qty = (int) get_option( 'gt_bogo_free_rule_get_qty', 1 ); $disc_val = get_option( 'gt_bogo_free_rule_discount_value', 50 ); $disc_type= get_option( 'gt_bogo_free_rule_discount_type', 'percent' ); $label = $disc_type === 'percent' ? $disc_val . '% off' : wc_price( $disc_val ) . ' off'; echo '<div class="gt-bogo-shop-banner" style="border:1px solid currentColor;opacity:0.85;padding:12px 20px;border-radius:2px;margin-bottom:24px;font-size:0.8em;font-weight:600;color:inherit;text-align:center">' . sprintf( esc_html__( 'Buy %1$d, receive %2$d complimentary at %3$s — applied automatically', 'gt-bogo-engine' ), (int) $buy_qty, (int) $get_qty, esc_html( $label ) ) . '</div>'; } }