Skip Navigation

[Resolved] Delete created post by cred commerce on cart abandon and clear cart

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Author
Posts
#1097334

Delete created post by cred commerce on cart abandon and clear cart

#1097366

You can (using Toolset API) hook at these moments of a checkout process:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/
cred_commerce_form_action (just after form is submitted)
cred_commerce_before_add_to_cart (Before adding to cart)
cred_commerce_add_product_to_cart (When product is added to cart)
cred_commerce_after_add_to_cart (After it is added to cart)
cred_commerce_after_payment_cancelled (payment cancelled)
cred_commerce_after_payment_completed (payment completed)
cred_commerce_after_payment_refunded (payment refunded)
cred_commerce_after_send_notifications (after all above is done and WooCommerce sends out emails)
cred_commerce_after_order_completed (order is completed)

So I think you can either listen to cred_commerce_after_payment_cancelled, as we have no other methods such as check a moment when the cart is cleared.
I think for this you need to hook in WooCommerce API's directly:
woocommerce_cleanup_sessions
hidden link

This is however not Toolset API with which we can assist.
You can try to run a wp_delete_post() on woocommerce_cleanup_sessions().
Or, a wp_delete_post() on cred_commerce_after_payment_cancelled().
https://codex.wordpress.org/Function_Reference/wp_delete_post

#1100260

Hello Beda,

I replied with the credentials on the other ticket while I can't submit it here privately

#1100574

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

We don't need credentials for your site.

As Beda explained above, there are a series of hooks available throughout the process of making a purchase from Toolset Forms via WooCommerce.

There isn't an explicit hook available for when a cart is abandoned.

I don't believe any of the hooks we provide will be triggered by an abandoned cart.

WooCommerce's woocommerce_cleanup_sessions hook appears to be a scheduled deletion of old session data, and I took a quick look at it and I don't think it can help you either.

There are quite a few plugins available for recovering abandoned carts, which typically send an email to the user that abandoned a cart after a certain interval.

It may be that one of those might have a hook that could be used instead of sending an email to trigger custom code to delete the redundant post, but you would need to check the documentation for them to see.

You could try contacting a one of the developers listed under Toolset Contractors (https://toolset.com/contractors/) to see if they were willing to undertake the work.

#1100789

Hello Nigel,

To be clear in my question. What I need is a way to know the newly created post by the CRED Commerce the moment it’s created.

The description about the cart and it’s scenarios was only to explain how important it is in order to continue with the workflow.

Is there any action can be added to the add product to cart hook to get the created post id?

#1100794

I tried the querystring array hook to collect the id of the new created post from the cred form using the url parameter but that didn’t work.

#1101432

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, thanks for clarifying, let me set up a test site and see if I can confirm.

#1101546

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

From your question it looks like you are following the workflow for charging users to post content (rather than to register), as described here: https://toolset.com/documentation/user-guides/using-cred-commerce-to-add-payments-to-forms/

On my test site I have a form (linked to a product) that submits draft posts, and which redirects to the checkout page.

So when that form is submitted
- a draft post is created, and the linked product is added to the cart

The following API filters fire in this sequence:

cred_commerce_before_add_to_cart
cred_commerce_add_product_to_cart
cred_commerce_after_add_to_cart
cred_commerce_form_action

The id of the newly created post is available from any of those filters.

When the checkout page is completed a WC order post is created.

The following API filter is triggered (even if you have no notifications):

cred_commerce_after_send_notifications

The post id, order id, and product id are all available with this filter.

When the Order status is marked as complete, the status of the original post is updated according to the form settings (typically it is published), and the following filters are triggered:

cred_commerce_after_payment_completed
cred_commerce_after_send_notifications

If you just want to know the id of the post created when the form is initially submitted, you can of course just use the normal CRED API hooks, e.g. cred_save_data.

#1101707

Hello Nigel,

Thank you for your reply.

I wonder if I can jump between those actions or call another function / shortcode to display the newly created post ID on the cart / checkout page so I can build a view that displays the rest related fields of the booking info and the parent product that is in the cart.

In that case, I can try finding a way to delete that new post after some certain time if the payment is not processed.

on the other hand when the payment is successful, I'll go with your advised procedure in you reply.

#1102474

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

The published post will have a draft status, assuming that's what you set in the Form settings, and as a post it will have a published date that records the time the post was submitted.

If the payment is completed, the process will update the post to have a published status (again, assuming that's what your Form settings are).

So, it seems to me the best option would be to run a scheduled job that gets all of the draft posts of this type and deletes all of those which are older than x number of hours.

See here for details about creating a scheduled job: https://codex.wordpress.org/Function_Reference/wp_schedule_event

#1102641

Hello Nigel,

I visited the link you advised where they address using GMT timing and not local timing.
When I checked the booking post publish date on the database i found that GMT times are zeros.

#1104340

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That is the standard behaviour of wp_insert_post, if a draft post is created the post_date is set but not the post_date_gmt.

(If you Google, you'll see this comes up a lot.)

The following example produces a post with no post_date_gmt:

$args = array(
  'post_title' =>'test post',
  'post_content' => 'some post stuff'.
  'post_status'	=> 'draft'
  );
wp_insert_post( $args );

So you'll need to work with post_date, not post_date_gmt.

#1108720

Hello Nigel,

Thank you for your reply,
I successfully created a view in the checkout page to render the last created booking / pack post by the current user And set the post date filter as:

Select posts whose
Published date is after or equal to: minute:PAST_ONE(19)

in the Query filter.
The problem is when I set the general wordpress UTC to another country/location.
I currently set it to UTC 0 and the filter works well

Then I added that part:

[wpv-no-items-found]
<h1>Cart Cleared Try Again</h1>
[clear_cart]
[/wpv-no-items-found]

New threads created by Beda and linked to this one are listed below:

https://toolset.com/forums/topic/wrong-results-in-view-filtered-by-date-if-wordpress-timezone-is-not-utc/

#1109462

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

In that case I think you would need to intercept the Views query using the filter wpv_filter_query.

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Here is the boilerplate I use for that filter:

function tsupp_custom_query( $view_args, $view_settings, $view_id ){

    if ( in_array( $view_id, array( 66, 77 ) ) ) { // Edit for View IDs to modify

        // do stuff

    }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_custom_query', 101, 3 );

I would use this, edit the View ID(s) as appropriate, and then for stuff to do, begin with just dumping the view_args to the debug logs so you can inspect the current arguments, e.g. with

error_log(print_r($view_args, true));

You'll want to adjust the date argument to allow for the timezone, which you can get (in hours) with:

// get UTC offset for current WP timezone
$offset = get_option('gmt_offset');

If you get stuck let me know and I'll try and set up something similar on a local test site.

#1110694

Hello Nigel,

Thank you for your reply, off-course I'm getting stuck. I created the view on the checkout page layout not via php.
So, Please guide me how to achieve it with the php

Thank you 🙂