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
#1111381

Nigel
Supporter

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

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

Sorry, I am getting lost as to what you are trying to achieve. I don't understand why you are creating a View on the checkout page.

My understanding of the issue is that you want to automatically clean up posts which are created but which become redundant because the cart has been abandoned.

My thoughts on the best way to achieve that are here: https://toolset.com/forums/topic/delete-created-post-by-cred-commerce-on-cart-abandon-and-clear-cart/#post-1102474

If that's not something you are able to do yourself you'll need to recruit a developer to help. We have a list of contractors familiar with Toolset here: https://toolset.com/contractors/

Sorry I can't be more help.

#1111527

Reply can be left empty when here is nothing to say .

#1111569

Nigel
Supporter

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

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

Once again I suggest you hire a developer to do such work, we can't do develop your site for you.

#1111583

I haven't asked you to develop it for me. I asked for the support I paid for when I purchased this tool. I expected to see straight forward answers to my problem as I'm asking an expert. Please go through the ticket from the beginning and see.

Anyway, I'm not expecting to recommend this tool to anybody because of the lack of clear documentation that lead to deal with such experience with a support agent who feels he does the work for the customer instead of helping show how the tool is valuable to deliver a finished job.

Please escalate the ticket to another agent.

#1112060

Hi Ahmed, I went thru the ticket, and see the problem initially reported is resolved.

I notice you then switched topic to another issue, a view that returns wrongs results, if the timezone settings are changed, is this correct?
Please report this issue in another ticket, so we can follow up there.
I did so here:
https://toolset.com/forums/topic/wrong-results-in-view-filtered-by-date-if-wordpress-timezone-is-not-utc/

Note that we do not provide Custom Code.
https://toolset.com/toolset-support-policy/

We can provide examples of usage of our API, which Nigel did extensively, with working site examples I see.
We cannot support abeve that, and/or other but Toolset API questions when it comes to code.

However, the issue with the View that does return wrong results will be looked at in the new ticket, and the Post ID of the post created while a checkoutprocess is ongoing is as Nigel elaborated accessible at the mentioned different stages and hooks.

However, you can already control what happens with that post when the payment is canceled, it's elaborated here:
https://toolset.com/documentation/user-guides/using-cred-commerce-to-add-payments-to-forms/#3-1-updating-the-post-status-when-the-payment-status-updates

Let's see to hope the other (Views) issue in the other ticket!

#1112275

Hello Beda,

I didn't go beyond the scope of the ticket. Let's separate the UTC/GMT issue aside in a different ticket as you kindly done.

Getting back to the view displaying the created post via cred commerce. I placed a delete post button in the template loaded via the view loop. So, I can create a js timeout to trigger the delete after a period of time.

The problem is:
The user might have done previous bookings or ordered packs (another CPT) which will display by using this author filter.

The Target is:
As I limited the view do loop 1 post, I want it to stop looping upon deleting the first displayed post. So, it won't bring other posts to display on the checkout page.

Nigel posted:

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 didn't know where and how this will manage the view, but it seemed loading the view from php instead of placing it in the layout. Which simply wasn't a clear example.

Another Option around:
If you see it's doable but in another ticket, Please let me know:

If I can load a view using Ajax without refreshing the page, So I can set condition between the dates selected and the dates looped in that view.

Thank you for your support,

Ahmed

#1112734

Getting back to the view displaying the created post via cred commerce

This is not the issue of this report.

The issue was:

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

https://toolset.com/forums/topic/delete-created-post-by-cred-commerce-on-cart-abandon-and-clear-cart/#post-1097334

I placed a delete post button in the template loaded via the view loop. So, I can create a js timeout to trigger the delete after a period.

I understand that Button will then supposedly delete the Post the form created if some conditions are correct, and the delay is expired (timeout trigger)

The user might have done previous bookings or ordered packs (another CPT) which will display by using this author filter.

What user filter?
I am not sure what you refer to. We suggested a filter of Toolset Forms, which will return you the Post ID of the post the Form creates.

As I limited the view do loop 1 post, I want it to stop looping upon deleting the first displayed post. So, it won't bring other posts to display on the checkout page.

I understand.

This an inadequate approach generally as if you output one post only, the idea and purpose of a loop are defied.
We generally do not suggest to use a View to output one post, but instead to use a Content Template for example, or just "the post" itself.
In this situation, you are using Views not as an output editor but as a Query Builder and return the results of that query.
This is also possible, as you have done it, however, doing so the query and output obeys the logic and rules of the WP_Query and PHP/SQL.
That means, a Loop will always loop as many times you set it to, and if you reload a page, whereas before there where two posts resulting in the query but the result is limited to one, now you will still see one (as the current issue you describe)

That is unavoidable by the logic of the code.

Nigel posted:

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 didn't know where and how this will manage the view, but it seemed loading the view from php instead of placing it in the layout. Which simply wasn't a clear example.

Sure, no issue, I can help you exactly as far our Support Policy allows us about (https://toolset.com/toolset-support-policy/)
1. That Code snippet is a "blueprint" for "how to use" the Views Filter "wpv_filter_query()" which is explained here:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

2. In WordPress, you have a chance to apply filters to the code you write. The above code Nigel shared is a blueprint for a code that crafts new rules for the query created by Views. With it, you can overwrite or extend what Views already does.

3. We can explain where, and how to use the hook.
For example, the one Nigel mentions is used to alter the arguments that create the query, BEFORE they are passed the WP_Query (WP_Query is the one of WordPress, that searches the database and gives Views some results).

So, everything that is inside that hook would be using native PHP and WordPress WP_Query syntax.
We do not assist such custom code that is added within the actual hook.

The Document, however, has an example, which I will use to illustrate it's usage.

The below snippet, if inserted to the theme's function.php would Return only posts from the current user, when listing posts of type "company".
Note that you still need to insert the view somewhere on your website, otherwise, by just inserting the below code to the functions.php nothing will happen.
I will explain later how to actually display a View with code, this one is only about filtering it's results or queries.

So here the code snippet, please read it's comments and consult the PHP DOC and WordPress Codex linked below to get familiar with coding, especially with WordPress filters and hooks, which are important if you decide to manipulate things with code.

//Add Filter, this is a WordPress core function that adds your custom function below to the View's wpv_filter_query 
//prefix_show_only_current_author is the name of your function.
add_filter( 'wpv_filter_query', 'prefix_show_only_current_author' );

//Here we actually create our function
function prefix_show_only_current_author( $query_args ) {
    //Set the WordPress global for the users data
    global $current_user;

    //Set the post types we want
    //Here Nigel's tip is helpful to var_dump($query_args) to see what is avaiable natively already
    $types = (array) $query_args['post_type'];

    ///If we are not in the Admin, and we are listing the proper post type
    if ( !is_admin() && in_array( 'company', $types ) ) {

        //Then set new Query Arguments (again, var_dump them to see what is there), we set a new Author to query by
        $query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
    }

    //Return the new query args to add to the wpv_filter_query
    return $query_args;
}

https://codex.wordpress.org/
hidden link
https://codex.wordpress.org/Function_Reference
https://codex.wordpress.org/Function_Reference/get_currentuserinfo

Now, it seems you need to have an output of a View that lists a post (so to delete it), but there may be more than one post, and after removing one from the list the next one pops up.
You would need to find what it is that makes those posts unique.
Then, query the View so that those posts are not returned.

For example, you can instead of using a View, simply add a Custom action to the Delete Button that deletes the post with ID such as the Forms filter return (Nigel explained that the Toolset Forms API returns the created post_id).

If you managed to get that post ID, you could use that Post ID to create a code that deletes it.
Or, you can even query the View itself by that specific Post ID if you want that. However, it is overkill as if you expect one only post, a Loop is not needed.

You could just display the post or whatever data of it with a Content Template.
Or even a Custom ShortCode that returns just the ID of the post, so you can pass it to your Custom Button if needed.

You can as well use the Toolset Delete post buttons; they accept an ID as well.
It's not the easiest to insert this on the cart, however, as we do not support those to be customized (only archives and single products are)

Here we come to how to display a View or template with code, instead of ShortCodes.
https://toolset.com/documentation/programmer-reference/views-api/

With those, you can render a View or Template, so it's like you insert the View's ShortCode, but with PHP.
That can help to place a specific Content Template on the checkout, https://toolset.com/documentation/programmer-reference/views-api/#render_view_template, which then in turn, for example, has a delete button, or any else data related to your post that the form created.

As said, it is not trivial to access that post id, as the Form is creating it while the checkout is ongoing, and hence, I again reccomend to use the Draft modus Forms offers if the payment is not fullfilled, as explained.

I can take a request to add there a "Delete Post" option, so this would not be done by code, as it requires you to do now.
I can however not assist the custom code required further than above, or loading Views by AJAX (which is possible, but we cannot assist it either).
In this case, you need to craft a call to an action in JS which in turn fires a PHP action that calls the View or any PHP template so to render what is in it.
However this is above the scope of our support, I apologize.

#1112797

Hello Beda,

Thank you for your thorough reply above, I understand and appreciate the efforts you made to make it as clear.

In this reply I'll just quote "This an inadequate approach generally as if you output one post only, the idea and purpose of a loop are defied.
We generally do not suggest to use a View to output one post, but instead to use a Content Template for example, or just "the post" itself."

The reason I used the view to display the post is I didn't find in the documentation a way to apply filters on Content template which would be more functional not to have a loop an go with the hassle of fixing the un-needed results.

If you have a snippet or a link to a reference on toolset website to show example of how to to add extra filters while placing the content template, Please do. Or please advise if you require I open another ticket for that matter.

I'll go through your rich and precious reply and will post my reply very soon.

Thank you very much,

Ahmed Hussein

#1113583

Yes, I understand the exceptional needs of a loop to output single items as you say.

Content templates don't accept filters.
But you can display Content Templates within either HTML conditions or PHP conditions that check upon the data you want to be checked.
Of course, that's easier in a View, and fine like that, but as previously stated, if you work with the loop, the loop will act as one and return as many results as found.

I think we remain with "I can take a request to add a "Delete Post" option to Toolset Forms when a Payment is cancelled"

That makes sense.
Further, I cannot assist with Custom Code.

Please let me know if you want such feature; I can file it so it can be voted upon.

#1113919

My issue is resolved now. Thank you!