简体   繁体   中英

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. 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. 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

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".

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

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

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;
        }
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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