add_action('template_redirect', 'process_upsell_with_payment');
function process_upsell_with_payment() {
if (isset($_GET['buy_now']) && $_GET['buy_now'] == 1 && isset($_GET['order_id'])) {
$order_id = absint($_GET['order_id']);
$product_id = 0;
$order = wc_get_order($order_id);
// Check if the order object exists
if ($order) {
// Loop through each item in the order
foreach ($order->get_items() as $item) {
// Get the product ID for each item
$product_id = $item->get_product_id();
}
}
if($product_id < 1){
return;
}
$order = wc_get_order($order_id);
if ($order) {
$upsell_product_id = get_post_meta($post->ID, '_ocu_upsell_product_id', true);
// Retrieve the saved payment token
$payment_token = get_post_meta($order_id, '_saved_payment_token', true);
if ($payment_token) {
// Create a new WooCommerce order for the upsell product
$new_order = wc_create_order();
// Add the upsell product
$new_order->add_product(wc_get_product($upsell_product_id), 1);
// Copy customer details from the original order
$new_order->set_customer_id($order->get_customer_id());
$new_order->set_billing_first_name($order->get_billing_first_name());
$new_order->set_billing_last_name($order->get_billing_last_name());
$new_order->set_billing_address_1($order->get_billing_address_1());
$new_order->set_billing_address_2($order->get_billing_address_2());
$new_order->set_billing_city($order->get_billing_city());
$new_order->set_billing_state($order->get_billing_state());
$new_order->set_billing_postcode($order->get_billing_postcode());
$new_order->set_billing_country($order->get_billing_country());
$new_order->set_billing_email($order->get_billing_email());
$new_order->set_billing_phone($order->get_billing_phone());
$new_order->set_shipping_first_name($order->get_shipping_first_name());
$new_order->set_shipping_last_name($order->get_shipping_last_name());
$new_order->set_shipping_address_1($order->get_shipping_address_1());
$new_order->set_shipping_address_2($order->get_shipping_address_2());
$new_order->set_shipping_city($order->get_shipping_city());
$new_order->set_shipping_state($order->get_shipping_state());
$new_order->set_shipping_postcode($order->get_shipping_postcode());
$new_order->set_shipping_country($order->get_shipping_country());
// Calculate totals for the new order
$new_order->calculate_totals();
// Process payment with the saved token
$result = wc_get_payment_gateway_by_order($order)->process_payment_with_token($new_order, $payment_token);
if ($result['result'] === 'success') {
$new_order->payment_complete();
wp_redirect(site_url("/thank-you/order-received/?order_id={$order_id}&upsell_order_id={$new_order->get_id()}"));
exit;
} else {
wc_add_notice('Payment failed. Please try again.', 'error');
}
}
}
}
add_action('woocommerce_thankyou', 'redirect_to_upsell_page', 1);
function redirect_to_upsell_page($order_id) {
$order = wc_get_order($order_id);
if (!$order || get_post_meta($order_id, '_upsell_shown', true)) {
return;
}
// Mark this order to avoid redirection loops
update_post_meta($order_id, '_upsell_shown', true);
// URL to your custom Elementor upsell page
$upsell_page_url = site_url('/upsell-purchase/?order_id=' . $order_id);
wp_redirect($upsell_page_url);
exit;
}
function redirect_to_upsell_page($order_id) {
$order = wc_get_order($order_id);
if (!$order || get_post_meta($order_id, '_upsell_shown', true)) {
return;
}
// Mark this order to avoid redirection loops
update_post_meta($order_id, '_upsell_shown', true);
// URL to your custom Elementor upsell page
$upsell_page_url = site_url('/upsell-page/?order_id=' . $order_id);
wp_redirect($upsell_page_url);
exit;
}
function accept_btn(){
$odid = $_GET['order_id'];
return '<a href="'.site_url().'/upsell-purchase/?order_id='.$odid.'&buy_now=1">Accept</a>';
}
add_shortcode('ocu_accept_button', 'accept_btn');
function skip_btn(){
$odid = $_GET['order_id'];
return '<a href="'.site_url().'/thank-you/order-received/order_id='.$odid.'">Skip</a>';
}
add_shortcode('ocu_reject_button', 'skip_btn');
]]>So far I’ve only found plugins that let me add only one product (as an “Order Bump”) or there is need to manually select those products what will be displayed at the checkout (would be annoying to choose over 500 products by hand). I need my whole store’s selection to be shown there, for example 5 different products at once and everytime customer refreshes the page, there will be new selection of products. Also the possibility of adding complimentary discount to these items automatically is very important. Manual cupouns will not do the deed for me.
Any ideas what plugin will do the described work? I appreciate!
]]>I am working on a full site editing theme (twentywentyfour) having problems to show upsell products on product detail page. In page editor on the Single Product Template I see a Related Products block. I already checked all other available content blocks. And all product hooks (e.g. add_action … ) does not seem to work neither. Am I missing something? Any idea on how to output upsell products on product detail page?
Thanks
Georg
i tired looking for a solution to display more than 12 (upsell) products in product page, but i didn’t found a solution.
I added 20 products and i see only 12 products.
can you help me please.
thank you
Also, I like the easy creation of discount campaigns. Surely a must-have during holiday seasons if you want to offer promos. Kudos to the team!
]]>Also, we are using the Recently Viewed Product Carousels of UpsellMaster and ran the last Black Friday Discount Campaign with this plugin. It was stress-free since all Discount Campaigns were scheduled in advance, and we could pick and select which products to offer at a discount at which time, including showing different ad banners for each campaign. The data in the upsell sales tracking module shows that we have generated thousands of $$$ in additional sales due to this plugin running on autopilot.
I am unaware of any other Upsell plugin that comes close to what UpsellMaster does for us. I can only recommend trying this and watching how this plugin systematically generates additional income for your webshop. A really great plugin!
]]>