WooCommerce从商店页面排除某些产品属性 [英] WooCommerce exclude certain product attributes from shop page

查看:178
本文介绍了WooCommerce从商店页面排除某些产品属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为此绞尽脑汁.当前,为了在商店页面上显示所有自定义产品属性(不要与产品页面混淆),我正在使用:

I've been wracking my brain on this one. Currently, to display all custom product attributes on the shop page (not to be confused with the product page) I'm using:

function show_attr() {
   global $product;
   echo '<div class="attributes">';
   $product->list_attributes();
   echo'</div>'
}

这很好用,并显示所有产品属性,但我只想包含某些属性.我还尝试了按照此人的建议:

This works just fine, and displays all product attributes, but I only want to include certain ones. I have also tried following this person's advice:

<?php foreach ( $attributes as $attribute ) :
    if ( empty( $attribute['is_visible'] ) || 'CSC Credit' == $attribute['name'] || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) ) {
        continue;
    } else {
        $has_row = true;
    }
?>

因此,不幸的是,该方法也不起作用.我能够删除所需的属性,但是它在每个页面上都将其删除,因此我想从商店页面中仅 将其排除.

So that unfortunately did not work either. I was able to remove the desired attribute, but it removes it on EVERY page, and I want to exclude it only from the shop page.

我看到$ attribute变量具有此[is_visible]条件.有人对我如何删除商店页面上的特定属性有任何想法吗?我完全不知所措.感谢您提供的所有帮助.

I see that the $attribute variable has this [is_visible] condition. Does anyone have any ideas of how I might remove that for specific attributes on the shop page? I'm at a total loss. Thanks for any and all help.

推荐答案

如我的评论中所述,您可以通过var_dump()可能会显示以下$attributes.

As mentioned in my comment you can control the attributes for any give product via the woocommerce_get_product_attributes filter. The $attributes passing through this filter are in an associative array of arrays. with the attribute's "slug" as the array key. As an example a var_dump() might reveal the following $attributes.

array (size=1)
  'pa_color' => 
    array (size=6)
      'name' => string 'pa_color' (length=8)
      'value' => string '' (length=0)
      'position' => string '0' (length=1)
      'is_visible' => int 0
      'is_variation' => int 1
      'is_taxonomy' => int 1

如果该属性是一个分类法,则该子句将以"pa_"开头,我一直认为该属性代表产品属性.不是分类法的属性将仅具有该段的名称,例如:"size".

If the attribute is a taxonomy, the slug will be prefaced with "pa_" which I've always assumed stood for product attribute. An attribute that is not a taxonomy will just have it's name for the slug, ex: "size".

使用 WooCommerce条件标签,您可以专门针对属性 在商店页面上.

Using WooCommerce Conditional tags you can specifically target the attributes only on the shop page.

以下是两个示例过滤器,第一个用于排除特定属性:

Here are two example filters, the first is for excluding a specific attribute:

// Exclude a certain product attribute on the shop page
function so_39753734_remove_attributes( $attributes ) {

    if( is_shop() ){
        if( isset( $attributes['pa_color'] ) ){
            unset( $attributes['pa_color'] );
        }
    }

    return $attributes;
}
add_filter( 'woocommerce_product_get_attributes', 'so_39753734_remove_attributes' );

后者用于根据您希望包含的属性建立属性的自定义列表.

And the latter is for building up a custom list of attributes based on attributes you do wish to include.

// Include only a certain product attribute on the shop page
function so_39753734_filter_attributes( $attributes ) {

    if( is_shop() ){
        $new_attributes = array();

        if( isset( $attributes['pa_color'] ) ){
            $new_attributes['pa_color'] = $attributes['pa_color'] ;
        }

        $attributes = $new_attributes;

    }

    return $attributes;
}
add_filter( 'woocommerce_product_get_attributes', 'so_39753734_filter_attributes' );

已更新,因为已弃用woocommerce_get_product_attributes,因此使用woocommerce_product_get_attributes于2018年3月29日发布.

Updated Mar 29, 2018 with woocommerce_product_get_attributes since woocommerce_get_product_attributes is deprecated.

这篇关于WooCommerce从商店页面排除某些产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆