-
AuthorPosts
-
April 19, 2020 at 6:33 pm #1205312
Greetings
I would like to hide the “Out of stock” items from Related Products
Without going to WooCommerce Settings -> Inventory -> Hide out of stock items from the catalog
and without going to each and every product page, and modify the setting of the element itself .. i have more than 500 products and i cant go to each one and make this edit..I am using this code:
add_filter( ‘woocommerce_output_related_products_args’, function( $args )
{
$args = wp_parse_args( array(
‘posts_per_page’ => 5,
‘meta_query’ => array (
‘key’ => ‘_stock_status’,
‘value’ => ‘instock’
)
), $args );
return $args;
});However, in most times, the results i get inside the Related Products are only few and not showing all 5 items (my setting is to show 5 items)
and sometimes it does show all 5 items !
Example:
https://ibb.co/VVdX6mk
https://ibb.co/9b1gjYJHow can i show 5 items (as set by the preference) but not displaying out of stock items ?
Thank you
April 26, 2020 at 11:19 pm #1207394@Rikard .. or anyone else .. been a week .. any answer to this ?
ThanksApril 29, 2020 at 8:24 am #1208081Hi,
Without going to WooCommerce Settings -> Inventory -> Hide out of stock items from the catalog
Sorry for the delay. The Hide out of stock items from the catalog option should be enabled to hide out of stock items from the shop page and the related product section.
In the woocommerce_output_related_products_args filter above, try to include the orderby and order parameters in the $args array.
'orderby' => 'rand', // @codingStandardsIgnoreLine. 'order' => 'desc',
Best regards,
IsmaelApril 29, 2020 at 7:07 pm #1208265Dear @Ismael
Thank you for answeringI cannot enable the option in WooCommerce settings as i am using a php code for that purpose !
So the problem is that instead of showing an out of stock item, it is showing a blank result .. i want it to show a result of in stock items rather than just blank !
If that is not possible, then how can i globally change the setting for the “related products” to show products by date order (newest first” this way we will not get the out of stock products to be chosen by the related products and therefore displaying a blank result !!
or we can have the related product show only the ones that are featured (with a star on them)May 4, 2020 at 11:02 pm #1209753… Any answer guys !?
Thanks !May 10, 2020 at 12:34 am #1211547… Hello :) Anyone ???
May 11, 2020 at 9:41 am #1211774Hi,
I cannot enable the option in WooCommerce settings as i am using a php code for that purpose !
Why do you have to do it by code when the option is directly available in the settings panel?
Are you planning to change the default query? If so, then just insert the additional parameters in the related products filter without changing the default query.
Best regards,
IsmaelMay 12, 2020 at 1:01 am #1211931Hello @Ismael
Why do you have to do it by code when the option is directly available in the settings panel?
Because i am using a custom endpoint that does nt function well when this option is enabled !
Are you planning to change the default query? If so, then just insert the additional parameters in the related products filter without changing the default query.
i am not sure i understood this :(
thanks
May 15, 2020 at 1:30 pm #1213271Hi,
Try to replace the snippet with this one.
add_filter( 'woocommerce_output_related_products_args', function( $args ) { $args = wp_parse_args( array( 'posts_per_page' => 5, 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'instock' ), array( 'key' => '_backorders', 'value' => 'no' ), ) ), $args ); return $args; });
What do you mean by custom endpoint?
Best regards,
IsmaelMay 15, 2020 at 3:31 pm #1213309Hello @ismael
Thank youIt did not work
https://snipboard.io/YTe2PW.jpgCustom Endpoints: in My account section we added custom endpoints like tabs to show products and courses of every user
- This reply was modified 4 years, 5 months ago by Cloudypro.
May 21, 2020 at 9:55 am #1214936Hi,
Some of the products are probably missing because the wc_get_related_products function shuffles the array that contains the related products or posts.
if ( apply_filters( 'woocommerce_product_related_posts_shuffle', true ) ) { shuffle( $related_posts ); }
Try to add this filter in the functions.php file to disable the shuffle.
add_filter('woocommerce_product_related_posts_shuffle', '__return_false' );
If this doesn’t work, you might have to remove the snippet and enable the default out of stock option. For additional assistance, you can contact our partner, Codeable.
// https://kriesi.at/contact/customization
Best regards,
IsmaelMay 29, 2020 at 3:36 pm #1217755Hello @ismael
Thank you .. this actually works however it is showing same results on all products of the same category, i am not getting different variations ! The same 5 products are showing on all other products of the same category
Can we force the related products to show the ones that have a star (featured) only ? and keep the shuffle within this criteria ?
In other word, if i have 50 products with star (featured), then we can have them displayed in a shuffle way and those ones that are not featured will not be displayed such as the ones i have them out of stock
Thanks
June 1, 2020 at 10:28 am #1218389Hi,
That is possible but it’s beyond the scope of support. You can ask a freelance developer to adjust the logic inside the wc_get_related_products function, and tell them that they have to find a way to pre shuffle the product items after querying them and before including them inside the $related_posts array or variable.
Best regards,
IsmaelJune 1, 2020 at 12:25 pm #1218415Thank you !
June 3, 2020 at 1:17 pm #1219039 -
AuthorPosts
- The topic ‘related products displaying out of stock’ is closed to new replies.