首页 > 解决方案 > woocommerce_product_query 钩子与 Elementor 产品选项卡

问题描述

下面的钩子

function custom_pre_get_posts_query( $q ) {


$tempAddress = $_COOKIE["tempAddress"];
$categories_to_exclude = array();

if (strpos($tempAddress, 'New York') !== FALSE)
    {
        //categories_to_exclude;
        //$q->set(); 
    }

else
    {
        $args = apply_filters('woocommerce_related_products_args', 
        array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'meta_query'            => array(
                 array(
                    'key'           => '_visibility',
                    'value'         => array('catalog', 'visible'),
                    'compare'       => 'IN'
                ),
            ),
            'tax_query'            => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'term_id', // Or 'name' or 'term_id'
                    'terms'    => array(51,52,53),
                    'operator' => 'NOT IN', // Excluded
                )
            )
        ) 
    );
    
    $q->set( 'tax_query', $args ); 
}}

add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );

帮助我们删除 WooCommerce 中某些类别的产品。但是,当主题在某个页面(例如主页)上有 Elementor 产品选项卡时,不会应用挂钩。

任何想法?

非常感谢!

标签: woocommercehook-woocommerce

解决方案


推荐阅读