class HDM_Dynamic_Pricing_Bridge { private $randomizer; public function __construct() { $this->randomizer = new HDM_Randomizer(); $this->register_smart_hybrid_hooks(); } private function register_smart_hybrid_hooks() { // DYNAMIC PRICING (for most cases) add_filter('woocommerce_product_get_price', [$this, 'calculate_dynamic_price'], 99999, 2); add_filter('woocommerce_product_get_sale_price', [$this, 'get_dynamic_sale_price'], 99999, 2); add_filter('woocommerce_product_is_on_sale', [$this, 'force_sale_detection'], 99999, 2); // META UPDATES (only for single product pages) add_action('wp', [$this, 'maybe_update_product_meta']); // PRICE DISPLAY OVERRIDE add_filter('woocommerce_get_price_html', [$this, 'override_price_display'], 99999, 2); } /** * ONLY update meta on single product pages when needed */ public function maybe_update_product_meta() { if (!is_product()) { return; } global $post; if (!$post) { return; } $product_id = $post->ID; $discount = $this->get_discount_percentage($product_id); if ($discount > 0) { $this->update_product_meta_safely($product_id, $discount); } else { $this->clear_product_meta($product_id); } } /** * SAFE meta update - only what's absolutely necessary */ private function update_product_meta_safely($product_id, $discount) { $product = wc_get_product($product_id); if (!$product) { return; } $regular_price = $product->get_regular_price(); if (!$regular_price) { return; } $sale_price = floatval($regular_price) * (1 - ($discount / 100)); // ONLY update these two meta keys update_post_meta($product_id, '_sale_price', $sale_price); update_post_meta($product_id, '_price', $sale_price); // This is critical! // Handle variable products if ($product->is_type('variable')) { $this->update_variable_product_meta($product_id, $discount); } } /** * Clean up meta when no discount */ private function clear_product_meta($product_id) { delete_post_meta($product_id, '_sale_price'); // Reset _price to regular price $product = wc_get_product($product_id); if ($product) { $regular_price = $product->get_regular_price(); if ($regular_price) { update_post_meta($product_id, '_price', $regular_price); } } } /** * Check if product has active HDM discount */ private function has_active_discount($product_id) { $current_set = $this->randomizer->get_current_set(); foreach ($current_set as $item) { if ($item['product_id'] == $product_id) { return true; } } // Check for variations $product = wc_get_product($product_id); if ($product && $product->is_type('variation')) { $parent_id = $product->get_parent_id(); foreach ($current_set as $item) { if ($item['product_id'] == $parent_id) { return true; } } } return false; } /** * Get discount percentage for product */ private function get_discount_percentage($product_id) { $current_set = $this->randomizer->get_current_set(); foreach ($current_set as $item) { if ($item['product_id'] == $product_id) { return floatval($item['discount_percentage']); } } // Check for variations $product = wc_get_product($product_id); if ($product && $product->is_type('variation')) { $parent_id = $product->get_parent_id(); foreach ($current_set as $item) { if ($item['product_id'] == $parent_id) { return floatval($item['discount_percentage']); } } } return 0; } /** * Calculate dynamic price */ public function calculate_dynamic_price($price, $product) { // Skip if no valid price or during cart calculations if (empty($price) || !is_numeric($price) || $price <= 0) { return $price; } $discount = $this->get_discount_percentage($product->get_id()); if ($discount > 0) { $final_price = floatval($price) * (1 - ($discount / 100)); return $final_price; } return $price; } /** * Force sale price return */ public function get_dynamic_sale_price($sale_price, $product) { $discount = $this->get_discount_percentage($product->get_id()); if ($discount > 0) { $regular_price = $product->get_regular_price(); if ($regular_price && $regular_price > 0) { return floatval($regular_price) * (1 - ($discount / 100)); } } return $sale_price; } /** * Force sale detection */ public function force_sale_detection($is_on_sale, $product) { return $is_on_sale || $this->has_active_discount($product->get_id()); } /** * Override price display completely */ public function override_price_display($price_html, $product) { if (!$this->has_active_discount($product->get_id())) { return $price_html; } $discount = $this->get_discount_percentage($product->get_id()); $regular_price = $product->get_regular_price(); $current_price = $product->get_price(); // Only show sale format if we have valid prices if ($regular_price && $current_price && $current_price < $regular_price) { return sprintf( '%s %s -%s%%', wc_price($regular_price), wc_price($current_price), $discount ); } return $price_html; } /** * Handle cart prices safely */ public function handle_cart_prices($cart) { if (is_admin() && !defined('DOING_AJAX')) { return; } foreach ($cart->get_cart() as $cart_item_key => $cart_item) { $product = $cart_item['data']; $product_id = $cart_item['product_id']; $discount = $this->get_discount_percentage($product_id); if ($discount > 0) { $regular_price = $product->get_regular_price(); if ($regular_price && $regular_price > 0) { $discounted_price = floatval($regular_price) * (1 - ($discount / 100)); $product->set_price($discounted_price); } } } } }
Warning: Cannot modify header information - headers already sent by (output started at /home/avantd/public_html/wp-content/plugins/hito-dicount-manager/includes/class-hdm-dynamic-pricing-bridge.php:1) in /home/avantd/public_html/wp-includes/pluggable.php on line 1450

Warning: Cannot modify header information - headers already sent by (output started at /home/avantd/public_html/wp-content/plugins/hito-dicount-manager/includes/class-hdm-dynamic-pricing-bridge.php:1) in /home/avantd/public_html/wp-includes/pluggable.php on line 1453