WooCommerce 从商店页面中排除某些产品属性
WooCommerce exclude certain product attributes from shop page
我一直在为这个问题绞尽脑汁。目前,要在商店页面上显示所有自定义产品属性(不要与产品页面混淆),我正在使用:
function show_attr() {
global $product;
echo '<div class="attributes">';
$product->list_attributes();
echo'</div>'
}
这很好用,显示了所有产品属性,但我只想包括某些属性。我也尝试遵循 this person's 建议:
<?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;
}
?>
不幸的是,这也没有用。我能够删除所需的属性,但它会在每个页面上删除它,我想从商店页面中仅 排除它。
我看到 $attribute 变量有这个 [is_visible]
条件。有没有人知道我可以如何删除商店页面上特定属性的那个?我完全不知所措了。感谢您提供的所有帮助。
试试这个!
<?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;
}
}
?>
如我的评论所述,您可以通过 woocommerce_get_product_attributes
过滤器控制任何给定产品的属性。通过此过滤器的 $attributes
在数组的关联数组中。使用属性的 "slug" 作为数组键。例如 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
如果属性是分类法,则 slug 将以 "pa_" 开头,我一直认为它代表产品属性。不是分类法的属性将只具有它的 slug 名称,例如:"size"。
使用 WooCommerce Conditional tags,您可以专门针对商店页面上的属性 仅。
这里有两个示例过滤器,第一个用于排除特定属性:
// 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' );
后者用于根据您希望包含的属性构建自定义属性列表。
// 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' );
已于 2018 年 3 月 29 日更新 woocommerce_product_get_attributes
,因为 woocommerce_get_product_attributes
已弃用。
我一直在为这个问题绞尽脑汁。目前,要在商店页面上显示所有自定义产品属性(不要与产品页面混淆),我正在使用:
function show_attr() {
global $product;
echo '<div class="attributes">';
$product->list_attributes();
echo'</div>'
}
这很好用,显示了所有产品属性,但我只想包括某些属性。我也尝试遵循 this person's 建议:
<?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;
}
?>
不幸的是,这也没有用。我能够删除所需的属性,但它会在每个页面上删除它,我想从商店页面中仅 排除它。
我看到 $attribute 变量有这个 [is_visible]
条件。有没有人知道我可以如何删除商店页面上特定属性的那个?我完全不知所措了。感谢您提供的所有帮助。
试试这个!
<?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;
}
}
?>
如我的评论所述,您可以通过 woocommerce_get_product_attributes
过滤器控制任何给定产品的属性。通过此过滤器的 $attributes
在数组的关联数组中。使用属性的 "slug" 作为数组键。例如 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
如果属性是分类法,则 slug 将以 "pa_" 开头,我一直认为它代表产品属性。不是分类法的属性将只具有它的 slug 名称,例如:"size"。
使用 WooCommerce Conditional tags,您可以专门针对商店页面上的属性 仅。
这里有两个示例过滤器,第一个用于排除特定属性:
// 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' );
后者用于根据您希望包含的属性构建自定义属性列表。
// 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' );
已于 2018 年 3 月 29 日更新 woocommerce_product_get_attributes
,因为 woocommerce_get_product_attributes
已弃用。