In WooCommerce, I want to display Out Of Stock products in the product category, but in terms of user experience, it is better to move these products to the end of the list so as not to confuse the user.
I am using actually Show Out of stock products at the end in Woocommerce answer code.
I made some attempts in this field, but every attempt brought a problem
For example, in an attempt to sort based on inventory over the sorting that the user had chosen, such as sorting based on price, it had priority and did not have the desired result.
Or in another attempt, only if the user chose the default sort, everything was correct, but if the user chose to sort by price or popularity, the existing and non-existent products were mixed together.
4
Answers
I found the answer to my question, I'm sharing it here to help others who have this problem. You just need to set a condition based on any type of categories and change the query according to that condition
To move out-of-stock products to the end of the product archive loop in WooCommerce, you can use a custom code snippet. WooCommerce doesn’t natively provide this functionality, so you’ll need to add some custom sorting logic. Below are the steps to achieve this:
Access Your Theme’s Functions.php File:
You’ll need to add the custom sorting logic to your WordPress theme’s functions.php file. You can access this file through your WordPress dashboard by navigating to Appearance > Theme Editor and then selecting functions.php.
Add Custom Sorting Logic:
Add the following code snippet to your functions.php file. This code will create a custom sorting option that moves out-of-stock products to the end of the list when a user selects it.
}
Save and Test:
After adding the code snippet to your functions.php file, save the changes. Now, when you go to your product category pages, you should see a new sorting option called "Out of Stock Last" in the product sorting dropdown.
When a user selects this sorting option, out-of-stock products will be moved to the end of the product list.
Please note that custom code changes like this should be made carefully, and it’s a good practice to have a backup of your site and be familiar with WordPress theme editing. Additionally, if your theme receives updates, you may need to revisit and potentially update this custom code to ensure compatibility.
You can prioritize products based on their stock status. you can use WooCommerce’s built-in filters to adjust the sorting behavior.
Something like this
I found the perfect solution to my problem.
I added the above code to the functions.php file and changed the default sorting from the customization section to sort by the most recent, and the problem of displaying outofstock products at the end of the list and ACF repeater fields was also resolved.