Php WooCommerce 3.x-隐藏产品类别
我想知道是否有人知道如何在我的网站上隐藏一个特定的产品类别。指我的Wordpress WooCommerce网站上的“店铺”、“相关产品”和“搜索” 对于“购物”页面我已经完成了(并且正在工作)以下内容:Php WooCommerce 3.x-隐藏产品类别,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想知道是否有人知道如何在我的网站上隐藏一个特定的产品类别。指我的Wordpress WooCommerce网站上的“店铺”、“相关产品”和“搜索” 对于“购物”页面我已经完成了(并且正在工作)以下内容: function custom_pre_get_posts_q( $q ) { $tax_query = (array) $q->get( 'tax_query' ); $tax_query[] = array( 'ta
function custom_pre_get_posts_q( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'carton' ), // Don't display products in the composite category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_q' );
` <?php foreach ( $related_products as $related_product ) : ?>
<?php
$post_object = get_post( $related_product->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>`
对于搜索我尝试了以下方法,但无效:
function exclude_category_from_search($query) {
if ($query->is_search) {
$cat_id = get_cat_ID('carton');
$query->set('cat', '-'.$cat_id);
}
return $query;
}
添加过滤器(“pre_get_posts”,“exclude_category_from_search”)
最后,对于与相关的产品我尝试了以下自WC 3.x以来似乎不受欢迎的方法:
function wc_remove_related_products( $args )
{
if (is_product() && has_term( 'carton', 'product_cat'))
{
return array();
}
return $args;
}
add_filter('woocommerce_related_products_args','wc_remove_related_products', 10);
我的儿童主题中也包含以下内容:
function custom_pre_get_posts_q( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'carton' ), // Don't display products in the composite category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_q' );
` <?php foreach ( $related_products as $related_product ) : ?>
<?php
$post_object = get_post( $related_product->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object );
wc_get_template_part( 'content', 'product' ); ?>
<?php endforeach; ?>`
有人知道如何使用新版本的WoomCommerce做到这一点吗?
我已经做了很多研究,但从这个新版本开始,所有的答案看起来都是不推荐的
PS:我需要保留这个类别,因为我正在使用它来创建一些复合产品,所以只隐藏这些产品,而不删除它们
干杯将woocommerce类别从搜索中排除 在function.php文件中添加以下函数
function sm_pre_get_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
$query->set( 'post_type', array( 'product' ) );
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'carton', //slug name of category
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'sm_pre_get_posts' );
从搜索中排除电子商务类别 在function.php文件中添加以下函数
function sm_pre_get_posts( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
$query->set( 'post_type', array( 'product' ) );
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'carton', //slug name of category
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'sm_pre_get_posts' );
要从搜索和商店页面中排除您的类别(“Carton”),请在my child主题的my function.php文件中添加以下内容:
/* hide CARTON category SEARCH
=============================*/
function sm_pre_get_posts( $query ) {
if ( $query->is_search() ) {
$query->set( 'post_type', array( 'product' ) );
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'carton', //slug name of category
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'sm_pre_get_posts' );
/* hide CARTON category for SHOP pages
====================================*/
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'carton' ), // Don't display products in the carton category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
关于“相关产品”,我不得不对其进行调整,但最终它改善了与woocommerce相关的产品。我基本上是按照品牌、类别和季节的顺序对我的相关产品进行分类。因为“Carton”是一个类别,所以它不会检索其中任何一个:
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$myTerm = $term->slug;
if($myTerm == 'mens' || $myTerm == 'ladies'){
$mainCat = $myTerm;
}else if($myTerm == 'accessories'){
$mainCat = $myTerm;
}else if($myTerm == 'children'){
$mainCat = $myTerm;
}else if($myTerm == 'summer' || $myTerm == 'winter'){
$seasonCat = $myTerm;
}
}
$terms = get_the_terms( get_the_ID(), 'pa_brand' );
foreach ($terms as $term) {
$theBrand = $term->slug;
}
?>
<div class="product-carousel related-products spb_content_element">
<div class="title-wrap clearfix">
<h3 class="spb-heading"><span><?php echo esc_attr($related_heading); ?></span></h3>
<div class="carousel-arrows"><a href="#" class="carousel-prev"><i class="sf-icon-chevron-prev"></i></a><a href="#" class="carousel-next"><i class="sf-icon-chevron-next"></i></a></div>
</div>
<div class="related products carousel-items <?php echo esc_attr($gutter_class); ?> product-type-<?php echo esc_attr($related_product_display_type); ?>" id="carousel-<?php echo esc_attr($sf_carouselID); ?>" data-columns="<?php echo esc_attr($woocommerce_loop['columns']); ?>">
<?php
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $theBrand
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $mainCat
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $seasonCat
)
),
'post_type' => 'product',
'orderby' => 'rand',
'order' => 'desc',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata(); ?>
</div>
</div>
global$post;
$terms=get_the_terms($post->ID,'product_cat');
foreach($terms作为$term){
$myTerm=$term->slug;
如果($myTerm==‘男士’| |$myTerm==‘女士’){
$mainCat=$myTerm;
}else if($myTerm==‘附件’){
$mainCat=$myTerm;
}else if($myTerm=='children'){
$mainCat=$myTerm;
}else if($myTerm=='summer'.$myTerm=='winter'){
$SECTIONCAT=$myTerm;
}
}
$terms=get_the_terms(get_the_ID(),'pa_brand');
foreach($terms作为$term){
$theBrand=$term->slug;
}
?>
要从搜索和商店页面中排除您的类别(“Carton”),请在my child主题的my function.php文件中添加以下内容:
/* hide CARTON category SEARCH
=============================*/
function sm_pre_get_posts( $query ) {
if ( $query->is_search() ) {
$query->set( 'post_type', array( 'product' ) );
$tax_query = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'carton', //slug name of category
'operator' => 'NOT IN',
),
);
$query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'sm_pre_get_posts' );
/* hide CARTON category for SHOP pages
====================================*/
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'carton' ), // Don't display products in the carton category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
关于“相关产品”,我不得不对其进行调整,但最终它改善了与woocommerce相关的产品。我基本上是按照品牌、类别和季节的顺序对我的相关产品进行分类。因为“Carton”是一个类别,所以它不会检索其中任何一个:
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$myTerm = $term->slug;
if($myTerm == 'mens' || $myTerm == 'ladies'){
$mainCat = $myTerm;
}else if($myTerm == 'accessories'){
$mainCat = $myTerm;
}else if($myTerm == 'children'){
$mainCat = $myTerm;
}else if($myTerm == 'summer' || $myTerm == 'winter'){
$seasonCat = $myTerm;
}
}
$terms = get_the_terms( get_the_ID(), 'pa_brand' );
foreach ($terms as $term) {
$theBrand = $term->slug;
}
?>
<div class="product-carousel related-products spb_content_element">
<div class="title-wrap clearfix">
<h3 class="spb-heading"><span><?php echo esc_attr($related_heading); ?></span></h3>
<div class="carousel-arrows"><a href="#" class="carousel-prev"><i class="sf-icon-chevron-prev"></i></a><a href="#" class="carousel-next"><i class="sf-icon-chevron-next"></i></a></div>
</div>
<div class="related products carousel-items <?php echo esc_attr($gutter_class); ?> product-type-<?php echo esc_attr($related_product_display_type); ?>" id="carousel-<?php echo esc_attr($sf_carouselID); ?>" data-columns="<?php echo esc_attr($woocommerce_loop['columns']); ?>">
<?php
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $theBrand
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $mainCat
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $seasonCat
)
),
'post_type' => 'product',
'orderby' => 'rand',
'order' => 'desc',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata(); ?>
</div>
</div>
global$post;
$terms=get_the_terms($post->ID,'product_cat');
foreach($terms作为$term){
$myTerm=$term->slug;
如果($myTerm==‘男士’| |$myTerm==‘女士’){
$mainCat=$myTerm;
}else if($myTerm==‘附件’){
$mainCat=$myTerm;
}else if($myTerm=='children'){
$mainCat=$myTerm;
}else if($myTerm=='summer'.$myTerm=='winter'){
$SECTIONCAT=$myTerm;
}
}
$terms=get_the_terms(get_the_ID(),'pa_brand');
foreach($terms作为$term){
$theBrand=$term->slug;
}
?>
如果要完全禁用产品类别(和产品标签),可以在functions.php文件中使用以下过滤器:
add_filter('get_the_terms', function ($terms, $id, $taxonomy) {
if ($taxonomy === 'product_cat' || $taxonomy === 'product_tag') {
return [];
}
return $terms;
}, 99, 3);
如果要完全禁用产品类别(和产品标签),可以在functions.php文件中使用以下过滤器:
add_filter('get_the_terms', function ($terms, $id, $taxonomy) {
if ($taxonomy === 'product_cat' || $taxonomy === 'product_tag') {
return [];
}
return $terms;
}, 99, 3);
嗨,你有答案吗?我看起来也一样。嘿@mikesneider我已经发布了答案。是的,我终于明白了;)如果有任何问题,请随意提问并竖起大拇指看看是否适合您!嗨,你有答案吗?我看起来也一样。嘿@mikesneider我已经发布了答案。是的,我终于明白了;)如果有任何问题,请随意提问并竖起大拇指看看是否适合您!