Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Wordpress 

[Woocommerce] How to get a specified attribute value from a product in a cart?

As the title states, I'm trying to get a certain attribute value from a specified attribute name of a product. I mainly go through each product in the cart and try to get a certain attribute value. For example, a cart of mobile phones, having an attribute which is "Warranty" of values "yes"/"no". I need to get for each phone in cart the value of the specific attribute "Warranty" of slug "p-warranty", then do some logic after that.

I would really appreciate any tips, hints, or suggestions on this, and how to actually make it work.

Note: Couldn't actually paste the code here; blocked by cloudflare...

Thank you
Disclaimer: I have not tested the following codes/suggestions.

You could just list the attributes using the following code;
PHP Code

<?php 
$product
->list_attributes(); 

Then, proceed to copy the product-attributes.php file from the plugins/woocommerce/templates/single-product/ directory to the theme/theme-child/woocommerce/single-product/ directory to make your changes.

Or try the following code;
PHP Code

<?php 
$warranty 
= $product->get_attribute( 'pa_warranty' ); 

Another suggestion is the following code (custom taxonomy);
PHP Code

<?php 
$warranty_values 
= get_the_terms( $product->id, 'pa_warranty');
 
foreach (
$warranty_values as $warranty_value) {
      echo 
$warranty_value->name;
} 

If additional help is required as you did not provide any initial code, feel free to PM me.


Possibly Related Threads...
Thread / Author Replies Views Last Post
by BlockonomeJul 28, 2023 02:39 PM 4 194 Jul 28, 2023 02:39 PM
Last Post: Vibe
[Wordpress] by CryptoMar 7, 2022 04:48 AM 3 232 Mar 7, 2022 04:48 AM
Last Post: hacking-in-plainsight
[Wordpress] by Bru6Apr 16, 2021 12:52 AM 0 136 Apr 16, 2021 12:52 AM
Last Post: Bru6