[英]Show product titles to specific delimiter on WooCommerce category archive pages
[英]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.