Search code examples
phpwordpresswoocommercecart

How to load WooCommerce cross sell and cart-collaterals inside one div


I'm trying to load the WooCommerce .cross-sells div inside a custom div named .cart-collaterals-cross-sell which already includes the .cart-collaterals elements, so that i can style them better in one row. At the moment the .cross-sells is loaded by the cross-sells.php and i tried to implement the code of it into the cart.php, so that i have both functions in one file and that i could put them inside the same div. Problem is that the cross sells doesn't load when i copy the code inside the cart.php

That's the original code of the cart.php which includes already the .cart-collaterals

<div class="cart-collaterals-cross-sell">
<div class="cart-collaterals">

    <h2><?php _e( 'Cart totals', 'woocommerce' ); ?></h2>

    <?php if ( ! is_ajax() && wc_coupons_enabled() ) { ?>
        <div class="nm-coupon-wrap">
            <div class="nm-coupon-inner">
                <a href="#" id="nm-coupon-btn"><?php esc_html_e( 'Gutschein', 'nm-framework' ); ?></a>

                <div class="nm-coupon">
                    <input type="text" id="nm-coupon-code" class="input-text" name="nm_coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" />

                    <input type="submit" id="nm-apply-coupon-btn" class="button border" name="nm_apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />

                    <?php do_action( 'woocommerce_cart_coupon' ); ?>
                </div>
            </div>
        </div>
    <?php } ?>

    <?php 
        /**
         * Cart collaterals hook.
         *
         * @hooked woocommerce_cross_sell_display
         * @hooked woocommerce_cart_totals - 10
         */
        do_action( 'woocommerce_cart_collaterals' );
    ?>

</div>
</div>

And that's the way i tried it. What am i doing wrong?

<div class="cart-collaterals-cross-sell">
<div class="cart-collaterals">

    <h2><?php _e( 'Cart totals', 'woocommerce' ); ?></h2>

    <?php if ( ! is_ajax() && wc_coupons_enabled() ) { ?>
        <div class="nm-coupon-wrap">
            <div class="nm-coupon-inner">
                <a href="#" id="nm-coupon-btn"><?php esc_html_e( 'Gutschein', 'nm-framework' ); ?></a>

                <div class="nm-coupon">
                    <input type="text" id="nm-coupon-code" class="input-text" name="nm_coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" />

                    <input type="submit" id="nm-apply-coupon-btn" class="button border" name="nm_apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />

                    <?php do_action( 'woocommerce_cart_coupon' ); ?>
                </div>
            </div>
        </div>
    <?php } ?>

    <?php 
        /**
         * Cart collaterals hook.
         *
         * @hooked woocommerce_cross_sell_display
         * @hooked woocommerce_cart_totals - 10
         */
        do_action( 'woocommerce_cart_collaterals' );
    ?>

</div>
    <div class="cross-sells">

        <h2><?php _e( 'You may be interested in&hellip;', 'woocommerce' ) ?></h2>

        <?php woocommerce_product_loop_start(); ?>

            <?php foreach ( $cross_sells as $cross_sell ) : ?>

                <?php
                    $post_object = get_post( $cross_sell->get_id() );

                    setup_postdata( $GLOBALS['post'] =& $post_object );

                    wc_get_template_part( 'content', 'product' ); ?>

            <?php endforeach; ?>

        <?php woocommerce_product_loop_end(); ?>

    </div>
</div>

Solution

  • You can use following two functions

    1. For Cart collaterals ==> woocommerce_cart_totals();
    2. For Cross Sells =======> woocommerce_cross_sell_display();

    Try Using following code I am providing here:

    <div class="cart-collaterals-cross-sell">
    <div class="cart-collaterals">
    
        <h2><?php _e( 'Cart totals', 'woocommerce' ); ?></h2>
    
        <?php if ( ! is_ajax() && wc_coupons_enabled() ) { ?>
            <div class="nm-coupon-wrap">
                <div class="nm-coupon-inner">
                    <a href="#" id="nm-coupon-btn"><?php esc_html_e( 'Gutschein', 'nm-framework' ); ?></a>
    
                    <div class="nm-coupon">
                        <input type="text" id="nm-coupon-code" class="input-text" name="nm_coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" />
    
                        <input type="submit" id="nm-apply-coupon-btn" class="button border" name="nm_apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
    
                        <?php do_action( 'woocommerce_cart_coupon' ); ?>
                    </div>
                </div>
            </div>
        <?php } ?>
    
        <?php 
            /**
             * Cart collaterals hook.
             *
             * @hooked woocommerce_cross_sell_display
             * @hooked woocommerce_cart_totals - 10
             */
            //do_action( 'woocommerce_cart_collaterals' );
            woocommerce_cart_totals();
        ?>
    
    </div>
        <div class="cross-sells">
    
            <?php woocommerce_cross_sell_display(); ?>
    
        </div>
    </div>