BuddyDev

Search

Buddyblog pay per post – how to delete pending oreders when draft is deleted

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #50755

    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

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2936
    Ravi on #50757

    Hello Tosin,

    Thank you for the posting. Deleting the associated order with the post is not a good idea as there might be more products associated with the order which will cause issues. So it will be good to delete it manually if necessary.

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #50758

    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'); 
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2936
    Ravi on #50760

    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
    Ravi

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #50761

    Thanks 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 );
     
  • Participant
    Level: Guru
    Posts: 885
    Tosin on #50762

    I tested the edited code but it did not work

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #50769

    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

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #50796

    Gentle reminder sir thanks

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2936
    Ravi on #50799

    Hello Tosin,

    I will give it a try today and will let you know.

    Regards
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2936
    Ravi on #50803

    Hello Tosin,

    I have checked and found that The cart is set to empty already by WooCommerce as soon as the order is placed. Please also check for the order status ‘processing’ and ‘on-hold’.

    Order deletion can be accomplished in this case.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: not resolved