Created
November 12, 2025 07:30
-
-
Save xlplugins/e145c462d9a23ed0178cdebb49bc53dc to your computer and use it in GitHub Desktop.
Dynamic upsell offer
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
| new WFOCU_Dynamic_Update_Offer_Product(); | |
| class WFOCU_Dynamic_Update_Offer_Product { | |
| public function __construct() { | |
| add_filter( 'wfocu_prepare_upsell_package_before', [ $this, 'maybe_dynamic_update_product' ], 10, 1 ); | |
| add_filter( 'wfocu_build_offer_product_before', [ $this, 'maybe_add_dynamic_product_in_offer' ], 10, 3 ); | |
| } | |
| /** | |
| * add upsell if user want for specific upsell else default return true | |
| * @return bool | |
| */ | |
| public function is_upsell() { | |
| $upsell_id = absint( WFOCU_Core()->data->get_funnel_id() ); | |
| /** currently set for specific upsell */ | |
| /* return true; //run for all upsell */ | |
| /* return ( 12091 === $upsell_id || 12092 === $upsell_id ); //replace upsell id in OR condition if want run for more then one*/ | |
| return ( 12091 === $upsell_id ); //replace upsell id if run for specific upsell | |
| } | |
| /** | |
| * @param $offer_meta | |
| * | |
| * Update dynamic-product id in offer meta | |
| * this product id reflect on order items view list on thankyou page and admin list | |
| * | |
| * @return mixed | |
| */ | |
| public function maybe_dynamic_update_product( $offer_meta ) { | |
| if ( ! $this->is_upsell() ) { | |
| return $offer_meta; | |
| } | |
| if ( false === $this->get_product_id() ) { | |
| return $offer_meta; | |
| } | |
| if ( ! empty( $offer_meta ) && isset( $offer_meta->products ) && 0 < count( get_object_vars( $offer_meta->products ) ) ) { | |
| foreach ( $offer_meta->products as &$hash ) { | |
| $hash = $this->get_product_id(); | |
| } | |
| } | |
| return $offer_meta; | |
| } | |
| /** | |
| * @param $offer_data | |
| * @param $offer_id | |
| * @param $is_front | |
| * | |
| * update dynamic product id in offer meta on offer page | |
| * | |
| * @return mixed | |
| */ | |
| public function maybe_add_dynamic_product_in_offer( $offer_data, $offer_id, $is_front ) { | |
| if ( false === $is_front ) { | |
| return $offer_data; | |
| } | |
| if ( ! $this->is_upsell() ) { | |
| return $offer_data; | |
| } | |
| if ( false === $this->get_product_id() ) { | |
| return $offer_data; | |
| } | |
| $product = wc_get_product( $this->get_product_id() ); | |
| if ( ! $product instanceof WC_Product ) { | |
| return $offer_data; | |
| } | |
| if ( ! empty( $offer_data ) && isset( $offer_data->products ) && 0 < count( get_object_vars( $offer_data->products ) ) ) { | |
| $hash_key = ''; | |
| foreach ( $offer_data->products as $key => &$hash ) { | |
| $hash = $this->get_product_id(); | |
| $hash_key = $key; | |
| } | |
| /** | |
| * set variable product in offer with all variation | |
| */ | |
| if ( ! empty( $hash_key ) && $product->is_type( 'variable' ) && is_array( $product->get_children() ) && count( $product->get_children() ) > 0 ) { | |
| if ( isset( $offer_data->fields->{$hash_key} ) && is_array( $product->get_children() ) && count( $product->get_children() ) > 0 ) { | |
| $offer_data->variations = new stdClass(); | |
| $discount_amount = isset( $offer_data->fields->{$hash_key}->discount_amount ) ? $offer_data->fields->{$hash_key}->discount_amount : 0; | |
| $offer_data->fields->{$hash_key}->default_variation = $product->get_children()[0]; | |
| foreach ( $product->get_children() as $variation_id ) { | |
| $offer_data->variations->{$hash_key}[ $variation_id ] = new stdClass(); | |
| $offer_data->variations->{$hash_key}[ $variation_id ]->vid = $variation_id; | |
| $offer_data->variations->{$hash_key}[ $variation_id ]->discount_amount = $discount_amount; | |
| } | |
| } | |
| } | |
| return $offer_data; | |
| } | |
| } | |
| /** | |
| * Get dynamic product id from primary order items list for set in offer | |
| * @return false|mixed | |
| */ | |
| public function get_product_id() { | |
| $get_current_offer = WFOCU_Core()->data->get_current_offer(); | |
| $get_funnel = WFOCU_Core()->data->get_funnel_id(); | |
| $get_funnel_steps = WFOCU_Core()->funnels->get_funnel_steps( $get_funnel ); | |
| $get_current_offer_index = WFOCU_Core()->funnels->get_current_index( $get_funnel_steps, $get_current_offer ); | |
| if ( false === $get_current_offer_index ) { | |
| return false; | |
| } | |
| $p_order = WFOCU_Core()->data->get( 'porder', false, '_orders' ); | |
| if ( ! $p_order instanceof WC_Order ) { | |
| return false; | |
| } | |
| $item_ids = array(); | |
| $get_items = $p_order->get_items(); | |
| if ( is_array( $get_items ) && count( $get_items ) > 0 ) { | |
| foreach ( $get_items as $item ) { | |
| if ( '' !== $item->get_meta( '_upstroke_purchase' ) ) { | |
| continue; | |
| } | |
| if ( isset( $item['variation_id'] ) && 0 != $item['variation_id'] ) { | |
| /** | |
| * get variable product id if variation found in order | |
| */ | |
| $item_ids[] = wp_get_post_parent_id( $item['variation_id'] ); | |
| } elseif ( isset( $item['product_id'] ) ) { | |
| $item_ids[] = $item['product_id']; | |
| } | |
| } | |
| } | |
| if ( ( is_array( $item_ids ) && isset( $item_ids[ $get_current_offer_index ] ) ) ) { | |
| return $item_ids[ $get_current_offer_index ]; | |
| } else { | |
| /** | |
| * move on thankyou page if parent order no more item for set in offer | |
| */ | |
| WFOCU_Core()->data->set( 'current_offer', 0 ); | |
| WFOCU_Core()->data->save(); | |
| return false; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment