Ad

Issue With Output Of WooCommerce Hooks On Cart And Checkout Page

I am trying to add some text below the applied coupon on my cart page, but for some reason, I can only get it to display above the table, as per the screenshot.

screenshot of cart

I even created a fresh install with the twentytwentyone theme, and no other plugins installed besides woocommerce.

These are the code that I am using:

add_action('woocommerce_cart_totals_before_shipping', 'bb_before_shipping');
function bb_before_shipping() {
    echo 'woocommerce_cart_totals_before_shipping';
}

And

add_action('woocommerce_before_cart_totals', 'apply_product_on_coupon');
function apply_product_on_coupon() {
    global $woocommerce;
    if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
        echo 'woocommerce_before_cart_totals';
    }       
}

Any help will be appreciated!

Ad

Answer

You are using the correct hook for the cart page, but the output is part of a HTML table.

So you get:

// Cart
function action_woocommerce_cart_totals_before_shipping() {
    echo '<tr><td>woocommerce_cart_totals_before_shipping</td></tr>';
}
add_action( 'woocommerce_cart_totals_before_shipping', 'action_woocommerce_cart_totals_before_shipping' );

// Checout
function action_woocommerce_review_order_before_shipping() {
    echo '<tr><td>woocommerce_review_order_before_shipping</td></tr>';  
}
add_action( 'woocommerce_review_order_before_shipping', 'action_woocommerce_review_order_before_shipping' );
Ad
source: stackoverflow.com
Ad