2016-05-17 10 views
0

WooCommerceではオプション「Visibility in在庫切れ」をチェックしましたが、そのオプションがチェックされていても、一部の商品カテゴリが表示されている必要があります。カテゴリの在庫切れ商品を表示する

一方、メインストアではそのカテゴリは表示されません。私はこのコードを使用しました:

add_action('pre_get_posts', 'custom_pre_get_posts_query'); 
function custom_pre_get_posts_query($q) { 

    if (! $q->is_main_query()) return; 
    if (! $q->is_post_type_archive()) return; 
    if (! is_admin() && is_shop()) { 

    $q->set('tax_query', array(array(
     'taxonomy' => 'product_cat', 
     'field' => 'slug', 
     'terms' => array('MYCATEGORY'), // Don't show this category. 
     'operator' => 'NOT IN' 
))); 
    } 

    remove_action('pre_get_posts', 'custom_pre_get_posts_query'); 
} 

これは可能でしょうか?

ありがとうございました!

答えて

1

これはテストされていない、ただ速い推測ですが、あなたのようなもので始まる試みることができる:

global $product; 

$terms = wp_get_post_terms($post->ID, 'product_cat'); 
foreach ($terms as $term) $categories[] = $term->slug; 

$category_to_show = ‘MYCATEGORY’; 
if($categories) { 
    if(in_array($category_to_show, $categories) && !$product->is_in_stock()) { 
     apply_filters('woocommerce_product_is_visible', true, $product->id); 
    } 
} 
+0

これはループで使用するのですか? –

+0

post-> IDを削除することで、関数ファイルの関数でこれを使用できるはずです。 次に、$ terms = wp_get_post_terms($ taxonomy = 'product_cat')を使用できます。 もしcontent-product.phpのようなループでそれを使ってみると、そのファイルに既に含まれているのでグローバル製品を削除することができます – assemblr

+0

これは私に 'Warning:in_array()この行は次のようになります: '' if_in_array($ category_to_show、$ categories)&&!$ product-> is_in_stock()) ''どこがエラーですか?私はこれを見つけることができません... –

関連する問題