简体   繁体   English

在 WooCommerce 商店页面中对没有缩略图的产品进行排序

[英]Sort products without thumbnail in WooCommerce shop page

I would like to create a function that instead of hiding the products without thumbnails, would be ordered in such a way that the products with the highlighted image appear first on the shop page while those without the highlighted image appear at the end.我想创建一个 function,而不是隐藏没有缩略图的产品,而是以这样一种方式订购,即带有突出显示图像的产品首先出现在商店页面上,而那些没有突出显示图像的产品出现在最后。 I tried to modify an already existing function :我试图修改一个已经存在的 function

    function woocommerce_product_query( $q ) {
        $q->set( 'meta_key', '_thumbnail_id' );
        $q->set('orderby', 'meta_value_num');
        $q->set('order', 'DESC');
    }
    add_action( 'woocommerce_product_query', 'woocommerce_product_query' );

I tried adding orderby to this already existing function too but nothing happens, keep hiding the products from me.我也尝试将orderby添加到这个已经存在的 function 但没有任何反应,继续对我隐藏产品。

In this photo the function only hides the products without thumbnail.在这张照片中,function 只隐藏了没有缩略图的产品。 I don't know how to solve...不知道怎么解决...店铺页面

I have implemented the code but the sorting is done alphabetically.我已经实现了代码,但排序是按字母顺序完成的。 While I would like to sort by product entry date:虽然我想按产品进入日期排序:

function my_shop_custom_products_query( $q ) {
        $q->get( 'meta_key', '_thumbnail_id' );
        $q->set('orderby', 'meta_value');
        $q->set('order', 'DESC');
        $q->set( 'meta_query', array(  'relation' => 'OR', array(
            
           'orderby' => 'meta_value',
            'order' => 'DESC',
            'key' => '_thumbnail_id',
           'compare' => 'NOT EXISTS',
     //  'value' => '0'       
  
        ),
  array(
     'key' => '_thumbnail_id',
           'compare' => '>=',
       'value' => '0',       
  'orderby' => 'meta_value',
       'order' => 'DESC'
  )),
); 
}

add_action( 'woocommerce_product_query', 'my_shop_custom_products_query' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM