Woocommerce create order after successful payment only

I am using Woocommerce for my e-commerce website. I know Woocommerce create the order before confirming the payment. Is it possible to create the order only if payment is successful?

Solutions

Actually, Woocommerce creates the order before the payment process. I think a possible solution is to check the order status on thank you page. Woocommerce falls on thank you page after checkout and provides the hook 'woocommerce_thankyou'. If payment was not completed the status can be 'failed' or 'pending' This is some example code:

add_action( 'woocommerce_thankyou', 'remove_order_on_failed_payment', 10, 1 );
function remove_order_on_failed_payment( $order_id ) {
    // Get an instance of the order object
    $order = wc_get_order( $order_id );

    if( in_array( $order->get_status(), ['failed', 'pending'] ) ) {
        wp_delete_post($order_id,true);
    }
}

Similar questions

Woo create order only after payment done
We have setup a Woocommerce shop with different payment methods (stripe, Klarna, PayPal). When the customer clicks on the final button for the order, the order is being generated in woo. Then an external window opens, where the customer handles all the payment things. If the customer exits this process without paying, we have a hanging orders. Is t...
Manually send WooCommerce order from pending payment to payment gateway
I have a renewal based site. I'm using WooCommerce with WooCommerce Subscription on my site. I attached a custom function to the filter wcs_renewal_order_created. The filter is applied after a renewal order is created. The custom function gave a fatal error and the renewal process got halted Current situation: The renewal orders are stuck on "Pendi...
Getting order data after successful checkout hook
In WooCommerce, I would like to send a request to an API once the customer has successfully checked out. Its basically a website where the client is selling online courses (Like udemy). When the customer checks out, I would like to send an API request and enroll the user for that particular course. I have tried several WooCommerce hooks but none wo...
Change the user role on nth number of orders after successful order placement
I previously implemented a solution to change the user role on registration, but seem to be missing something this time around. Does anyone out there have a solution for this that still works or be able to point me in the right direction as to what I'm missing or doing wrong? Does is what I have tried so far.
Payment options not showing after enabling them in woocommerce payment.
I am using WordPress and woocommerce plugin. In this case there is some default payment method. I have installed other payment methods. However after proper configuration. The payment method is not showing in the webpage. I am looking for someone who can help me on this. Thanking you Sandipta Roy
Woocommerce - Display payment method "payment on delivery" only for Admin Users
Please, how can i set on Woocommerce the payment method "payment on delivery" only for Admin Profile User? Is that possible? This means, the payment method "payment on delivery" will be available on the checkout page only for users logged on WP and with admin profile. Gratitude for the help!

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.