Sauter la navigation

[Résolu] Retrieving WC order ID inside cart and checkout pages

This support ticket is created Il y a 6 années et 1 mois. 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)

Marqué : 

Ce sujet contient 7 réponses, a 2 voix.

Dernière mise à jour par Beda Il y a 6 années et 1 mois.

Assisté par: Beda.

Auteur
Publications
#1153112

Pat

Hello,

In order to add some custom fields to the WC checkout process (cart, checkout pages and mails), I would like to be able to retrieve the WC order ID inside these pages in order to add to info thanks to a hook.
Today, it is really difficult to get this information (order ID) and even with hooks, this is not possible to add custom fields created by Toolset to the checkout process.

This is a pity as Toolset enables to have additional info to the product and we are not able to display this info in the cart / checkout page or in the WC mails.

Thanks to plan to have something available in the future for this.
Regards
Pat

#1154659

Displaying those fields would be a matter of inserting the shortcodes in a page in the backend if available or by code applying to WooCommerce's template, not Toolset's

Hence, you would have to know which fields you want to display where, and then either insert them with the shortCodes or by adding to filters of WooCommerce, if available.

What fields would you like to show in the Cart, Checkout and Mails?
Note that if you have several fields, you would probably have to decide which fields, of which products, to show.

In emails. this could also be done with the Forms notifications which can be fired when a Post or user is created or edited, and that can be connected to a certain product.
However, that is not a customization of the WooCommerce checkout itself, which we leave to WooCommerce to handle since it's working great already.

I would need more details about usage cases, where this would be used and how (it probably would require a whole new layer of compatibility, this is why I would need as much detail as possible.

Currently, most things could already be displayed, if within a template it can apply to (this may be a page, a PHP template..)
But, for example, you cannot add our fields with the GUI within WooCommerce's Checkout templates, only by using Filters which WooCommerce might offer, or another form of custom code.

Knowing where what data exactly should be displayed, can help to describe the feature request in more detail

#1155219

Pat

Hi Beda,

Thanks for taking care.

Let's take the following example :

I have a postype "Event" that helps to create events. The events are defined by their location, date ...
Then another postype "Inscription" that create a link between the event and the people that subscribes to the particuliar event. In the Inscription post, we need to retrieve all info from the event and all info from the people (in fact, we can use the parent child relationship between event and inscription).

Now, in the subscription process, the user will chose a specific event, place it in the cart and checkout.

All events have the same price and I have created a standard product with this price. Then, a WC Cred is also created that is linked to this product.

The issue there is that when the user arrives in the cart, he has only the info from the standard product without any info from the specific event he has chosen (location, dates ..).
So, WC Cred is fine when you have a specific product each time you create event, but if you want to use a generic product for different events, then, you are not able to retrieve the event info inside the cart or the checkout page. That's what I would like to have !

Hope this clarify the need.
Regards
Pat

#1161388

Thanks for the further details.

I need to clarify:

"Now, in the subscription process, the user will choose a specific event, place it in the cart"
Hence, Events either are WooCommerce Products or, since you state those are a separate Post Type, then how do you place that Event in the cart? Is this done thru a Product, connected to a Form, which lets users create or edit Events or other posts, and then need to pay the product as connected to the Form?
This is the only way I would know, with Toolset - to charge users for any form of interaction on the site.

I think my assumption is right, since "Then, a WC Cred is also created that is linked to this product".
I need to know, the form that is ligated to payment, what does it create?
The relation itself, the Event Post or the Subscription Post?
In any case, however, the "item" in the cart, will be the Product, hence the WC Post Type.

I think this is as well confirmed by "the issue here is that when the user arrives in the cart, he has only the info from the standard product without any info from the specific event he has chosen (location, dates ..).", correct?

That is expected since you can only place a product into the cart, you can only see information on that post type there since that is handled to 100% by WooCommerce.

What you need, in this case, is a method to display the Post (any kind of Post) that the user either created or chose as related to, when the user checks out the Product you bind to that form.

Hence, I recommend using the Forms Commerce API.
https://toolset.com/documentation/programmer-reference/cred-commerce-api/

That API allows you to get and manipulate data while using forms ligated to products
Toolset does not allow to customize the cart, so how to display this data on the cart, is something you need to ask WooCommerce Support.
However, you can get the data (the ID of the Event created or edited, or any other post, that is manipulated by the Form), while the user checks out.
You can for example store that value to a database entry or in any other place where then WooCommerce can grab and display it in the cart.

An idea could be to store the ID of the Event or Participation in a user Field and then display the User Field on the Cart Page of WooCommerce.
Updating the user field would be done with the Forms Commerce API, and update_user_meta().

Our support could give some examples on such code, but not craft is ready to go, for this, you would have to contact a Contractor if you need assistance in the custom coding.

The request *Retrieving WC order ID inside cart and checkout pages* would not help here, because the Order ID is not ligated to the post created at all, it is only ligated to WooCommerce Data of the product and its purchase.
However, if you'd want to display the WooCommerce Order ID in the WooCommerce Checkout page, that would be something to consult with WooCommerce Support, since it would not require Toolset (and Toolset would not help to achieve that).
This because Toolset only allows to customize the single product and archive template, but not the cart, and the Order ID is managed by WooCommerce, as well.

Please let me know if you have further questions.

I can add a request to customize and control the WooCommerce Checkout / Cart to 100%, that is a large request but I can file it, for the future roadmaps. I could however not guarantee it to be implemented.

#1162709

Pat

Hi Beda,

Concerning the product management, I have 2 ways of doing :

1. When the product is well defined and with a limited quantity, I'm using the standard WC product and display it on the site in order for the user to be able to place it directly in the cart. Then, products could have some additional Types custom fields and from now, I have no clue to display these custom fields inside the cart or the WC mails.

2. For some sites, I have chosen to use Cred forms in order to define some additional info for the product. This means the Cred form is linked to a product that serves only as a price definition (here, the called product is defined by a specific field inside the Cred form, depending on the user choice or defined thanks to the user role).
So, in this case, I have also custom fields that are inside the Cred form and have no idea how to retrieve them in the cart or in the WC mails.

So, whatever the solution I' using, I need to retrieve these info in the cart or in the WC mails.
Hope this clarify the need.
Regards
Pat

#1163573

1. If you have Fields on a Product, you can display them on a Single Product or an archive, as we do support that. You can not display them on the cart because we do not support to alter the cart or any other WooCommerce template so far.

As mentioned, I can add the request to modify the cart, but that is a large project, something that will first require a lot of assessments and preparation, and likely, if, implemented in a few years probably.
I can file this request

WooCommerce can give the API to alter those templates, with which you will then call and display the data. Getting the data will not be the issue, as Toolset offers ShortCodes and API to display or get this data, but not on the WooCommerce cart.
That is either requiring custom code with WooCommerce API or a feature in Toolset to alter the cart to this extent, which is as said a large project. I can file it.

2. The same as above is valid here, we have the data as fields in a post and can retrieve them easily with the ShortCodes or Toolset API but cannot display them in the cart, as related to the (or many) products in the cart, like that, is not supported.

Also here, customizing the cart and providing some globally accessible data about the products bought should solve this, but that is as said a large project - not a single request that can solve it.

I can file to allow customizing the cart with Toolset, for a starter, and the people with the oversight will then decide if this can be done.

Unfortunately for more, currently custom coding of WooCommerce templates is required.

#1163613

Pat

Hi Beda,

I'm aware of the difficulties to use Toolset firlds inside WC cart and mails !
Now, I'm OK to use WC hooks to add some info in the cart / mails.
This being said, it's still difficult to retrieve the needed info and integrate them inside the hook.

Any idea on this?
Regards
Pat

#1164692

Data created with Toolset is basically native WordPress data, so most of it you can get with the core API (WordPress) like get_post_meta() to get fields of a post, get_user_meta() to get fields of a user, and get_posts() or similar to get posts of a kind by conditions.
https://developer.wordpress.org/reference/functions/get_post_meta/
https://codex.wordpress.org/Function_Reference/get_user_meta
https://codex.wordpress.org/Template_Tags/get_posts

For fields, Toolset also offers a wrapper for the WordPress API, a Types API that lets you get fields:
https://toolset.com/documentation/customizing-sites-using-php/functions/

For related posts, means when you need to get data of a post that is related to a post by as Toolset Post Relationship, you would use this API which is similar to the get_posts() or query in general:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

These 3 ways offer the possibilites to get the data required, they need to be fired on the cart, hence within the hooks WooCommerce Offers.

I can provide single examples of how to get a field value, a related post, or similar.
My Pastebin https://pastebin.com/u/bedas, the Toolset DOC linked above and lien caché is already presenting many of those examples.
Please let me know if you need a precise example of some topic.

Note that we cannot offer help with the WooCommerce API.

Related to the request, should I go ahead and file that?

I would formulate "To allow customizing the cart of WooCommerce, inclusive displaying data of related (bought) items"

This is what we can handle in this ticket, for technical assistance I would recommend to open a ticket separately from this, however as abovementioned, we cannot help with the WooCommerce API.