0
votes

I'm trying to get products attribute of size in woocommerce
and here's my code :

<?php
    $test = $_product->get_attributes();

    if ( $test != NULL ) {
        foreach($test['pa_size']['options'] as $size){
                if ($size !== NULL) {
                    echo apply_filters( 'woocommerce_cart_item_size',  $size , $cart_item, $cart_item_key );

                }   else  {
                    echo "Not Specified";
                }
        }
        } else {
        echo "Not Specified";
        }
?>

and here if the product has attributes it will check if has attribute of size.
then It will return this size in the var $size
the problem that I got the result not as the value of the attribute.
the value is ( small , medium or large )
but I got a key value like this.

48

So how can I get the value of the attribute not the key.
Thanks in Advance.

1

1 Answers

0
votes

Well I found the solve here
https://developer.wordpress.org/reference/functions/get_the_terms/

and here's the code :

$test = $_product->get_attributes();
if(!empty($test)) {
    $terms = get_the_terms($product_id, "pa_size");
    foreach ( $terms as $term ) {
    echo "<option>" . $term->name . "</option>";
    }
} else {
    echo "Not Specified";
}