显示WooCommerce产品属性的自定义分类术语图像

问题描述 投票:1回答:1

在WooCommerce我使用Category and Taxonomy Image插件,允许我将图像添加到产品属性术语。

现在我正在尝试显示特定产品属性,商店页面上每个产品的相关术语图像。

Category and Taxonomy Image插件的作者使用以下代码显示术语图像:

  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

我使用下面的代码在商店页面上显示“颜色”产品属性名称:

add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
    global $product;

    $spec_val = $product->get_attribute('spec');

    if(!empty($spec_val)) { 
        echo'<span class="view_attr"> SPECIFICATION: '  . $spec_val  . '</span>';
    }
}

如何显示术语图像?

也许这就是解决方案:

add_action('woocommerce_after_shop_loop_item','woo_new_product_tab_content');
function woo_new_product_tab_content() {
    global $product;

    $ingredients = $product->get_attributes( 'color' );

    foreach( $ingredients as $attr_name => $attr ){
        foreach( $attr->get_terms() as $term ){
            if ( wc_attribute_label( $attr_name ) == "Color" ) {
                echo $term->name ;
                $meta_image = get_wp_term_image($term->term_id);
                echo '<img src="'.$meta_image.'"/>';
            } 
            else echo '';
        }
    }
}
php wordpress woocommerce custom-taxonomy taxonomy-terms
1个回答
1
投票

与其他分类法相比,WooCommerce中的产品属性非常具体且复杂。每个产品属性都是一个分类,有自己的术语,可以用于变量产品的变化......

插件Taxonomy ImagesCategory and Taxonomy Image允许在所有WooCommerce自定义分类标准术语上将图像作为产品标签和产品属性(产品类别默认具有此功能)。

在这里,我们使用Category and Taxonomy Image及其专用函数get_wp_term_image()

在下面的代码中,您可以启用数组中定义的多个产品属性。如果选项“启用存档?”对于product属性,您可以选择使用术语链接。

add_action('woocommerce_after_shop_loop_item','woo_new_product_tab_content');
function woo_new_product_tab_content() {
    global $product;

    // Define your product attribute labels in the array (label names)
    $defined_pa_labels = array( 'Color' );

    // Loop through WC_Product_Attribute Objects
    foreach( $product->get_attributes() as $taxonomy => $product_attribute ) {
        $taxonomy_name  = $product_attribute->get_name();       // Slug
        $taxonomy_label = wc_attribute_label( $taxonomy_name ); // Name (label name)

        if( in_array( $taxonomy_label, $defined_pa_labels ) ) {

            // Loop through product attribute WP_Term Objects
            foreach( $product_attribute->get_terms() as $term ) {
                $term_name = $term->name;  // Term name
                $term_slug = $term->slug;  // Term slug
                $term_id = $term->term_id; // Term ID

                // Get product attribute term image
                if( $image_url = get_wp_term_image( $term_id ) ) {

                    // Get product attribute term link (optional) 
                    // if the product attribute is enabled on archives)
                    $term_url  = get_term_link( $term, $taxonomy );

                    // Output
                    echo '<span style="text-align:center"><img src="'.esc_url( $image_url).'"/>'.$term->name.'</span>';
                }
            }
        }
    }
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.