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

时间:2019-04-22 22:41:25

标签: php wordpress woocommerce custom-taxonomy taxonomy-terms

在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 '';
        }
    }
}

1 个答案:

答案 0 :(得分:1)

产品属性在WooCommerce中比其他分类法更加具体和复杂。每个产品属性都是一个分类法,具有自己的术语,可用于可变产品的变体...

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

这里我们使用Category and Taxonomy Image及其专用功能get_wp_term_image()

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

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文件中。经过测试,可以正常工作。

相关问题