Created
January 13, 2026 06:31
-
-
Save xlplugins/5f746115bf2adf9652ed6e9bd8065450 to your computer and use it in GitHub Desktop.
Funnelkit Bump Compatibility: Order Bumps with WooCommerce Product Price Based on Countries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if ( ! class_exists( 'WFOB_Compatibility_With_Price_Based_Country' ) ) { | |
| class WFOB_Compatibility_With_Price_Based_Country { | |
| /** | |
| * Initialize hooks | |
| */ | |
| public function __construct() { | |
| add_filter( 'wfob_product_switcher_price_data', [ $this, 'update_price_data' ], 10, 2 ); | |
| add_filter( 'wfob_discount_regular_price_data', [ $this, 'update_discount_price' ], 10, 2 ); | |
| add_filter( 'wfob_discount_price_data', [ $this, 'update_discount_price' ], 10, 2 ); | |
| add_filter( 'wfob_product_raw_data', [ $this, 'update_raw_data' ], 10, 3 ); | |
| add_filter( 'wfob_printed_price', [ $this, 'update_printed_price' ], 10, 3 ); | |
| } | |
| /** | |
| * Check if plugin is active and zone is available | |
| * | |
| * @return bool | |
| */ | |
| private function is_enabled() { | |
| if ( ! function_exists( 'wcpbc_the_zone' ) ) { | |
| return false; | |
| } | |
| $zone = wcpbc_the_zone(); | |
| return $zone && is_a( $zone, 'WCPBC_Pricing_Zone' ); | |
| } | |
| /** | |
| * Get base price from product using 'edit' context | |
| * | |
| * @param WC_Product $product Product instance | |
| * @param string $meta_key Meta key (_price, _regular_price, _sale_price) | |
| * @return mixed Base price or null | |
| */ | |
| private function get_base_price( $product, $meta_key ) { | |
| switch ( $meta_key ) { | |
| case '_price': | |
| return $product->get_price( 'edit' ); | |
| case '_regular_price': | |
| return $product->get_regular_price( 'edit' ); | |
| case '_sale_price': | |
| return $product->get_sale_price( 'edit' ); | |
| } | |
| return null; | |
| } | |
| /** | |
| * Get country-based price for a product property | |
| * | |
| * Uses multiple fallback methods to ensure zone pricing is applied: | |
| * 1. Plugin's get_price_prop() method | |
| * 2. Direct zone meta read (e.g., _ca_price) | |
| * 3. Exchange rate conversion if applicable | |
| * | |
| * @param WC_Product $product Product instance | |
| * @param string $meta_key Meta key (_price, _regular_price, _sale_price) | |
| * @param mixed $default Default value if zone price not found | |
| * @return mixed Zone price or default | |
| */ | |
| private function get_zone_price( $product, $meta_key, $default = null ) { | |
| if ( ! $this->is_enabled() || ! $product instanceof WC_Product ) { | |
| return $default; | |
| } | |
| $zone = wcpbc_the_zone(); | |
| if ( ! $zone ) { | |
| return $default; | |
| } | |
| // Get base price (raw, without filters) | |
| $base_price = $this->get_base_price( $product, $meta_key ); | |
| if ( empty( $base_price ) ) { | |
| return $default; | |
| } | |
| // Method 1: Use plugin's get_price_prop() method | |
| $zone_price = $zone->get_price_prop( $product, $base_price, $meta_key ); | |
| // Method 2: Try direct zone meta read (e.g., _ca_regular_price) | |
| if ( empty( $zone_price ) ) { | |
| $zone_meta_key = $zone->get_postmetakey( $meta_key ); | |
| if ( $zone_meta_key ) { | |
| $zone_price = $product->get_meta( $zone_meta_key, true ); | |
| } | |
| } | |
| // Method 3: Apply exchange rate if zone uses it | |
| if ( empty( $zone_price ) && is_callable( [ $zone, 'get_exchange_rate_price' ] ) ) { | |
| $exchange_rate = is_callable( [ $zone, 'get_exchange_rate' ] ) ? $zone->get_exchange_rate() : 1; | |
| if ( $exchange_rate && $exchange_rate != 1 ) { | |
| $zone_price = $zone->get_exchange_rate_price( $base_price, false, 'product', $product->get_id() ); | |
| } | |
| } | |
| // Return zone price if valid, otherwise fallback to base price or default | |
| return ! empty( $zone_price ) ? $zone_price : ( $base_price ?: $default ); | |
| } | |
| /** | |
| * Get product from cart item | |
| * | |
| * @param string $cart_item_key Cart item key | |
| * @return WC_Product|false Product instance or false | |
| */ | |
| private function get_product_from_cart( $cart_item_key ) { | |
| if ( empty( $cart_item_key ) || ! WC()->cart ) { | |
| return false; | |
| } | |
| $cart_item = WC()->cart->get_cart_item( $cart_item_key ); | |
| if ( empty( $cart_item['data'] ) || ! $cart_item['data'] instanceof WC_Product ) { | |
| return false; | |
| } | |
| return $cart_item['data']; | |
| } | |
| /** | |
| * Update price data for order bump display | |
| * | |
| * @param array $price_data Price data array | |
| * @param WC_Product $product Product instance | |
| * @return array Updated price data | |
| */ | |
| public function update_price_data( $price_data, $product ) { | |
| if ( ! $this->is_enabled() || ! $product instanceof WC_Product ) { | |
| return $price_data; | |
| } | |
| $regular_price = $this->get_zone_price( $product, '_regular_price', $product->get_regular_price( 'edit' ) ); | |
| $price = $this->get_zone_price( $product, '_price', $product->get_price( 'edit' ) ); | |
| if ( ! empty( $regular_price ) ) { | |
| $price_data['regular_org'] = floatval( $regular_price ); | |
| } | |
| if ( ! empty( $price ) ) { | |
| $price_data['price'] = floatval( $price ); | |
| } | |
| return $price_data; | |
| } | |
| /** | |
| * Update discount price (used for both regular and sale price discounts) | |
| * | |
| * @param mixed $price Current price | |
| * @param string $cart_item_key Cart item key | |
| * @return mixed Updated zone price | |
| */ | |
| public function update_discount_price( $price, $cart_item_key = '' ) { | |
| if ( ! $this->is_enabled() ) { | |
| return $price; | |
| } | |
| $product = $this->get_product_from_cart( $cart_item_key ); | |
| if ( ! $product ) { | |
| return $price; | |
| } | |
| // Determine meta key based on current filter | |
| $meta_key = current_filter() === 'wfob_discount_regular_price_data' ? '_regular_price' : '_price'; | |
| return $this->get_zone_price( $product, $meta_key, $price ); | |
| } | |
| /** | |
| * Update raw product data with zone prices | |
| * | |
| * @param array $raw_data Raw product data | |
| * @param WC_Product $product Product instance | |
| * @param string $cart_item_key Cart item key (unused) | |
| * @return array Updated raw data | |
| */ | |
| public function update_raw_data( $raw_data, $product, $cart_item_key = '' ) { | |
| if ( ! $this->is_enabled() || ! $product instanceof WC_Product ) { | |
| return $raw_data; | |
| } | |
| $regular_price = $this->get_zone_price( $product, '_regular_price', $raw_data['regular_price'] ?? null ); | |
| $sale_price = $this->get_zone_price( $product, '_sale_price', $raw_data['sale_price'] ?? null ); | |
| $price = $this->get_zone_price( $product, '_price', $raw_data['price'] ?? null ); | |
| if ( $regular_price !== null ) { | |
| $raw_data['regular_price'] = $regular_price; | |
| } | |
| if ( $sale_price !== null ) { | |
| $raw_data['sale_price'] = $sale_price; | |
| } | |
| if ( $price !== null ) { | |
| $raw_data['price'] = $price; | |
| } | |
| return $raw_data; | |
| } | |
| /** | |
| * Update printed price HTML to ensure correct zone pricing is displayed | |
| * | |
| * @param string $printed_price Current price HTML | |
| * @param array $price_data Price data array | |
| * @param WC_Product $product Product instance | |
| * @return string Updated price HTML | |
| */ | |
| public function update_printed_price( $printed_price, $price_data, $product ) { | |
| if ( ! $this->is_enabled() || ! $product instanceof WC_Product ) { | |
| return $printed_price; | |
| } | |
| if ( empty( $price_data['price'] ) ) { | |
| return $printed_price; | |
| } | |
| // Get zone price and regenerate HTML if different | |
| $zone_price = $this->get_zone_price( $product, '_price', $price_data['price'] ); | |
| if ( $zone_price && $zone_price != $price_data['price'] ) { | |
| $product->set_price( $zone_price ); | |
| $printed_price = $product->get_price_html(); | |
| } | |
| return $printed_price; | |
| } | |
| } | |
| new WFOB_Compatibility_With_Price_Based_Country(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment