簡體   English   中英

僅顯示特定類別的WooCommerce產品頁面

[英]Show WooCommerce product pages for only specific category

我有一個擁有網站的客戶,客戶可以在其中訂購產品,銷售人員可以訂購銷售樣品。 我希望能夠顯示單個類別的單個產品頁面,但隱藏所有其他產品的單個產品頁面。

我過去曾使用此代碼來隱藏所有產品頁面,但我需要按類別進行過濾。 有什么提示嗎?

//Remove all single product pages
function hide_product_page($args){
    $args["publicly_queryable"]=false;
    $args["public"]=false;
    return $args;
}
add_filter( 'woocommerce_register_post_type_product','hide_product_page',12,1); 

編輯:這是背景:銷售示例產品是免費的,對於那些可以通過密碼訪問的產品,我們只有一個訂單頁面(不是單個產品頁面。)即使阻止顯示類別頁面,單個產品頁面仍然存在。 一些隨機的人找到了這些頁面,並下了“免費”產品的訂單。 我需要防止這種情況的發生,因此僅“隱藏”單個產品頁面是不夠的,我必須確保它們不存在。 但是,我們仍然需要向公眾出售的常規產品的產品頁面。

編輯:我最終在我的functions.php中使用了它:

function custom_shop_page_redirect(){
    if (class_exists('WooCommerce')){
        if(is_product()){
            global $post;
            $price = get_post_meta( $post->ID, '_regular_price', true);

            if($price == 0) {
                wp_redirect(home_url());
                exit();
            }
        }
    } 
    return;
} 
add_action('template_redirect','custom_shop_page_redirect');

它不檢查類別,而是禁用價格為零的商品的產品頁面。 這完成了我所需要的。

假設您有一個類別,其中包含如pizza 您可以像這樣在WooCommerce中隱藏此類別中的產品, 阻止該類別顯示在商店頁面中(如果啟用了在產品之前顯示類別)。 (將此內容包含在您的子主題或主題的funtion.php 。)

$CATEGORY_SLUGS = array('pizza'); // Can contain multiple slugs.

// Prevents category from showing up.
function get_terms_exclude_by_category_slug($terms, $taxonomies, $args) {
  global $CATEGORY_SLUGS;
  if (in_array('product_cat', $taxonomies)
      && !is_admin()
      && is_shop()) {
    $new_terms = array();
    foreach ($terms as $key => $term) {
      if (!in_array($term->slug, $CATEGORY_SLUGS)) {
        $new_terms[] = $term;
      }
    }
    $terms = $new_terms;
  }
  return $terms;
}

// Prevents products in certain categories from showing up.
function exclude_products_by_category_slug($q) {
  global $CATEGORY_SLUGS;
  $tax_query = (array) $q->get('tax_query');
  $tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => $CATEGORY_SLUGS,
    'operator' => 'NOT IN'
  );
  $q->set('tax_query', $tax_query);
}

// The last two parameters are needed only because the callback
// receives 3 arguments instead of the default 1.
add_filter('get_terms', 'get_terms_exclude_by_category_slug', 10, 3);
add_action('woocommerce_product_query', 'exclude_products_by_category_slug');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM