Php WooCommerce从商店页面中排除某些产品属性
我一直在为这件事绞尽脑汁。目前,要在商店页面上显示所有自定义产品属性(不要与产品页面混淆),我使用:Php WooCommerce从商店页面中排除某些产品属性,php,wordpress,woocommerce,attributes,storefront,Php,Wordpress,Woocommerce,Attributes,Storefront,我一直在为这件事绞尽脑汁。目前,要在商店页面上显示所有自定义产品属性(不要与产品页面混淆),我使用: function show_attr() { global $product; echo '<div class="attributes">'; $product->list_attributes(); echo'</div>' } 函数show_attr(){ 全球$产品; 回声'; $product->list_属性(); 回声“ }
function show_attr() {
global $product;
echo '<div class="attributes">';
$product->list_attributes();
echo'</div>'
}
函数show_attr(){
全球$产品;
回声';
$product->list_属性();
回声“
}
这工作得很好,并显示所有产品属性,但我只想包括某些属性。我也尝试过以下建议:
不幸的是,这也不起作用。我可以删除所需的属性,但它会在每个页面上删除它,我只想将它从商店页面中排除
我看到$attribute变量有这个[可见]
条件。有人知道我如何删除商店页面上的特定属性吗?我完全不知所措。谢谢您的帮助。试试这个
<?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;
}
}
?>
正如我在评论中提到的,您可以通过过滤器控制任何给定产品的属性。通过此筛选器的
$attributes
位于数组的关联数组中。使用属性的“slug”作为数组键。例如,var\u 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”
使用,您可以仅在“商店”页面上专门针对属性
以下是两个示例过滤器,第一个用于排除特定属性:
// 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\u product\u get\u属性
,因为woocommerce\u get\u product\u属性
已被弃用。+1!我刚下班,但明天早上会尽快试试这个。真不敢相信我居然没想到这个。我是Wordpress的新手,所以所有的事情都一下子就来了。我会接受明天的答案,一旦我把这个拿起来,摆起来。感谢您的及时回复!:)很高兴它有帮助!我打算回答您需要使用is\u shop()
。我想你也可以过滤woocommerce\u get\u product\u attributes
来代替编写自己的循环。啊,太棒了!通过这些例子,这就更有意义了。感谢您花时间进一步解释。我真的很感谢你的帮助。最近有个坏消息:woocommerce\u get\u product\u attributes不受欢迎,取而代之的是woocommerce\u product\u get\u 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' );