Skip Navigation

[Resolved] Get the value of a custom field of a WooCommerce product

This thread is resolved. Here is a description of the problem and solution.

Problem:
Get the value of a custom field of a WooCommerce variable product (in the xml file generated by Google Product Feed).

Solution:
Please make sure to first add variations for the Variable product. And use the code and solution given here:
https://toolset.com/forums/topic/get-the-value-of-a-custom-field-of-a-woocommerce-product/page/2/#post-577980

This support ticket is created 7 years, 1 month 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 19 replies, has 3 voices.

Last updated by kristofG 7 years, 1 month ago.

Assisted by: Noman.

Author
Posts
#576139
Screenshot_10.png
Screenshot_9.png
Screenshot_8.png

I have read many posts and tried many snippets, but I can't seem to make this work ????
We have the WooCommerce Google Product Feed plugin, but we need more control over the <title> in the xml feed, so we found a snippet filter:

function lw_woocommerce_gpf_title( $title, $product_id ) {
    return xxx;
}
add_filter( 'woocommerce_gpf_title', 'lw_woocommerce_gpf_title', 10, 2 );

When we set the return to "test", it displays in the xml, so the code works.
However, we are unable to return the value of our custom field 'google-merchant-product-title', it is empty.

 return get_post_meta( get_the_ID(), 'wpcf-google-merchant-product-title', true); 

What are we doing wrong?

#576154

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Kristof,

Thank you for contacting Toolset support. I have tried to analyze your code and as I see you need to replace this line:

 return get_post_meta( get_the_ID(), 'wpcf-google-merchant-product-title', true); 

With this line of code:

 return get_post_meta( $product_id, 'wpcf-google-merchant-product-title', true); 

And it will return the correct product ID.

Thank you

#576158

Sorry, but that did not help, the <title> element is still empty

<title>
<![CDATA[ ]]>
</title>

#576202

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

That’s a bit strange, may be this is related to your case:
hidden link

If above is not sufficient still, please provide temporary access (WP-Admin and FTP Login info) to your site (preferably staging site), so that I can look into your setup and check this. Please note that we do not debug custom code or custom requirements much as per our support policy (https://toolset.com/toolset-support-policy/) but I would like to take a look and see what I can find.

Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please also let me know where the xml file or php file is located for this particular function, and if there is any page or CPT related to that where I can find that Custom field, please share all that needed info.

Thank you

#576495

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

403 error.png

Thanks for providing more info and login. I just checked and WP login info is not working.

First it said wrong user or password. On next try this link shows 403 Forbidden error:
hidden link

I have attached a screenshot. Please check and advise further so that I can login. Next reply will be private only.

Thank you

#577219

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I am sorry but it is still not working, at first it said wrong user or password and on 2nd try it gives 403 Forbidden error.

#577325

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks, but still not able to login. On first attempt to login, it says "Too many login attempts locked out"

Please disable your Security plugin (e.g. Wordfence plugin) or this security system temporary OR provide us access to an easily accessible staging site hopefully.

My network also having dynamic IPs and I often switch between my networks. Thanks

#577595

plugin has been disabled

#577629

Hi Kristof,

I'm posting here to notify you that Mohammed is off today. He will reply to you once he get back tomorrow.

Thanks.

#577816

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Please double check login info, it is not working, check here:
[ video removed ]

I will delete the above video on your next reply.
Thank you

#577818

It looks like you coppied "FTP Access Details" as part of the password.

#577826

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I have again tried:
[ video removed ]

#577828

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Now it is working, I am looking into this and will get back to you soon with an update.

Thank you for your patience.

#577972

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I have thoroughly checked this issue and tried to get the ID for variable product but I am not succeeded in that. When I check with Simple products it does get correct ID.

When I add variations for the Variable product, it does show variations Title in this format:

Product Title (Variation name) Product's Variation ID 

Test product I created: hidden link

But it does not return custom field value in the title with the given hooks of the plugin and this is true even for Standard WP fields (non Toolset fields).

I also checked WooCommerce api and that feed plugin’s API doc but I can't find anything that talks about getting variable product ID like we need it.

Since the issue also occurs with Standard WP Custom fields and standard WooCommerce variable products, it is mainly between WooCommerce + WooCommerce Feed plugin, this falls out of our support. I would suggest to please contact that plugin author and ask them if they have any such hook that can allow to modify Product Title in the feed for Variable products and replace that with a Custom field (native one), as this is not documented yet.

I assume they will probably have some internal hook for this. I will also try tomorrow and see if I can get it by any workaround but I am not certain on this at moment.

Thanks

#577973

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

UPDATE: I think I manged to make it work, I am testing it further and will update you here shortly, please wait few minutes.

Thank you