Skip Navigation

[Resolved] Add custom field info inthe WC cart

This support ticket is created 7 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 12 replies, has 2 voices.

Last updated by Christian Cox 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#482763

Pat

Hello,

I have a WC + Toolset site and would like to add some info in the cart. So, I prepared a child WC template for the cart page, but have some question about the integration of a custom field in it.

Here is my current definition :

I have a post_type (inscription-box) which have a specific custom field (concours). I have also a Cred that creates a post (inscription-box) and that contain this custom field : (concours). This Cred is a WC Cred which is linked to a product (box-membre) that defines the price to pay.

Now, when someone place an order on this, the cart is like this :
box-membre 50€

I would like to have that :
box-membre
concours NIMES 50€

with concours NIMES the value of the custom field concours (defined in the Cred form.

What do I need to place in my cart template to get this?
Regards
Pat

#482911

Hi Pat, I'll be happy to help with that. Can you tell me more about your template?

1. How was your template created? Is it a custom PHP file, or have you used Toolset Views to create a Content Template, or some other method? Please share your template with me.
2. How is the template applied to your cart page?
3. How is the cart added to your template? Are you using PHP, or a WooCommerce shortcode?

It will be helpful for me to see some screenshots of your template setup so I can assist.

#482914

Pat

Hi Christian
Thanks for your support.

I'm using Toolset Starter theme
I have a standard page layouts with only the content in it for this cart page
I'm using the WC shortcode to add the cart to the page : [woocommerce_cart]

Regards
Pat

#482921

Hi Pat, modifying the WooCommerce cart item display would require custom PHP code. Toolset currently supports using the shortcodes you can find here:

https://toolset.com/documentation/user-guides/views-shortcodes/

If you need professional help with this code, you can contact one of our certified partner contractors. Here is the list of our contractors:
https://toolset.com/consultant/

Please let me know if I can be of further assistance here.

#482928

Pat

Hi Christian,
I was more or less thinking about a solution with shortcode.
The issue for me is that I'm not sure how to declare the required custom field (concours)?
Any idea?
Regards
Pat

#482942

Hi Pat,

Unfortunately there is currently no way to do this with a shortcode because you do not have the ability to modify the template for [woocommerce_cart]. The entire cart is rendered by the [woocommerce_cart] shortcode, meaning there is not a place on the WordPress backend where you could add your own shortcode into each product row. Custom PHP would be required to modify the cart template.

If you would like, I can pass this on to our developers as a feature request. They would take your input under consideration for future development, and may be able to work this into a future release.

#482980

Pat

Hi Christian,

Thanks for your feedback.
I'm OK with custom PHP, but need to have a better understanding on how to retrieve the custom field in the cart environment.

Would it be possible for you to give some highlights on this?
Thanks
Pat

#482999

Hi Pat,

Sure, here is the general concept. You need to override the WooCommerce cart templates so you can add your own custom code to each row. Your custom code will display your custom field value. I'm not sure which files you would need to update, so here's some documentation from WooCommerce about their templates:
hidden link

I assume the cart creates rows of items by looping over products in the cart. Inside that loop, you would be able to access the product's ID. Then you can get the product's custom field value using the product's ID and the Types API:
https://toolset.com/documentation/customizing-sites-using-php/functions/

Then you would output that custom field value in each row of the new template for your cart.

#483025

Pat

Hi Chrisitan,
Sorry if not been clear enough.
My issue is not to manage the php and the child WC theme.
I need help to know how to retrieve the "concours" info to place in the cart.

As already said :
I have defined a post_type (inscription-box) which have a specific custom field (concours). I have also a Cred that creates a post (inscription-box) and that contain this custom field : (concours). This Cred is a WC Cred which is linked to a product (box-membre) that defines the price to pay.

So, in order to buy an inscription-box, th euser has to go in a specific page where the Cred form is integrated. He chose the specific concours he needs and this info is stored in the "concours" custom field. Then, after validating his order, he his redirected to the cart page.

The cart looks like this :

box-membre 50€

I would like to have that :

box-membre
concours NIMES 50€

with concours NIMES the value of the custom field "concours" recorded in the postype "inscription-box" created thanks to the Cred form.

Regards
Pat

#483182

Pat

Hi Christian,

Just to clarify what I need from Toolset :

I need to be able to retrieve some postmeta of the postype (inscription-box) that have been created with a WC Cred form associated with the product "box-membre" in order to add this info the the cart thanks to php modification in the child WC template.

Looking at the database, I have found this in the order postmeta :
s:307:"a:3:{i:0;a:3:{s:15:"cred_product_id";s:5:"41293";s:12:"cred_form_id";i:7543;s:12:"cred_post_id";i:41436;}}";

Here, the postype ID is 41436 and the ID of the product which is currently placed in the cart is 41293.
So, I'm sure that we should be able to retrieve the needed info but currently, I have no idea how to get it.
Regards
Pat

#483772

Hi Pat,

If I understand correctly, you would like to know how to get the value of a custom field in PHP. That can be accomplished using the Types Fields API. I posted a link before:

https://toolset.com/documentation/customizing-sites-using-php/functions/

There's information here about how to get the value of any type of custom field. For example:

<?php echo types_render_field("concours", array("id" => "41293")); ?>

This code outputs the value of "concours" for a post (in your case, a product) with ID 41293.

#483782

Pat

Hi Christian,

Just one point : the custom field "concours" does not belong to product postype. It belongs to another postype (inscription-box).
Second, I'm using different products depending of some custom fields defined in the Cred
SO, I would prefer to have a php function not integrating an ID, but something more specific (ID of the product of the cart item (each item of the cart should be processed like this).

So, the php function should look like this :
- Get the order ID
- For each line of the cart
- Get the cred_post_id
- Get the custom field "concours" of cred_post_id
- Next line of the cart

Regards
Pat

#483803

Okay I think I understand better now. You know the order ID, and you want to use that to get the cred_post_id. It's serialized in the post meta entry for this order.

You can use the get_post_meta WordPress function to retrieve the value of meta_key cred_post_id given an order post ID. Here's the WP codex:

https://developer.wordpress.org/reference/functions/get_post_meta/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.