Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • I came up with this solution that does what you ask for, and also hides the “You may be interested in…” title in case 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:

    <?php
    /**
     * Cross-sells
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/cart/cross-sells.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any important changes.
     *
     * @see https://docs.woocommerce.com/document/template-structure/
     * @package WooCommerce\Templates
     * @version 4.4.0
     */
    
    defined( 'ABSPATH' ) || exit;
    
    if ( $cross_sells ) : ?>
    	<?php
    		ob_start();
    		foreach ( $cross_sells as $cross_sell ) :
    			$stock_status = $cross_sell->get_stock_status();
    			if ( $stock_status != 'outofstock' ) { // only display product if it is not out of stock
    				$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' );
    			}
    		endforeach;
    		$html = ob_get_clean();
    		if ($html) { // only display the whole cross sells section of there's something to display
    			?>
    				<div class="cross-sells">
    					<?php
    					$heading = apply_filters( 'woocommerce_product_cross_sells_products_heading', __( 'You may be interested in&hellip;', 'woocommerce' ) );
    
    					if ( $heading ) :
    						?>
    						<h2><?php echo esc_html( $heading ); ?></h2>
    					<?php endif; ?>
    
    					<?php woocommerce_product_loop_start(); ?>
    
    					<?php echo $html; ?>
    
    					<?php woocommerce_product_loop_end(); ?>
    
    				</div>
    
    			<?php
    		}
    	?>
    	<?php
    endif;
    
    wp_reset_postdata();

    Hope that helps!

    • This reply was modified 3 years, 8 months ago by rAthus. Reason: Fixed typos
    Thread Starter rAthus

    (@rathus)

    Hey @3sonsdevelopment ,

    Thank you so much! It seems like it solved my issue!

    I didn’t find that during my researches, maybe I was using the wrong terms.

    Have a nice day,

    Arthur

Viewing 2 replies - 1 through 2 (of 2 total)