'Hot Flame',
'bolt-gold' => 'Flash Sale',
'star-gold' => 'Golden Star',
'tag-color' => 'Offer Tag',
'gift-color' => 'Gift Box',
'none' => 'No Icon',
);
}
/**
* Allowed HTML tags and attributes (including inline SVG icons) for wp_kses escaping.
*/
public static function allowed_html() {
$allowed = wp_kses_allowed_html( 'post' );
$svg_common = array(
'class' => true,
'style' => true,
'fill' => true,
'stroke' => true,
'stroke-width' => true,
'stroke-linecap' => true,
'stroke-linejoin' => true,
'opacity' => true,
);
$allowed['svg'] = array_merge( $svg_common, array(
'width' => true,
'height' => true,
'viewbox' => true,
'xmlns' => true,
) );
$allowed['path'] = array_merge( $svg_common, array( 'd' => true ) );
$allowed['polygon'] = array_merge( $svg_common, array( 'points' => true ) );
$allowed['polyline'] = array_merge( $svg_common, array( 'points' => true ) );
$allowed['rect'] = array_merge( $svg_common, array( 'x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true ) );
$allowed['circle'] = array_merge( $svg_common, array( 'cx' => true, 'cy' => true, 'r' => true ) );
$allowed['line'] = array_merge( $svg_common, array( 'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true ) );
$allowed['g'] = $svg_common;
if ( isset( $allowed['div'] ) ) {
$allowed['div']['data-of-countdown'] = true;
$allowed['div']['data-of-reloaded'] = true;
$allowed['div']['data-product_id'] = true;
$allowed['div']['data-id'] = true;
}
if ( isset( $allowed['span'] ) ) {
$allowed['span']['data-of-countdown'] = true;
}
if ( isset( $allowed['img'] ) ) {
$allowed['img']['loading'] = true;
$allowed['img']['decoding'] = true;
$allowed['img']['srcset'] = true;
$allowed['img']['sizes'] = true;
}
return $allowed;
}
/**
* Allowed URL protocols including data URIs for uploaded custom icon thumbnails.
*/
public static function allowed_protocols() {
return array_merge( wp_allowed_protocols(), array( 'data' ) );
}
/**
* Enqueue OfferCrisp admin CSS & JS only on plugin screens.
*/
public function enqueue_admin_assets( $hook_suffix ) {
if ( false === strpos( (string) $hook_suffix, 'offercrisp' ) ) {
return;
}
wp_enqueue_style(
'offercrisp-admin',
plugin_dir_url( __FILE__ ) . 'assets/css/admin.css',
array(),
self::VERSION
);
wp_enqueue_script(
'offercrisp-admin',
plugin_dir_url( __FILE__ ) . 'assets/js/admin.js',
array(),
self::VERSION,
true
);
wp_localize_script(
'offercrisp-admin',
'offercrispAdminData',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'offercrisp_ajax_nonce' ),
)
);
}
public function render_slot_icon( $preset_key, $custom_val = '', $size = 20 ) {
$custom_val = trim( (string) $custom_val );
$s = absint( $size );
if ( '' !== $custom_val ) {
if ( preg_match( '/^(https?:\/\/|data:image\/[a-z0-9+\-.]+;base64,)/i', $custom_val ) ) {
$safe_src = ( 0 === stripos( $custom_val, 'data:image/' ) ) ? esc_attr( $custom_val ) : esc_url( $custom_val );
return '';
}
return '';
}
if ( 'none' === $preset_key || '' === $preset_key ) {
return '';
}
return self::icon( $preset_key, $s );
}
public static function icon( $name, $size = 16 ) {
if ( 'none' === $name || '' === $name ) {
return '';
}
$s = absint( $size );
$icons = array(
'flame-color' => '',
'bolt-gold' => '',
'sparkles-gold' => '',
'crown-gold' => '',
'diamond-cyan' => '',
'perfume-gold' => '',
'gift-color' => '',
'tag-color' => '',
'star-gold' => '',
'rocket-color' => '',
'trophy-gold' => '',
'heart-glow' => '',
'upload-img' => '',
'bell-gold' => '',
'flame' => '',
'bolt' => '',
'sliders' => '',
'percent' => '',
'calendar' => '',
'clock' => '',
'type' => '',
'palette' => '',
'tag' => '',
'globe' => '',
'eye' => '',
'save' => '',
'check' => '',
'layers' => '',
'bell' => '',
'user' => '',
);
return isset( $icons[ $name ] ) ? '' : '';
}
/**
* Default Settings (100% English, Unlimited Products Display by Default)
*/
public function get_default_settings() {
return array(
'enabled' => '0',
'offer_name' => 'Offer',
'badge_text' => 'OFFER',
'tag_slug' => 'offer',
'apply_all' => '0',
'offer_page_id' => 0,
'pricing_method' => 'table', // 'table' | 'global_percent' | 'global_fixed'
'discount_value' => '20',
'discount_base' => 'sale', // 'sale' | 'regular'
'table_override' => '1',
'schedule_enabled' => '0', // ON/OFF toggle for Global Campaign Schedule
'countdown_start' => '',
'offer_start' => '',
'offer_end' => '',
'page_title' => 'Deal of the Day',
'page_description' => 'A limited-time, high-impact flash sale — grab your favorite deals before time runs out.',
'upcoming_message' => 'A new special offer is starting soon. Stay tuned for Deal of the Day.',
'ended_message' => 'The Offer has ended. Stay tuned for the next one.',
'special_pill_text' => 'SPECIAL OFFER',
'active_pill_text' => 'Flash Sale Active',
'bar_subtitle' => 'Flash Sale — Limited Time Only',
'hurry_text' => 'Hurry — limited time offer',
'preset' => 'classic_slate',
'primary_color' => '#2563EB',
'primary_dark' => '#111827',
'secondary_color' => '#3B82F6',
'live_green' => '#16A34A',
'upcoming_purple' => '#475569',
'hero_bg' => '#111827',
'hero_title_accent' => '#93C5FD',
'hero_timer_bg' => '#1F2937',
'sp_bar_mid' => '#1E293B',
'sp_bar_end' => '#111827',
'sp_bar_text' => '#FFFFFF',
'sp_bar_timer_bg' => '#0F172A',
'tz_offset_mins' => 360,
'icon_sp_box' => 'flame-color',
'custom_icon_sp_box' => '',
'icon_sp_title' => 'bolt-gold',
'custom_icon_sp_title' => '',
'icon_hero_sale' => 'bolt-gold',
'custom_icon_hero_sale' => '',
'icon_card_badge' => 'flame-color',
'custom_icon_card_badge' => '',
'products_per_page' => -1,
);
}
public function get_settings() {
if ( null === $this->settings ) {
$saved = get_option( self::OPTION_KEY, array() );
$this->settings = wp_parse_args( is_array( $saved ) ? $saved : array(), $this->get_default_settings() );
}
return $this->settings;
}
public function on_terms_changed( $object_id, $terms, $tt_ids, $taxonomy ) {
if ( 'product_tag' === $taxonomy || 'product_cat' === $taxonomy ) {
$this->clear_performance_caches();
}
}
public function clear_performance_caches() {
delete_transient( self::TRANSIENT_DATA );
$this->runtime_cache = null;
$this->price_cache = array();
if ( function_exists( 'wc_delete_product_transients' ) ) {
wc_delete_product_transients();
}
if ( class_exists( 'WC_Cache_Helper' ) ) {
WC_Cache_Helper::get_transient_version( 'product', true );
WC_Cache_Helper::get_transient_version( 'shipping', true );
}
if ( has_action( 'litespeed_purge_all' ) ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Third-party LiteSpeed Cache purge hook.
do_action( 'litespeed_purge_all' );
}
}
private function sync_selected_offer_page( $new_page_id, $old_page_id = 0 ) {
if ( $old_page_id > 0 && $old_page_id !== $new_page_id ) {
$old_post = get_post( $old_page_id );
if ( $old_post && 'page' === $old_post->post_type ) {
$cleaned = trim( str_replace( '[offercrisp_page]', '', (string) $old_post->post_content ) );
wp_update_post( array(
'ID' => $old_page_id,
'post_content' => $cleaned,
) );
}
}
if ( $new_page_id > 0 ) {
$new_post = get_post( $new_page_id );
if ( $new_post && 'page' === $new_post->post_type ) {
$existing_content = trim( (string) $new_post->post_content );
if ( false === strpos( $existing_content, '[offercrisp_page]' ) ) {
$updated_content = ( '' === $existing_content )
? '[offercrisp_page]'
: $existing_content . "\n\n[offercrisp_page]";
wp_update_post( array(
'ID' => $new_page_id,
'post_content' => $updated_content,
) );
}
}
}
}
public function on_activate() {
$s = $this->get_settings();
if ( empty( $s['offer_page_id'] ) || ! get_post( absint( $s['offer_page_id'] ) ) ) {
$page_id = $this->create_dedicated_offer_page();
if ( $page_id ) {
$s['offer_page_id'] = $page_id;
update_option( self::OPTION_KEY, $s, true );
$this->settings = $s;
}
}
if ( taxonomy_exists( 'product_tag' ) && ! term_exists( 'offer', 'product_tag' ) ) {
wp_insert_term( 'Offer', 'product_tag', array( 'slug' => 'offer' ) );
}
$this->clear_performance_caches();
}
public function on_deactivate() {
$this->clear_performance_caches();
}
private function create_dedicated_offer_page() {
// Only reuse an existing page if it was explicitly created by OfferCrisp.
$existing = get_page_by_path( 'offercrisp-offers' );
if ( $existing && 'page' === $existing->post_type ) {
$is_managed = '1' === get_post_meta( $existing->ID, '_offercrisp_managed_page', true );
$has_sc = false !== strpos( (string) $existing->post_content, '[offercrisp_page]' );
if ( $is_managed || $has_sc ) {
return absint( $existing->ID );
}
}
$page_id = wp_insert_post( array(
'post_title' => 'Flash Offers',
'post_name' => 'offercrisp-offers',
'post_content' => '[offercrisp_page]',
'post_status' => 'publish',
'post_type' => 'page',
'comment_status' => 'closed',
) );
if ( ! $page_id ) {
return 0;
}
update_post_meta( absint( $page_id ), '_offercrisp_managed_page', '1' );
return absint( $page_id );
}
public function is_selected_offer_page() {
if ( is_admin() ) {
return false;
}
$s = $this->get_settings();
$page_id = absint( $s['offer_page_id'] );
$qid = get_queried_object_id();
if ( $page_id > 0 && ( is_page( $page_id ) || ( $qid > 0 && $qid === $page_id ) ) ) {
return true;
}
return false;
}
/**
* O(1) Transient + Memory Runtime Cache of Tagged Product IDs, Schedules & Offer Prices
*/
private function get_runtime_cache() {
if ( null !== $this->runtime_cache ) {
return $this->runtime_cache;
}
$cached = get_transient( self::TRANSIENT_DATA );
if ( is_array( $cached ) && isset( $cached['parents'], $cached['schedules'], $cached['prices'], $cached['free_delivery'] ) ) {
$this->runtime_cache = $cached;
return $this->runtime_cache;
}
$s = $this->get_settings();
$raw_tags = trim( (string) $s['tag_slug'] );
$parents = array();
if ( '1' === (string) $s['apply_all'] ) {
$ids = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
) );
$parents = array_fill_keys( array_map( 'absint', $ids ), true );
} elseif ( '' !== $raw_tags ) {
$raw_parts = array_filter( array_map( 'trim', explode( ',', $raw_tags ) ) );
$tag_slugs = array_filter( array_map( 'sanitize_title', $raw_parts ) );
if ( empty( $tag_slugs ) ) {
$tag_slugs = array( sanitize_title( $raw_tags ) );
}
$ids = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Required to filter products by offer tag/category; results are transient-cached.
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_tag',
'field' => 'slug',
'terms' => $tag_slugs,
),
array(
'taxonomy' => 'product_tag',
'field' => 'name',
'terms' => $raw_parts,
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $tag_slugs,
),
),
) );
$parents = array_fill_keys( array_map( 'absint', $ids ), true );
}
$schedules = array();
$prices = array();
$free_delivery = array();
if ( ! empty( $parents ) ) {
foreach ( array_keys( $parents ) as $pid ) {
$st = (string) get_post_meta( $pid, self::META_START, true );
$en = (string) get_post_meta( $pid, self::META_END, true );
$pr = (string) get_post_meta( $pid, self::META_PRICE, true );
if ( '' !== $st || '' !== $en ) {
$schedules[ $pid ] = array(
'start' => $st,
'end' => $en,
);
}
if ( '' !== $pr && is_numeric( $pr ) && floatval( $pr ) > 0 ) {
$prices[ $pid ] = floatval( $pr );
}
if ( '1' === (string) get_post_meta( $pid, self::META_FREE_DELIVERY, true ) ) {
$free_delivery[ $pid ] = true;
}
$children = get_posts( array(
'post_parent' => $pid,
'post_type' => 'product_variation',
'post_status' => array( 'publish', 'private' ),
'posts_per_page' => -1,
'fields' => 'ids',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
) );
foreach ( $children as $vid ) {
$vpr = (string) get_post_meta( $vid, self::META_PRICE, true );
if ( '' !== $vpr && is_numeric( $vpr ) && floatval( $vpr ) > 0 ) {
$prices[ absint( $vid ) ] = floatval( $vpr );
}
if ( '1' === (string) get_post_meta( $vid, self::META_FREE_DELIVERY, true ) ) {
$free_delivery[ absint( $vid ) ] = true;
}
}
}
}
$data = array(
'parents' => $parents,
'schedules' => $schedules,
'prices' => $prices,
'free_delivery' => $free_delivery,
);
set_transient( self::TRANSIENT_DATA, $data, 4 * HOUR_IN_SECONDS );
$this->runtime_cache = $data;
return $this->runtime_cache;
}
public function get_effective_tz_offset_seconds() {
$wp_offset = (float) get_option( 'gmt_offset', 0 );
if ( 0.0 !== $wp_offset ) {
return (int) round( $wp_offset * 3600 );
}
$s = $this->get_settings();
$mins = isset( $s['tz_offset_mins'] ) ? intval( $s['tz_offset_mins'] ) : 360;
return $mins * 60;
}
public function get_now_local_ts() {
return time() + $this->get_effective_tz_offset_seconds();
}
public function parse_local_dt( $dt_str ) {
$dt_str = trim( (string) $dt_str );
if ( '' === $dt_str ) {
return 0;
}
$clean = str_replace( 'T', ' ', $dt_str );
$ts = strtotime( $clean . ' UTC' );
return $ts ? (int) $ts : 0;
}
public static function hex_to_rgba( $hex, $alpha = 1.0 ) {
$hex = ltrim( trim( (string) $hex ), '#' );
if ( 3 === strlen( $hex ) ) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
if ( 6 !== strlen( $hex ) ) {
return 'rgba(236, 72, 153, ' . floatval( $alpha ) . ')';
}
$r = hexdec( substr( $hex, 0, 2 ) );
$g = hexdec( substr( $hex, 2, 2 ) );
$b = hexdec( substr( $hex, 4, 2 ) );
return 'rgba(' . $r . ', ' . $g . ', ' . $b . ', ' . floatval( $alpha ) . ')';
}
public function get_global_state() {
$s = $this->get_settings();
if ( empty( $s['enabled'] ) || '1' !== (string) $s['enabled'] ) {
return 'inactive';
}
if ( empty( $s['schedule_enabled'] ) || '1' !== (string) $s['schedule_enabled'] ) {
return 'live';
}
$now = $this->get_now_local_ts();
$off_start = ! empty( $s['offer_start'] ) ? $this->parse_local_dt( $s['offer_start'] ) : 0;
$off_end = ! empty( $s['offer_end'] ) ? $this->parse_local_dt( $s['offer_end'] ) : 0;
if ( $off_end > 0 && $now >= $off_end ) {
return 'ended';
}
if ( $off_start > 0 && $now < $off_start ) {
return 'upcoming';
}
return 'live';
}
public function is_product_in_offer_scope( $product, $for_single_banner = true ) {
$parent_id = $this->resolve_parent_id( $product );
if ( ! $parent_id ) {
return false;
}
$cache = $this->get_runtime_cache();
if ( isset( $cache['parents'][ $parent_id ] ) || isset( $cache['prices'][ $parent_id ] ) || isset( $cache['schedules'][ $parent_id ] ) ) {
return true;
}
$s = $this->get_settings();
if ( '1' === (string) $s['apply_all'] ) {
return true;
}
$raw_tags = trim( (string) $s['tag_slug'] );
if ( '' !== $raw_tags ) {
$parts = array_filter( array_map( 'trim', explode( ',', $raw_tags ) ) );
$slugs = array_map( 'sanitize_title', $parts );
$terms = array_unique( array_merge( $parts, $slugs ) );
if ( has_term( $terms, 'product_tag', $parent_id ) || has_term( $terms, 'product_cat', $parent_id ) ) {
return true;
}
}
if ( $for_single_banner && empty( $cache['parents'] ) ) {
return true;
}
return false;
}
public function get_product_banner_state( $product, $for_single_banner = true ) {
$s = $this->get_settings();
if ( empty( $s['enabled'] ) || '1' !== (string) $s['enabled'] ) {
return 'inactive';
}
if ( ! $this->is_product_in_offer_scope( $product, $for_single_banner ) ) {
return 'inactive';
}
$parent_id = $this->resolve_parent_id( $product );
$cache = $this->get_runtime_cache();
$st_str = isset( $cache['schedules'][ $parent_id ]['start'] ) ? $cache['schedules'][ $parent_id ]['start'] : (string) get_post_meta( $parent_id, self::META_START, true );
$en_str = isset( $cache['schedules'][ $parent_id ]['end'] ) ? $cache['schedules'][ $parent_id ]['end'] : (string) get_post_meta( $parent_id, self::META_END, true );
if ( '' !== $st_str || '' !== $en_str ) {
$now = $this->get_now_local_ts();
$st_ts = '' !== $st_str ? $this->parse_local_dt( $st_str ) : 0;
$en_ts = '' !== $en_str ? $this->parse_local_dt( $en_str ) : 0;
if ( $en_ts > 0 && $now >= $en_ts ) {
return 'inactive';
}
if ( $st_ts > 0 && $now < $st_ts ) {
return 'upcoming';
}
return 'live';
}
$global_state = $this->get_global_state();
if ( 'inactive' === $global_state || 'ended' === $global_state ) {
return 'inactive';
}
if ( 'upcoming' === $global_state ) {
return 'upcoming';
}
return 'live';
}
public function is_product_live( $product ) {
return ( 'live' === $this->get_product_banner_state( $product ) );
}
public function get_product_end_timestamp( $product ) {
$parent_id = $this->resolve_parent_id( $product );
$cache = $this->get_runtime_cache();
$now = $this->get_now_local_ts();
$b_state = $this->get_product_banner_state( $product, true );
if ( 'upcoming' === $b_state ) {
$st_str = isset( $cache['schedules'][ $parent_id ]['start'] ) ? $cache['schedules'][ $parent_id ]['start'] : (string) get_post_meta( $parent_id, self::META_START, true );
if ( '' !== $st_str ) {
$st_ts = $this->parse_local_dt( $st_str );
if ( $st_ts > 0 ) {
return $st_ts;
}
}
$s = $this->get_settings();
if ( '1' === (string) $s['schedule_enabled'] && ! empty( $s['offer_start'] ) ) {
$glob_start = $this->parse_local_dt( $s['offer_start'] );
if ( $glob_start > 0 ) {
return $glob_start;
}
}
}
$en_str = isset( $cache['schedules'][ $parent_id ]['end'] ) ? $cache['schedules'][ $parent_id ]['end'] : (string) get_post_meta( $parent_id, self::META_END, true );
if ( '' !== $en_str ) {
$en_ts = $this->parse_local_dt( $en_str );
if ( $en_ts > 0 ) {
return $en_ts;
}
}
$s = $this->get_settings();
if ( '1' === (string) $s['schedule_enabled'] && ! empty( $s['offer_end'] ) ) {
$glob_end = $this->parse_local_dt( $s['offer_end'] );
if ( $glob_end > 0 ) {
return $glob_end;
}
}
return strtotime( 'tomorrow 00:00:00 UTC', $now );
}
public function get_offer_page_target_timestamp( $state = null ) {
if ( null === $state ) {
$state = $this->get_global_state();
}
$s = $this->get_settings();
$now = $this->get_now_local_ts();
if ( 'upcoming' === $state && '1' === (string) $s['schedule_enabled'] && ! empty( $s['offer_start'] ) ) {
return $this->parse_local_dt( $s['offer_start'] );
}
if ( 'live' === $state ) {
$cache = $this->get_runtime_cache();
$nearest_end = 0;
$nearest_next = 0;
foreach ( array_keys( $cache['parents'] ) as $pid ) {
if ( isset( $cache['schedules'][ $pid ] ) ) {
$st = ! empty( $cache['schedules'][ $pid ]['start'] ) ? $this->parse_local_dt( $cache['schedules'][ $pid ]['start'] ) : 0;
$en = ! empty( $cache['schedules'][ $pid ]['end'] ) ? $this->parse_local_dt( $cache['schedules'][ $pid ]['end'] ) : 0;
$is_active = ( 0 === $st || $now >= $st ) && ( 0 === $en || $now < $en );
if ( $is_active && $en > $now ) {
if ( 0 === $nearest_end || $en < $nearest_end ) {
$nearest_end = $en;
}
} elseif ( $st > $now ) {
if ( 0 === $nearest_next || $st < $nearest_next ) {
$nearest_next = $st;
}
}
}
}
if ( $nearest_end > 0 ) {
return $nearest_end;
}
if ( $nearest_next > 0 ) {
return $nearest_next;
}
if ( '1' === (string) $s['schedule_enabled'] && ! empty( $s['offer_end'] ) ) {
return $this->parse_local_dt( $s['offer_end'] );
}
return strtotime( 'tomorrow 00:00:00 UTC', $now );
}
return 0;
}
private function resolve_parent_id( $product ) {
if ( is_numeric( $product ) ) {
if ( ! function_exists( 'wc_get_product' ) ) {
return absint( $product );
}
$p = wc_get_product( absint( $product ) );
if ( ! $p ) {
return absint( $product );
}
$product = $p;
}
if ( is_object( $product ) ) {
if ( method_exists( $product, 'is_type' ) && $product->is_type( 'variation' ) ) {
return absint( $product->get_parent_id() );
}
if ( method_exists( $product, 'get_id' ) ) {
return absint( $product->get_id() );
}
}
return 0;
}
public function local_ts_to_utc( $local_ts ) {
if ( ! $local_ts ) {
return 0;
}
return (int) round( $local_ts - $this->get_effective_tz_offset_seconds() );
}
/**
* Get the raw pre-offer base price of a product or variation ( bypassing our own price filters )
*/
private function get_raw_base_price( $product ) {
if ( ! is_object( $product ) ) {
return 0.0;
}
$s = $this->get_settings();
$raw_reg = (float) $product->get_regular_price( 'edit' );
$raw_sale = (float) $product->get_sale_price( 'edit' );
$raw_cur = (float) $product->get_price( 'edit' );
if ( $raw_reg <= 0 ) {
$meta_reg = get_post_meta( $product->get_id(), '_regular_price', true );
if ( '' !== $meta_reg && is_numeric( $meta_reg ) ) {
$raw_reg = (float) $meta_reg;
}
}
if ( $raw_sale <= 0 ) {
$meta_sale = get_post_meta( $product->get_id(), '_sale_price', true );
if ( '' !== $meta_sale && is_numeric( $meta_sale ) ) {
$raw_sale = (float) $meta_sale;
}
}
if ( $raw_cur <= 0 ) {
$meta_p = get_post_meta( $product->get_id(), '_price', true );
if ( '' !== $meta_p && is_numeric( $meta_p ) ) {
$raw_cur = (float) $meta_p;
}
}
if ( 'sale' === $s['discount_base'] && $raw_sale > 0 ) {
return $raw_sale;
}
if ( $raw_reg > 0 ) {
return $raw_reg;
}
if ( $raw_sale > 0 ) {
return $raw_sale;
}
return max( 0.0, $raw_cur );
}
/**
* Look up saved custom Table Offer Price for an item ID or its parent ID (with direct postmeta fallback)
*/
private function get_saved_table_price( $item_id, $parent_id = 0 ) {
$cache = $this->get_runtime_cache();
if ( $item_id > 0 && isset( $cache['prices'][ $item_id ] ) && $cache['prices'][ $item_id ] > 0 ) {
return (float) $cache['prices'][ $item_id ];
}
if ( $item_id > 0 ) {
$m = get_post_meta( $item_id, self::META_PRICE, true );
if ( '' !== $m && is_numeric( $m ) && floatval( $m ) > 0 ) {
return floatval( $m );
}
}
if ( $parent_id > 0 && $parent_id !== $item_id ) {
if ( isset( $cache['prices'][ $parent_id ] ) && $cache['prices'][ $parent_id ] > 0 ) {
return (float) $cache['prices'][ $parent_id ];
}
$pm = get_post_meta( $parent_id, self::META_PRICE, true );
if ( '' !== $pm && is_numeric( $pm ) && floatval( $pm ) > 0 ) {
return floatval( $pm );
}
}
return 0.0;
}
/**
* Calculate Offer Price for a Product or Variation
*/
private function calculate_offer_price( $product ) {
if ( ! is_object( $product ) || ! method_exists( $product, 'get_id' ) ) {
return null;
}
$item_id = absint( $product->get_id() );
$parent_id = $this->resolve_parent_id( $product );
if ( isset( $this->price_cache[ $item_id ] ) ) {
return false === $this->price_cache[ $item_id ] ? null : $this->price_cache[ $item_id ];
}
if ( ! $this->is_product_live( $product ) ) {
$this->price_cache[ $item_id ] = false;
return null;
}
$s = $this->get_settings();
$base = $this->get_raw_base_price( $product );
$custom_price = $this->get_saved_table_price( $item_id, $parent_id );
// If a custom Offer Price is set in the table (e.g. 2450, 3000, 3550), always apply it when Table mode or Table Priority is active
if ( $custom_price > 0 && ( 'table' === $s['pricing_method'] || '1' === (string) $s['table_override'] ) ) {
$this->price_cache[ $item_id ] = $custom_price;
return $custom_price;
}
if ( $base <= 0 ) {
if ( $custom_price > 0 ) {
$this->price_cache[ $item_id ] = $custom_price;
return $custom_price;
}
$this->price_cache[ $item_id ] = false;
return null;
}
if ( 'global_percent' === $s['pricing_method'] ) {
$pct = max( 0.0, min( 99.0, floatval( $s['discount_value'] ) ) );
if ( $pct > 0 ) {
$final = round( $base * ( 1.0 - ( $pct / 100.0 ) ) );
$this->price_cache[ $item_id ] = max( 1.0, $final );
return $this->price_cache[ $item_id ];
}
} elseif ( 'global_fixed' === $s['pricing_method'] ) {
$amt = max( 0.0, floatval( $s['discount_value'] ) );
if ( $amt > 0 && $amt < $base ) {
$final = round( $base - $amt );
$this->price_cache[ $item_id ] = max( 1.0, $final );
return $this->price_cache[ $item_id ];
}
}
if ( $custom_price > 0 ) {
$this->price_cache[ $item_id ] = $custom_price;
return $custom_price;
}
$this->price_cache[ $item_id ] = false;
return null;
}
public function filter_product_price( $price, $product ) {
$s = $this->get_settings();
if ( $this->applying_price || empty( $s['enabled'] ) || '1' !== (string) $s['enabled'] ) {
return $price;
}
$this->applying_price = true;
$offer_p = $this->calculate_offer_price( $product );
$this->applying_price = false;
return ( null !== $offer_p ) ? (string) $offer_p : $price;
}
public function filter_product_sale_price( $sale_price, $product ) {
$s = $this->get_settings();
if ( $this->applying_price || empty( $s['enabled'] ) || '1' !== (string) $s['enabled'] ) {
return $sale_price;
}
$this->applying_price = true;
$offer_p = $this->calculate_offer_price( $product );
$this->applying_price = false;
return ( null !== $offer_p ) ? (string) $offer_p : $sale_price;
}
public function filter_product_regular_price( $reg_price, $product ) {
$s = $this->get_settings();
if ( $this->applying_price || empty( $s['enabled'] ) || '1' !== (string) $s['enabled'] ) {
return $reg_price;
}
$this->applying_price = true;
$offer_p = $this->calculate_offer_price( $product );
$base_p = $this->get_raw_base_price( $product );
$this->applying_price = false;
if ( null !== $offer_p && $base_p > $offer_p ) {
return (string) $base_p;
}
return $reg_price;
}
/**
* Top-level filter on woocommerce_variation_prices (Runs even when Redis Object Cache / wc_var_prices_ transient is hit!)
*/
public function filter_woocommerce_variation_prices( $prices_array, $product, $for_display ) {
if ( ! is_array( $prices_array ) || empty( $prices_array['price'] ) ) {
return $prices_array;
}
if ( ! $this->is_product_live( $product ) ) {
return $prices_array;
}
$this->applying_price = true;
foreach ( array_keys( $prices_array['price'] ) as $vid ) {
$var_obj = wc_get_product( $vid );
if ( ! $var_obj ) {
continue;
}
$offer_p = $this->calculate_offer_price( $var_obj );
if ( null !== $offer_p && $offer_p > 0 ) {
$base_p = $this->get_raw_base_price( $var_obj );
$prices_array['price'][ $vid ] = (string) $offer_p;
$prices_array['sale_price'][ $vid ] = (string) $offer_p;
if ( $base_p > $offer_p ) {
$prices_array['regular_price'][ $vid ] = (string) $base_p;
}
}
}
$this->applying_price = false;
asort( $prices_array['price'] );
asort( $prices_array['regular_price'] );
asort( $prices_array['sale_price'] );
return $prices_array;
}
public function filter_variation_prices_array( $prices_array, $product, $for_display ) {
if ( ! is_array( $prices_array ) || empty( $prices_array ) ) {
return $prices_array;
}
$this->applying_price = true;
foreach ( $prices_array as $vid => $orig_p ) {
$var_obj = wc_get_product( $vid );
if ( $var_obj ) {
$offer_p = $this->calculate_offer_price( $var_obj );
if ( null !== $offer_p ) {
$prices_array[ $vid ] = (string) $offer_p;
}
}
}
$this->applying_price = false;
asort( $prices_array );
return $prices_array;
}
public function filter_variation_regular_prices_array( $prices_array, $product, $for_display ) {
if ( ! is_array( $prices_array ) || empty( $prices_array ) ) {
return $prices_array;
}
$this->applying_price = true;
foreach ( $prices_array as $vid => $orig_reg ) {
$var_obj = wc_get_product( $vid );
if ( $var_obj ) {
$offer_p = $this->calculate_offer_price( $var_obj );
$base_p = $this->get_raw_base_price( $var_obj );
if ( null !== $offer_p && $base_p > $offer_p ) {
$prices_array[ $vid ] = (string) $base_p;
}
}
}
$this->applying_price = false;
asort( $prices_array );
return $prices_array;
}
public function filter_product_is_on_sale( $on_sale, $product ) {
if ( $this->applying_price ) {
return $on_sale;
}
if ( ! is_object( $product ) || ! $this->is_product_live( $product ) ) {
return $on_sale;
}
$this->applying_price = true;
if ( method_exists( $product, 'is_type' ) && $product->is_type( 'variable' ) ) {
$children = $product->get_children();
foreach ( $children as $vid ) {
$var_obj = wc_get_product( $vid );
if ( $var_obj && null !== $this->calculate_offer_price( $var_obj ) ) {
$this->applying_price = false;
return true;
}
}
}
$offer_p = $this->calculate_offer_price( $product );
$this->applying_price = false;
return ( null !== $offer_p ) ? true : $on_sale;
}
public function filter_variation_prices_hash( $hash ) {
$s = $this->get_settings();
$cache = $this->get_runtime_cache();
$hash[] = self::VERSION . '_' . $this->get_global_state() . '_' . md5( wp_json_encode( array( $s['pricing_method'], $s['discount_value'], $s['discount_base'], $s['tag_slug'], $cache['prices'], $cache['schedules'] ) ) );
return $hash;
}
/**
* Guarantee formatted Offer Price HTML (
Old Price Offer Price) for both Simple and Variable Products
*/
public function filter_woocommerce_get_price_html( $price_html, $product ) {
if ( is_admin() || $this->applying_price || ! is_object( $product ) || ! function_exists( 'wc_get_product' ) ) {
return $price_html;
}
if ( ! $this->is_product_live( $product ) ) {
return $price_html;
}
$this->applying_price = true;
// 1. Variable Product (e.g. Perfume with 100ml variation or multiple variations)
if ( method_exists( $product, 'is_type' ) && $product->is_type( 'variable' ) ) {
$children = $product->get_children();
$offer_items = array();
$all_eff = array();
$all_orig = array();
if ( is_array( $children ) && ! empty( $children ) ) {
foreach ( $children as $vid ) {
$var_obj = wc_get_product( $vid );
if ( ! $var_obj ) {
continue;
}
$orig_p = $this->get_raw_base_price( $var_obj );
$offer_p = $this->calculate_offer_price( $var_obj );
if ( null !== $offer_p && $offer_p > 0 ) {
$offer_items[] = array(
'orig' => $orig_p > 0 ? $orig_p : $offer_p,
'offer' => $offer_p,
);
$all_eff[] = $offer_p;
$all_orig[] = $orig_p > 0 ? $orig_p : $offer_p;
} elseif ( $orig_p > 0 ) {
$all_eff[] = $orig_p;
$all_orig[] = $orig_p;
}
}
}
if ( ! empty( $offer_items ) ) {
// If only 1 variation has an offer price (e.g. a single discounted variation), or all offered variations share the same offer price, display 2,700.00৳ 2,450.00৳
$o_vals = array_column( $offer_items, 'offer' );
$b_vals = array_column( $offer_items, 'orig' );
$min_o = min( $o_vals );
$max_o = max( $o_vals );
$min_b = min( $b_vals );
$max_b = max( $b_vals );
if ( $min_o === $max_o ) {
$this->applying_price = false;
if ( $max_b > $min_o ) {
return wc_format_sale_price( wc_price( $max_b ), wc_price( $min_o ) );
}
return '' . wc_price( $min_o ) . '';
} else {
$this->applying_price = false;
$orig_range = ( $min_b !== $max_b ) ? wc_format_price_range( $min_b, $max_b ) : wc_price( $min_b );
$offer_range = wc_format_price_range( $min_o, $max_o );
return ' ' . $offer_range . '';
}
}
$this->applying_price = false;
return $price_html;
}
// 2. Simple Product or Single Variation
$offer_p = $this->calculate_offer_price( $product );
$orig_p = $this->get_raw_base_price( $product );
$this->applying_price = false;
if ( null !== $offer_p && $offer_p > 0 ) {
if ( $orig_p > $offer_p ) {
return wc_format_sale_price( wc_price( $orig_p ), wc_price( $offer_p ) );
}
return '' . wc_price( $offer_p ) . '';
}
return $price_html;
}
/**
* Ensure WooCommerce always outputs price_html inside available_variations for live Offer products
*/
public function filter_show_variation_price( $show, $product ) {
if ( is_object( $product ) && $this->is_product_live( $product ) ) {
return true;
}
return $show;
}
/**
* Check if a specific variation ID (or its parent product) has Free Delivery enabled
*/
public function is_item_free_delivery( $item_id, $parent_id = 0 ) {
$item_id = absint( $item_id );
$parent_id = absint( $parent_id );
$cache = $this->get_runtime_cache();
if ( $item_id > 0 ) {
if ( isset( $cache['free_delivery'][ $item_id ] ) ) {
return true;
}
if ( '1' === (string) get_post_meta( $item_id, self::META_FREE_DELIVERY, true ) ) {
return true;
}
}
if ( $parent_id > 0 && $parent_id !== $item_id ) {
if ( isset( $cache['free_delivery'][ $parent_id ] ) ) {
return true;
}
if ( '1' === (string) get_post_meta( $parent_id, self::META_FREE_DELIVERY, true ) ) {
return true;
}
}
return false;
}
/**
* Filter variation JSON data so custom themes & WooCommerce always receive crossed-out regular/sale price + Free Delivery flag
*/
public function filter_available_variation( $data, $parent_product, $variation ) {
if ( ! is_array( $data ) || ! is_object( $variation ) || ! is_object( $parent_product ) ) {
return $data;
}
if ( ! $this->is_product_live( $parent_product ) ) {
return $data;
}
$this->applying_price = true;
$offer_p = $this->calculate_offer_price( $variation );
$base_p = $this->get_raw_base_price( $variation );
$raw_reg = (float) get_post_meta( $variation->get_id(), '_regular_price', true );
if ( $raw_reg > $base_p ) {
$base_p = $raw_reg;
}
$this->applying_price = false;
if ( null !== $offer_p && $offer_p > 0 ) {
$disp_offer = function_exists( 'wc_get_price_to_display' ) ? (float) wc_get_price_to_display( $variation, array( 'price' => $offer_p ) ) : (float) $offer_p;
$disp_base = function_exists( 'wc_get_price_to_display' ) ? (float) wc_get_price_to_display( $variation, array( 'price' => $base_p ) ) : (float) $base_p;
$data['display_price'] = $disp_offer;
if ( $disp_base > $disp_offer ) {
$data['display_regular_price'] = $disp_base;
$data['price_html'] = '' . wc_format_sale_price( wc_price( $disp_base ), wc_price( $disp_offer ) ) . '';
} else {
$data['price_html'] = '' . wc_price( $disp_offer ) . '';
}
}
$is_free_deliv = $this->is_item_free_delivery( $variation->get_id(), $parent_product->get_id() );
$data['of_free_delivery'] = $is_free_deliv;
if ( $is_free_deliv ) {
$free_badge = '
No active offer products right now for tag .