• Resolved Arthur Virabyan

    (@virabyan)


    Hello,

    I’m having an issue with sorting products in WooCommerce. I want to configure the product archive page so that products from category ID 221 are displayed first, followed by all other products.

    I’ve tried several methods but none have produced the desired result. Here’s what I’ve attempted:

    1. Added custom SQL queries to sort products.
    2. Used filters like posts_clauses, posts_orderby, and pre_get_posts, but so far all my attempts either result in no products from the desired category being displayed or a mix of products from all categories.

    Has anyone faced a similar issue and can provide a working solution? I would appreciate any advice or code examples that could help achieve the desired product order.

    Thank you in advance for your help!

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @virabyan

    From your explanation, it seems like you’ve tried many ways already. However, have you tried using a plugin like Rearrange Woocommerce Products? This plugin provides options to customize the order of products based on categories.

    Alternatively, you can use a custom code. Though writing or providing custom code is not within the scope of our support policy, here’s a basic example:

    add_action( 'woocommerce_product_query', 'custom_product_query' );
    function custom_product_query( $q ){
    if( ! is_admin() && is_shop() ) {
    $q->set( 'tax_query', array(array(
    'taxonomy' => 'product_cat',
    'field' => 'term_id',
    'terms' => array( 221 ),
    'operator' => 'IN',
    )));
    $q->set( 'orderby', 'title' );
    $q->set( 'order', 'ASC' );
    }
    }

    If you are still having problems, we recommend asking development and customization related questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Thread Starter Arthur Virabyan

    (@virabyan)

    Thank you for the suggestion, but the Rearrange Woocommerce Products plugin doesn’t help with my specific issue. The plugin allows for sorting individual products, but it doesn’t provide the option to sort by categories.

    Additionally, the code provided doesn’t work as intended because it only displays products from the specified category, while the rest of the products disappear. I tried writing my own code to achieve this, but I encountered the same issue where products from other categories were not displayed.

    If there’s another way to achieve this kind of sorting, I would really appreciate your guidance.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @virabyan

    The issue you’re experiencing might be due to the limitation of the ‘tax_query’ parameter. It is designed to filter products based on taxonomy terms, not to sort them. Therefore, when you use it, it will only display the products that meet the specified criteria.

    To achieve your goal, you might need to use a more complex query that selects all products from category ID 221 and then all other products. This would likely involve joining two separate queries, which can be a bit tricky.

    Unfortunately, providing a detailed solution for this would require custom development work, which is beyond the scope of our support. I would recommend seeking help from the following:

    Alternatively, you can use the WooCommerce Product collections block to make your shop archive page. It has the option to filter products based on category. So, basically, you will add two Product collection blocks, one to show category-specific products on top and another to show the rest.

    If you are still having problems, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    I’m sorry I couldn’t provide a more direct solution, but I hope this information helps point you in the right direction. If you have any other questions, please don’t hesitate to ask.

    Maya

    (@tdgu)

    Hi,
    The simplest method is by using the Advanced Post Types Order plugin. It allows you to effortlessly switch between categories and customize sorting while preserving the previous order. For more information, check out Sorting for WooCommerce Products .

    Additionally, the plugin supports numerous other features, which you can explore in Sorting WooCommerce

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.