implementing strip plugin payment method in order created pragmatically
-
hello, hope you’re doing well.
I need some help regarding this payment plugin.
I am using woocmmerce and ‘Payment Plugins for Stripe WooCommerce’ plugin in my client’s website. And my scenario is that when customer goes to checkout page and selects a main product and places an order, I am redirecting user to page called upsell-purchase page using woocommerce ‘thank-you’ page hook, instead of redirecting him to ‘thank-you’ page. The ‘upsell-purchase’ page is designed by elementor and has 2 buttons ‘Accept’ and ‘Reject’ button. If user ‘accept’ i am creating new order pragmatically and redirect user to ‘Thank You’ page, if he Click Reject then he is redirected to Thank You page without creating any order. Important thing is new pragmatically created order must be created using the same user information and payment method which is present in the main product. And payment method is stripe in this case and this where I am having trouble as I am unable to fetch payment method for custom created product.
So basically I have created small simple one click upsell snippet and I have developed almost all the code which is redirecting user to page and so on and even creating order but with any payment method attached I unable to get the main product’s Payment method since I am using ‘this ‘Payment Plugins for Stripe WooCommerce’ strip Plugin for payment method and not other payment method.
I cannot use upsell plugins as they are not compatible with ‘payment plugin by stripe’ plugin.
Please help me by guiding in which file or document I can find the code for payment method for strip that I can implement in my custom order creation function. Or is even possible. I will really appreciate any help or any new direction as this is the last issue that I am the site.
Extra Info: Using custom Meta Box I attach a specific product to main product and this is the product which I am showing on upsell-purchase page when user is redirected to that page. so ‘_ocu_upsell_product_id’ is the meta key where upsell product I is saved and I am using this in my code.
I can provide full code if needed.
Thanks in advance.
This is part of my code that I tried but failed: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');
The topic ‘implementing strip plugin payment method in order created pragmatically’ is closed to new replies.