My theme displays cross sell products on cart page which is fine.
This is done via the code below which can be found in the cart/cross-sells.php
template file.
<?php foreach ( $cross_sells as $cross_sell ) : ?>
<?php
$post_object = get_post( $cross_sell->get_id() );
setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found
wc_get_template_part( 'content', 'product' );
?>
<?php endforeach; ?>
But I dont want to display products which are out of the stock
I managed to change number of columns for cross sells etc but i cant find anywhere some snippet that displays only available products.
Can someone walk me through how to do that?
3
Answers
woocommerce->settings->productst->inventory->out of stock visibility = check this button.
There are always multiple solutions but 1 of them could be by overwriting the template file
https://github.com/woocommerce/woocommerce/blob/3.8.0/templates/cart/cross-sells.php
Replace (line: 28 – 38)
With
Inspired by @7uc1f3r’s answer, I came up with this solution that also hides the title in the cas there’s no product to display.
I did override
/wp-content/plugins/woocommerce/templates/cart/cross-sells.php
, creating a file in my child theme/wp-content/themes/MY-CHILD-THEME/woocommerce/content-product.php
, with the following code: