2016-09-28 149 views
0

我一直在对这个问题大惊小怪。目前,以显示所有定制产品的商店页面上的属性(不与产品页相混淆),我使用的是:WooCommerce从商店页面中排除某些产品属性

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]条件。有没有人有任何想法,我可能会删除该商店页面上的特定属性?我处于全面亏损状态。感谢任何和所有的帮助。

回答

1

正如我在评论中提及您可以通过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 

如果属性的分类法中,嵌入将与“PA_”我一直认为代表着产品的属性来开头。一个不是分类的属性只是它的名字,例如:“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_get_product_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_get_product_attributes', 'so_39753734_filter_attributes'); 
+0

啊精彩!这使得这些例子更有意义。感谢您花时间进一步解释。我非常感谢帮助。 – Kedmasterk

+0

不客气。 – helgatheviking

0

试试这个!

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

+1!我刚刚离开工作,但会在早上尽快尝试。我不敢相信我没有想到这一点。我是WordPress的新手,所以一切都马上就来到我身上。明天我会接受这个答案,然后摆动。感谢您的及时回应! :) – Kedmasterk

+0

很高兴帮助! –

+0

我打算回答你需要使用'is_shop()'。我想你也可以过滤'woocommerce_get_product_attributes'来代替编写自己的循环。 – helgatheviking

相关问题