Replace WooCommerce attribute labels by a custom image for each

后端 未结 1 1948
礼貌的吻别
礼貌的吻别 2021-01-22 17:28

I am working on project and I need some group help.

I am using woocommerce product system and on shop archive page product I am showing attribute-label: attribute-value

相关标签:
1条回答
  • 2021-01-22 17:54

    Updated since WC 3.2+

    As Product attributes dont have images, you should create a in your active theme a folder images (if it doesn't exist) and inside a subfolder attributes.

    For each product attribute, you will have to add an image inside this subfolder attributes, which name will be the name of your attribute (the slug). For example for "Color" attribute you will have to add an image named color.jpg.

    Then I have make some changes in your code, to get this working. Only the terms set in the product for each attribute will be displayed. For each attribute you will get an image.

    Here is the code:

    add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
    function isa_woocommerce_all_pa(){
        global $product;
    
        $attributes = $product->get_attributes();
    
        if ( ! $attributes ) return;
    
        $out = '<ul class="custom-attributes">';
    
        foreach ( $attributes as $attribute ) {
    
            if ( $attribute->get_variation() ) continue; // skip variations
    
            if ( $attribute->is_taxonomy() ) {
                $taxonomy = $attribute->get_name();
                $taxo_obj = $attribute->get_taxonomy_object();
                $name = $taxo_obj->attribute_name; // <== Corrected
                $label = $taxo_obj->attribute_label; // <== Corrected
    
                $out .= '<li class="' . esc_attr( $taxonomy ) . '">';
    
                ## ATTRIBUTE IMAGE ##
                // For a child theme use get_stylesheet_directory_uri() instead.
                $out .= '<img class="attribute-image" src="'.get_template_directory_uri().'/images/attributes/'.$name.'.jpg" alt="Attribute '.$label.'"/> ';
                $out .= '<span class="attribute-values">';
    
                $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );
    
                foreach ( $terms as $term_name )
                    $term_names['name'] = $term_name;
    
                $out .= implode(', ', $term_names);
                $out .= '</span></li>';
    
            } else {
                $value_string = implode( ', ', $attribute->get_options() );
                $out .= '<li class="' . sanitize_title($taxonomy) . ' ' . sanitize_title( $value_string ) . '">';
                $out .= '<span class="attribute-label">' . $taxonomy . ': </span> ';
                $out .= '<span class="attribute-value">' . esc_html( $value_string ) . '</span></li>';
            }
        }
        $out .= '</ul>';
    
        echo $out;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This code is tested and work.

    0 讨论(0)
提交回复
热议问题