WooCommerce Hook: woocommerce_cart_collaterals

Marc Wag­ner

Sep­tem­ber 11, 2024

3 min read|

Woo­Com­mer­ce offers a varie­ty of hooks and fil­ters to extend the func­tion­a­li­ty of an online store. One of the­se hooks is woocommerce_cart_collaterals. This hook is useful if you want to add addi­tio­nal con­tent or func­tion­a­li­ty near the so-cal­­led “Cart Col­la­te­rals”, which typi­cal­ly con­tain things like the “Sub­to­tal” sec­tion, cou­pon field or ship­ping infor­ma­ti­on.

In this blog post, we’ll look at how this hook works and go through some prac­ti­cal PHP examp­les that show how to use it.

What is the woocommerce_cart_collaterals Hook? #

The hook woocommerce_cart_collaterals is cal­led in the stan­dard Woo­Com­mer­ce tem­p­la­te cart/cart.php. It is used to insert con­tent within the col­la­te­ral area of the shop­ping cart, which nor­mal­ly con­ta­ins the grand total area and ship­ping opti­ons.

Position of the hook in the template:

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

Possible applications #

With this hook you can:

  • Add user-defi­­ned notes or infor­ma­ti­on.
  • Dis­play adver­ti­sing ban­ners or pro­mo­ti­ons.
  • Imple­ment resel­ling or cross-sel­­ling stra­te­gies.
  • Show addi­tio­nal shop­ping cart recom­men­da­ti­ons.

Example 1: Inserting a user-defined message in the collaterals

Let’s ima­gi­ne you want to add a cus­tom mes­sa­ge to alert the cus­to­mer to spe­cial pro­mo­ti­ons. You can do this with the fol­lo­wing code

add_action( 'woocommerce_cart_collaterals', 'custom_cart_collaterals_message' );

function custom_cart_collaterals_message() {
    echo '<div class="custom-cart-message">';
    echo '<p><strong>Jetzt kaufen und 10% Rabatt auf die nächste Bestellung erhalten!</strong></p>';
    echo '</div>';
}

This code inserts a simp­le cus­tom mes­sa­ge into the col­la­te­ral area of the shop­ping cart.

Explanation:

The hook woocommerce_cart_collaterals is used to add the user-defi­­ned func­tion custom_cart_collaterals_message. An HTML struc­tu­re con­tai­ning the mes­sa­ge is out­put within the func­tion.

Example 2: Show cross-selling products

Woo­Com­mer­ce has a built-in func­tion to dis­play cross-sel­­ling pro­ducts in the shop­ping cart. You can cus­to­mi­ze or add the dis­play of cross-sel­­ling pro­ducts by using the woocommerce_cart_collaterals hook.

remove_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' ); // Entfernt die standardmäßige Anzeige

add_action( 'woocommerce_cart_collaterals', 'custom_cross_sell_display' );

function custom_cross_sell_display() {
    // Zeige maximal 4 Cross-Sell-Produkte an, 2 pro Reihe
    woocommerce_cross_sell_display( 4, 2 );
}

Explanation

First, the default cross-sell dis­play with remove_action is remo­ved. Then the cus­tom func­tion custom_cross_sell_display is added to dis­play up to 4 cross-sel­­ling pro­ducts in two rows.

Example 3: Adding an advertising banner for a special promotion

If you want to adver­ti­se a spe­cial pro­mo­ti­on or a dis­count code in the shop­ping cart, you can insert a ban­ner with the hook.

add_action( 'woocommerce_cart_collaterals', 'custom_promo_banner' );

function custom_promo_banner() {
    echo '<div class="promo-banner">';
    echo '<img src="https://yourwebsite.com/promo-banner.jpg" alt="Sonderaktion" />';
    echo '<p>Verwenden Sie den Code <strong>SALE20</strong> und sparen Sie 20%!</p>';
    echo '</div>';
}

Explanation:

The custom_promo_banner func­tion inserts an image and a refe­rence to a spe­cial pro­mo­ti­on with a dis­count code in the col­la­te­ral area. This can be useful to draw atten­ti­on to cur­rent pro­mo­ti­ons.

Conclusion #

The woocommerce_cart_collaterals Hook offers a fle­xi­ble way to inte­gra­te addi­tio­nal con­tent or func­tions into the shop­ping cart area of your Woo­Com­mer­ce store. Whe­ther you want to add infor­ma­ti­on, dis­play cross-sel­­ling pro­ducts or adver­ti­se pro­mo­ti­ons — this hook helps you to fur­ther per­so­na­li­ze and opti­mi­ze the check­out pro­cess.

Thanks to the simp­le hand­ling with PHP, the­se adjus­t­ments can be imple­men­ted quick­ly and effi­ci­ent­ly to impro­ve the shop­ping expe­ri­ence for your cus­to­mers.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar