简体   繁体   English

WooCommerce 从商店页面中排除某些产品属性

[英]WooCommerce exclude certain product attributes from shop page

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.我能卸下所需的属性,但它删除它在每一页上,我想只能从店铺页面排除。

I see that the $attribute variable has this [is_visible] condition.我看到 $attribute 变量有这个[is_visible]条件。 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.感谢您的任何帮助。

As mentioned in my comment you can control the attributes for any give product via the woocommerce_get_product_attributes filter.正如我在评论中提到的,您可以通过woocommerce_get_product_attributes过滤器控制任何给定产品的属性。 The $attributes passing through this filter are in an associative array of arrays.通过此过滤器的$attributes位于数组的关联数组中。 with the attribute's "slug" as the array key.使用属性的“slug”作为数组键。 As an example a var_dump() might reveal the following $attributes .例如, var_dump()可能会显示以下$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

If the attribute is a taxonomy, the slug will be prefaced with "pa_" which I've always assumed stood for product attribute.如果属性是分类法,则 slug 将以“pa_”开头,我一直认为它代表产品属性。 An attribute that is not a taxonomy will just have it's name for the slug, ex: "size".不是分类法的属性将仅具有 slug 的名称,例如:“大小”。

Using WooCommerce Conditional tags you can specifically target the attributes only on the shop page.使用WooCommerce条件标签,你可以专门针对在店铺页面上的属性。

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' );

Updated Mar 29, 2018 with woocommerce_product_get_attributes since woocommerce_get_product_attributes is deprecated.由于woocommerce_get_product_attributes已被弃用,因此在 2018 年 3 月 29 日更新woocommerce_product_get_attributes

Try this!试试这个!

<?php
if (is_page('shop')) {
    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;
        }
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM