Skip Navigation

[Resolved] Get the values of new fields created in woocommerce products through PHP.

This support ticket is created 5 years, 4 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 12 replies, has 2 voices.

Last updated by Francisco Ramón Molina Busquiel 5 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#1293823

Hello,

If I create new fields for WooCommerce products, how can I obtain the values of those fields in the product the user is currently on through PHP?

Another thing, I suppose that to obtain the values of the current product it will be necessary to know the id of the product that is currently being viewed.
How can I get this product id?

Thanks for your help.
Regards,
Francisco R.

#1293939

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francisco,

Thank you for contacting our support forum.

If these fields are custom fields created in Types then you can just use the Types field api to retrieve the field values using php.
https://toolset.com/documentation/customizing-sites-using-php/functions/

To get the product id you can use the shortcode [wpv-post-id]

Thanks,
Shane

#1294037

Hi Shane,

The documentation is not clear at all. It's a mess. 🙁
For example, I create 2 new numerical fields for WooCommerce products.
Field1: slug: s_1
Field2: slug: s_2
Now I want to get the value of field1 of the product with id = 20
How would it be done?
Only the "types_render_field" function is used in the documentation.
It would be something like this?
$cc1 = types_render_field ("s_1", array ("output" => "raw");
but here, where do you put that is the value of the product with id = 20?

Another thing.
In the documentation put a link "Read more about displaying custom fields", but this link leads to https://toolset.com/documentation/getting-started-with-toolset/create-templates-to-display-custom-posts/
It has nothing to do with the other.

Another thing.
In the documentation in output it says: 'raw' | 'normal' (default), but below it also puts 'html'.
It means that there are 3 possible values ​​for output?

Another thing.
In the documentation when "More" is displayed, more attributes are shown, for repeater, user, term, but it does not specify how they are used.

Nor does it make it possible to go through all the records (loop) of a new type created with Types.

Everything is very confusing.
It is all so brief that it does not serve much. 🙁

Can you give me more help in this regard?

Thanks,
Regards,
Francisco R.

#1294083

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francisco,

You can get the specific field like this.


$cc1 = types_render_field ("s_1", array ("output" => "raw","item" =>"1234");

Where 1234 is the id of the post .

This URL is actually correct because it shows you how to build the custom templates as well as include custom fields into those custom templates.

In the documentation in output it says: 'raw' | 'normal' (default), but below it also puts 'html'.
It means that there are 3 possible values ​​for output?

Lets say your custom field is a URL field. If you set the URL field to display normally it will display the URL as a clickable link. If you set the display to normal it will print out the exact url without wrapping it in the HTML for an anchor tag.

In the documentation when "More" is displayed, more attributes are shown, for repeater, user, term, but it does not specify how they are used.

These are as specified "repeater" means these attributes apply if the field is a repeatable field. "user" makes reference to if this is a custom field for users and "term" are attributes for custom fields on taxonomies.

This document is an API document its usually meant for intermediate to advanced users who are familiar with coding.

Thanks,
Shane

#1294101

Hi Shane,

But even for developers, these are not born knowing the apis of any product, especially because they have not made that product. For example, in the ACF documentation there are many examples and it is much more extensive. The ACF documentation is made to be able to really use the ACF API, it is not just an exposition of attributes, etc. The same can be said of other products. The Formidable Forms documentation is fantastic and if something is not sufficiently explained the support team give detailed explanations.

Toolset has many limitations, so it is necessary on many occasions to do things in PHP. The minimum would be to have extensive documentation and numerous examples.

Regards,
Francisco R.

#1294105

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francisco,

I do recognise that the documentation is quite limited on how to use the items so we here in support try to see how best we can explain to customers how to use the products or the API.

Also was my explanation in the previous thread able to help you to get through the issue you were having ?

Thanks,
Shane

#1294123

Hi Shane,

I think so.
I appreciate your help.
Although you did not explain how to do a loop to get all the values of all the records of a new type created by Types.

Thanks for your help.
Francisco R.

#1294157

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francisco,

Could you explain a little about looping through records created with types.

Are you referring to getting all the posts created in types?

Thanks,
Shane

#1294451

>> Are you referring to getting all the posts created in types?
Yes. Please.

Thanks.
Francisco R.

#1295031

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francisco,

To do this with php you will need to use this function here .
https://codex.wordpress.org/Function_Reference/get_posts

This will get all the posts, all you need to do is specify the post type and it will give you all the posts in an array.

However if you have our views plugin you can use this to list out all the posts as well.

Thanks,
Shane

#1295071

Hi Shane,

Ok. Thank you very much.

Regards,
Francisco R.

#1295223

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Francisco,

Is everything clear now ?

Are there any concerns about this ?

Thanks,
Shane

#1296959

My issue is resolved now. Thank you!