Raj
Raj

Reputation: 137

How to Add Attribute Terms Image in WooCommerce?

In WooCommerce, I have created an attribute "Brand", and added some terms, like "Brand One", "Brand Two" etc.. I want to add an image for each term. Right now there is no option to add image in attribute terms.

Please tell me how to add image in attribute terms.

An admin link is like this:

 .../wp-admin/edit-tags.php?taxonomy=pa_brand&post_type=product'

Upvotes: 4

Views: 19207

Answers (5)

Heemanshu Bhalla
Heemanshu Bhalla

Reputation: 3763

I've fixed this issue. Actually i was not passing the right value in taxonomy. I was using variation swatches plugin so was not know which value to pass for taxonomy. below is the working code. I was trying to fetch "brands" attributes list with images.

$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();

if ($attribute_taxonomies) :
    foreach ($attribute_taxonomies as $tax) :
        if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :
            
            if($tax->attribute_name=="brands"){
                $taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'number=6&orderby=name&hide_empty=1');
            }
        endif;
    endforeach;
endif;

foreach ($taxonomy_terms as $item) :
    foreach($item as $child):
        //print_r($child);
         $thumbnail_id = get_woocommerce_term_meta( $child->term_id, 'product_pa_brands', true );
        $textureImg = wp_get_attachment_image_src( $thumbnail_id );
       

//we are getting image in $textureImg[0]

        }
    endforeach;
endforeach;

Upvotes: 0

Faisal Ashfaq
Faisal Ashfaq

Reputation: 2678

"Variation Swatches and Photos extension" plugin is premium and no body would like to purchase that to serve such a purpose as getting an icon image for a brand.

The "Taxonomy Images" has not been updated since WP 3.6.1...

What I came up with is Category and Taxonomy Image and that does the job.

Here's how you can get the image URL:

    if (function_exists('get_wp_term_image'))
    {
        $meta_image = get_wp_term_image($term_id); 
        //It will give category/term image url 
    }

    echo $meta_image; // category/term image url

Upvotes: 0

tivnet
tivnet

Reputation: 2021

With the "Variation Swatches" plugin (see the answer by @Dre), everything works smoothly, and getting the image is as easy as this:

$swatch_term = new WC_Swatch_Term( 'swatches_id', $term_id, $taxonomy, false,
                'swatches_image_size' );

$html = '<img src="' . $swatch_term->thumbnail_src . '" alt=""/>';

The "Taxonomy Images" has not been updated since WP 3.6.1...

Upvotes: 2

Dre
Dre

Reputation: 2953

WooCommerce stores product attributes outside of the usual taxonomy table, so you'll need to go with something more WC-specific. Try the Variation Swatches and Photos extension.

UPDATE: You can use the Taxonomy Images plugin but you have to make a minor alteration. By default the plugin only displays taxonomies that are set to display in the admin area (i.e. the show_ui value is set to true). WooCommerce hides the product attribute taxonomies by default, so the plugin will not display them in the settings screen. You can change this behaviour by commenting out/deleting lines 402-402 of taxonomy-images.php:

    if ( ! isset( $taxonomy->show_ui ) || empty( $taxonomy->show_ui ) )
        continue

Removing these lines will allow he plugin to display all taxonomies, regardless of whether they are hidden or not.

Props to @helgatheviking for suggesting that plugin in the first place

Upvotes: 5

helgatheviking
helgatheviking

Reputation: 26319

Technically a WooCommerce "attribute" is just a WordPress Custom Taxonomy. Therefore I would try something like the Taxonomy Images plugin.

Upvotes: 0

Related Questions