Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display WordPress WooCommerce custom attribute in templates/functions?

Would put this in WP Stack Exchange, but often they say since it has PHP it should be in SO, so never quite sure where is best. Can move if more appropriate.

To display a custom woocommerce product attribute called, “Fabrics” for example I’ve read you could do the following.

$fabric_values = get_the_terms( $product->id, ‘pa_fabrics’);

foreach ( $fabric_values as $fabric_value ) {
   echo $fabric_value->name;
}

However, is there a shorter way since we would be using a lot of attributes throughout php templates.

For instance is there a way to simply do, “echo get_the_terms( $product->id, ‘pa_fabrics’);”

Or is there a single function one could add to their site, so that would then be able to echo any product attribute which a single very short line like above like you can when use “Advanced Custom Fields” with non WooCommerce sites?

UPDATE

Found this thread on SO that offers a way to create a single short code to relatively easily get the data. While that's certainly an option, would like to see if there is any cleaner built in ways such as:

echo get_the_terms( $product->id, 'pa_fabrics');

or

echo $product->get_attributes('pa_fabrics');

The last option seems the cleanest and most ideal but results in an error: "Fatal error: Uncaught Error: Call to a member function get_attributes() on null in (my functions.php file where the code was added).

like image 405
cchiera Avatar asked Feb 05 '23 07:02

cchiera


1 Answers

The answer to your question is it depends. Consider for a moment how flexible you need this to be.

Let's start by looking at what is wrong with the 2 suggested examples.

1) echo get_the_terms( . . .

When using a function it's important to know the return type. get_the_terms() will return an array when successful. You need to do something with that array in order to display it.

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

2) echo $product->get_attributes(...

You're heading down the right path :) The error you're seeing tells you that $product isn't what you're expecting it to be. get_attributes() is a method of the WC_Product class. You need to have an instance of that class in order to use it.

One way of getting hold of the product would be to use wc_get_product().

$product = wc_get_product();

Now the second problem you have is with the method itself. get_attributes(), like get_the_terms(), will return an array. It's then your responsibility to display that data.

Instead I believe you're looking for get_attribute(). This method takes an attribute name as its only argument and returns a string of attribute values.

Example:

// Get a product instance. I could pass in an ID here.
// I'm leaving empty to get the current product.
$product = wc_get_product();

// Output fabrics in a list separated by commas.
echo $product->get_attribute( 'pa_fabrics' );

// Now that I have $product, I could output other attributes as well.
echo $product->get_attribute( 'pa_colors' );
like image 126
Nathan Dawson Avatar answered Feb 08 '23 05:02

Nathan Dawson