Last active
April 16, 2025 15:12
-
-
Save xlplugins/a973da25445c9d798c5f6dc850d055db to your computer and use it in GitHub Desktop.
Upsell Addon for add mutiple order product to upsell with predefined discount set in 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
class WFOCU_Upsell_Addon { | |
public function __construct() { | |
add_action( 'wp_footer', [ $this, 'js' ] ); | |
add_shortcode( 'wfocu_bucket_total', [ $this, 'bucket_total' ] ); | |
add_filter( 'wfocu_build_offer_product_before', array( $this, 'maybe_check_offer_data_for_qty_change' ), 10, 2 ); | |
add_filter( 'wfocu_prepare_upsell_package_before', array( $this, 'maybe_check_offer_data_for_qty_change' ), 10, 2 ); | |
add_action( 'wfocu_add_custom_html_above_accept_button', array( $this, 'schemes_template_html' ), 10, 2 ); | |
add_filter( 'wfocu_validate_charge_request', '__return_true' ); | |
} | |
public function maybe_check_offer_data_for_qty_change( $offer_data ) { | |
try { | |
$items = WFOCU_Core()->data->get_current_order()->get_items(); | |
$product_data = null; | |
$first_key = ''; | |
foreach ( $offer_data->products as $key => $prod ) { | |
$product_data = $offer_data->fields->{$key}; | |
$first_key = $key; | |
break; | |
} | |
if ( empty( $first_key ) ) { | |
return $offer_data; | |
} | |
/** | |
* @var $item WC_Order_Item_Product; | |
*/ | |
foreach ( $items as $item ) { | |
if ( $item->get_total() == 0 ) { | |
continue; | |
} | |
$pid = $item->get_product_id(); | |
$pkey = md5( $pid ); | |
$offer_data->products->{$pkey} = $pid; | |
$product_data->quantity = $item->get_quantity(); | |
$offer_data->fields->{$pkey} = $product_data; | |
} | |
unset( $offer_data->products->{$first_key} ); | |
unset( $offer_data->fields->{$first_key} ); | |
} catch ( Exception|Error $e ) { | |
} | |
return $offer_data; | |
} | |
public function schemes_template_html() { | |
echo "<button class='wfocu_multi_product_add' style='height:50px;width:150px;padding:5px;margin-bottom:10px'>Add Product</button>"; | |
} | |
public function bucket_total() { | |
echo "<p class='wfocu_bucket_total'></p>"; | |
} | |
public function js() { | |
?> | |
<script> | |
(function ($) { | |
console.log('jquery loaded'); | |
let MainBucket = null; | |
$(document).on('wfocuBucketLinksConverted', function (e, Bucket) { | |
console.log(Bucket); | |
MainBucket = Bucket; | |
}); | |
$(document).on('wfocuBucketCreated', function (e, Bucket) { | |
console.log('wfocuBucketCreated', Bucket); | |
for (let i in wfocu_vars.offer_data.products) { | |
let qty = wfocu_vars.offer_data.fields[i].quantity; | |
let tax = wfocu_vars.offer_data.products[i].tax; | |
let price = wfocu_vars.offer_data.products[i].price; | |
Bucket.addItem(i, price, [], tax, qty) | |
} | |
$('.wfocu_bucket_total').html(Bucket.formatMoney(Bucket.getTotal())) | |
MainBucket = Bucket; | |
}); | |
$(window).on('load', function () { | |
$('body').on('click', '.wfocu_multi_product_add', function () { | |
console.log('send button clicked'); | |
MainBucket.sendBucket(); | |
}) | |
window.wfocu_send_multi_product = function () { | |
console.log('send button clicked1'); | |
MainBucket.sendBucket(); | |
} | |
}) | |
})(jQuery) | |
</script> | |
<?php | |
} | |
} | |
new WFOCU_Upsell_Addon(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment