skip to Main Content

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


  1. woocommerce->settings->productst->inventory->out of stock visibility = check this button.

    Login or Signup to reply.
  2. 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)

    <?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.OverrideProhibited, Squiz.PHP.DisallowMultipleAssignments.Found
    
            wc_get_template_part( 'content', 'product' );
        ?>
    
    <?php endforeach; ?>
    

    With

    <?php foreach ( $cross_sells as $cross_sell ) : ?>
    
        <?php
            $stock_status = $cross_sell->get_stock_status();
    
            if ( $stock_status != 'outofstock' ) {
    
                $post_object = get_post( $cross_sell->get_id() );
    
                setup_postdata( $GLOBALS['post'] =& $post_object ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited, Squiz.PHP.DisallowMultipleAssignments.Found
    
                wc_get_template_part( 'content', 'product' );
            }
        ?>
    
    <?php endforeach; ?>
    
    Login or Signup to reply.
  3. 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:

    <?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 WooCommerceTemplates
     * @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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search