[英]WooCommerce exclude certain product attributes from shop page
我一直在為這個而絞盡腦汁。 目前,要在商店頁面上顯示所有自定義產品屬性(不要與產品頁面混淆),我正在使用:
function show_attr() {
global $product;
echo '<div class="attributes">';
$product->list_attributes();
echo'</div>'
}
這很好用,並顯示所有產品屬性,但我只想包括某些屬性。 我也試過聽從這個人的建議:
<?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]
條件。 有沒有人知道如何刪除商店頁面上特定屬性的內容? 我完全不知所措。 感謝您的任何幫助。
正如我在評論中提到的,您可以通過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 的名稱,例如:“大小”。
使用WooCommerce條件標簽,你可以專門針對只在店鋪頁面上的屬性。
以下是兩個示例過濾器,第一個用於排除特定屬性:
// 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' );
由於woocommerce_get_product_attributes
已被棄用,因此在 2018 年 3 月 29 日更新了woocommerce_product_get_attributes
。
試試這個!
<?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.