Hello Brajesh,
I recently submitted a post using the pay per post addon, now when I arrived in the checkout page I did not complete payment and instead went to my blog profile page and deleted the submitted draft/pending post. Now the problem is after deleting the pending post the corresponding woocommerce order was not deleted.
How can I automatically delete incomplete woocommerce order when the corresponding related draft post has been deleted.
Thanks
Thanks for the feedback Ill like to proceed with this as my product setup is simple direct transactions and associated product orders only contain a single product related to the post. I set up 1 product per pricing. I dont use multiple products for the same order
I have this example code below
function delete_pending_post_order($post_id) { if (get_post_type($post_id) === 'post') { $order_id = get_post_meta($post_id, '_order_id', true); if ($order_id) { $order = wc_get_order($order_id); if ($order && in_array($order->get_status(), array('pending', 'draft'))) { $order->delete(true); } } } } add_action('before_delete_post', 'delete_pending_post_order');
Hello Tosin,
Try the following code:
function delete_pending_post_order( $post_id, $post ) { if ( empty( $post->post_type ) || 'post' !== $post->post_type ) { return; } if ( ! function_exists( 'bbl_ppp_get_post_order_id' ) || ! function_exists( 'wc_get_order' ) ) { return; } $order_id = bbl_ppp_get_post_order_id( $post_id ); if ( $order_id ) { $order = wc_get_order( $order_id ); if ( $order && in_array( $order->get_status(), array( 'pending', 'draft' ) ) ) { $order->delete( true ); } } } add_action( 'before_delete_post', 'delete_pending_post_order', 10, 2 );
Regards
RaviThanks Ravi After giving this a better thought I think emptying woocommerec cart might be better so I edited your code
function empty_cart_pending_post_order( $post_id, $post ) { if ( empty( $post->post_type ) || 'post' !== $post->post_type ) { return; } if ( ! function_exists( 'bbl_ppp_get_post_order_id' ) || ! function_exists( 'wc_get_order' ) || ! function_exists( 'WC' ) ) { return; } $order_id = bbl_ppp_get_post_order_id( $post_id ); if ( $order_id ) { $order = wc_get_order( $order_id ); if ( $order && in_array( $order->get_status(), array( 'pending', 'draft' ) ) ) { // Empty the cart instead of deleting the order. WC()->cart->empty_cart(); } } } add_action( 'before_delete_post', 'empty_cart_pending_post_order', 10, 2 );
Hi Tosin,
Hope you are doing well.
We have some festive holidays here. @ravisharm will be back officially on 24/25th. He will be assisting you with it after that.Regards
Brajesh
You must be logged in to reply to this topic.