【问题标题】:Woocommerce hide multiple categories from shop using arraysWoocommerce 使用数组从商店中隐藏多个类别
【发布时间】:2014-04-26 16:25:39
【问题描述】:

下面带有 19 的代码可以正常工作,因为它是从 woocommerce 网站复制的。不幸的是,我无法将 19 替换为 $producten 以便 $producten 包含一个包含所有类别的数组。

$producten 包含多个需要隐藏并在数据库中定义的类别。我想做什么以及如何做有可能吗?

$userid = get_current_user_id();

$result = mysql_query("SELECT productID FROM bestellingen WHERE userID='$userid'");
$producten = array();
while($row = mysql_fetch_array($result)){
array_push($producten, $row['productID']);
}

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( 19 ), // de categorienummers
        'operator' => 'NOT IN'
    )));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

提前致谢!

【问题讨论】:

    标签: php arrays woocommerce categories webshop


    【解决方案1】:

    使用如下会话解决:

    $result = mysql_query("SELECT productID FROM bestellingen WHERE userID='$userid'");
    $producten = array();
    while($row = mysql_fetch_array($result)){
    array_push($producten, $row['productID']);
    }
    $_SESSION['PRODUCTEN'] = $producten;
    
    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' => $_SESSION['PRODUCTEN'], // Don't display products in the knives category on the shop page
            'operator' => 'NOT IN'
        )));
    
    
    }
    
        unset($_SESSION['PRODUCTEN']);
    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 2012-11-02
      • 2019-07-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多