dqxhit3376 2014-04-26 16:25
浏览 36
已采纳

Woocommerce使用数组隐藏商店中的多个类别

The code below with the 19 is working correctly as it is copied from the woocommerce website. Unfortunate I can't get it working to replace the 19 with $producten so that $producten contains an array with all categories.

$producten contains multiple categories that need to be hidden and are defined in the database. Is it possible what I want to do and how?

$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' );

}

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongzaijiao4863 2014-04-27 14:10
    关注

    It is solved using a session like:

    $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' );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大