Skip to content
Advertisement

WooCommerce check if product attribute has value

I’m trying to amend an existing Woo code snippet to change how prices are displayed. I need to display certain text if a certain attribute is present on a variable product.

I thought this might work >

JavaScript

Or maybe this

JavaScript

Neither of the above work….

It’s to sit insde this code (in functions.php) and to eventually replace the text in $wcv_price if the word ‘Rent’ is not found as a price attribute. I’m obviously not a php genius…. 🙂

JavaScript

Advertisement

Answer

To get the value of a product attribute, you can use the get_attribute() method on the product class.

Example:

JavaScript

This method will return a string.

Where this gets slightly more complicated is when you potentially have multiple values for a given attribute. get_attribute() will return a comma delimited string of values. In that case, you’d need to convert it back into an array.

Example:

JavaScript

Full version of check:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement