Home Forums WoodMart support forum Get Woocomerce Poduct attribute color and image values

Get Woocomerce Poduct attribute color and image values

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #105863

    shuki
    Participant

    For a custom plugin im developing, im trying to get and list the color(hex) and image-url field values of a specific Woocomerce product pa_colors attribute. so far i can get the attribute title and description using the code seen here, but cant seem to find how to access color preview (hex) and image preview (url) values. here is my code:

    $_product = wc_get_product( $product_id );
    $attributes = $_product->get_attributes('pa_colors');
    
               foreach($attributes as $attr=>$attr_dts){
    
                   $attribute_label = wc_attribute_label($attr);
    
                   if ( isset( $attributes[ $attr ] ) || isset( $attributes[ 'pa_' . $attr ] ) ) {
    
                       $attribute = isset( $attributes[ $attr ] ) ? $attributes[ $attr ] : $attributes[ 'pa_' . $attr ];
    
                       if ( $attribute['is_taxonomy'] ) {
    
                          $values = wc_get_product_terms( $_product->id, $attribute['name'],array( 'fields' =>  'all' ));
                            if( $values ){
                                    foreach ( $values as $term ){
                                        echo '<dh>' . $term->name.' </dh>';
                                        echo '<dd>' . term_description( $term->term_id, $term->taxonomy ) . '</dd>';
                                       //here is where i want to get the term color values and image url.
                                    }
                                echo '</dl>';
                            }
    
                       } else {
    
                           $formatted_attributes[$attribute_label] = $attribute['value'];
                       }
    
                   }
               }
    #105878

    Artem Temos
    Keymaster

    Hello,

    You can get these values with the following code

    $color = get_term_meta( $term->term_id, 'color', true );
    $image = get_term_meta( $term->term_id, 'image', true );

    Regards

    #105914

    shuki
    Participant

    you made my day ! thanks

    #105915

    Artem Temos
    Keymaster

    You are welcome.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Get Woocomerce Poduct attribute color and image values’ is closed to new replies.