Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #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/9b1gjYJ

    How can i show 5 items (as set by the preference) but not displaying out of stock items ?

    Thank you

    #1207394

    @Rikard .. or anyone else .. been a week .. any answer to this ?
    Thanks

    #1208081

    Hi,

    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,
    Ismael

    #1208265

    Dear @Ismael
    Thank you for answering

    I 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)

    #1209753

    … Any answer guys !?
    Thanks !

    #1211547

    … Hello :) Anyone ???

    #1211774

    Hi,

    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,
    Ismael

    #1211931

    Hello @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

    #1213271

    Hi,

    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,
    Ismael

    #1213309

    Hello @ismael
    Thank you

    It did not work
    https://snipboard.io/YTe2PW.jpg

    Custom 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, 1 month ago by Cloudypro.
    #1214936

    Hi,

    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,
    Ismael

    #1217755

    Hello @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

    #1218389

    Hi,

    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,
    Ismael

    #1218415

    Thank you !

    #1219039

    Hi,

    You’re welcome. Please don’t hesitate to open a new thread if you need anything else.

    Have a nice day!

    Best regards,
    Ismael

Viewing 15 posts - 1 through 15 (of 15 total)
  • The topic ‘related products displaying out of stock’ is closed to new replies.