最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Sort products without thumbnail in WooCommerce shop page

programmeradmin1浏览0评论

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. I tried to modify an already existing 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.

In this photo the function only hides the products without thumbnail. I don't know how to solve ...

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. I tried to modify an already existing 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.

In this photo the function only hides the products without thumbnail. I don't know how to solve ...

Share Improve this question edited Feb 4, 2021 at 14:25 Naycom asked Feb 4, 2021 at 8:34 NaycomNaycom 231 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I would try to rename the custom function and add meta_query too! So it'd be like this:


function my_shop_custom_products_query( $q ) {
        $q->set( 'meta_key', '_thumbnail_id' );
        $q->set('orderby', 'meta_value_num');
        $q->set('order', 'DESC');
        $q->set( 'meta_query', array( array(
           'key' => '_thumbnail_id',
           'compare' => '>=',
           'value' => '0'       
        )));
}

add_action( 'woocommerce_product_query', 'my_shop_custom_products_query' );

If for some reason it didn't work, then try to replace meta_value_num with meta_value and see if that'd work!

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' );
发布评论

评论列表(0)

  1. 暂无评论