• Resolved Warren

    (@rabbitwordpress)


    Hi there,

    I want to add a message on the cart / basket page for specific products only.

    I used the code below but the message displays for all products instead of just the three that its indented for: 672, 667, 260.

    add_action( 'woocommerce_cart_totals_before_shipping', 'twg_add_checkout_message', 12 );
    function twg_add_checkout_message() {
        // set your products IDs here:
        $product_ids = array( 672, 667, 260 );
        $bool = false;
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $item = $cart_item['data'];
            if ( in_array( $item->id, $product_ids ) )
                $bool = true;
        }
        // If the special cat is detected in one items of the cart
        // It displays the message
        if ($bool)
            echo '<div class="vouchers_checkout_message">';
            echo '<h3>How can I get my voucher before Christmas?</h3>';
            echo '<p>Royal Mail are currently experiencing long delays, so if you need your voucher to arrive <strong>before Christmas</strong>, we suggest <strong>upgrading to the £4.99 DHL</strong> option which is delivered the next working day after dispatch. Our last shipping date with DHL is <strong>22nd December</strong>.</p>';
            echo '</div>';
    }

    Please help, or is there better solution to the snippet above.

    Many thanks
    Warren

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Warren

    (@rabbitwordpress)

    Hi there,

    So it seems only one echo will work, when I changed it to this:

    if ($bool)
            echo '<div class="vouchers_checkout_message"><h3>How can I get my voucher before Christmas?</h3><p>Royal Mail are currently experiencing long delays, so if you need your voucher to arrive <strong>before Christmas</strong>, we suggest <strong>upgrading to the £4.99 DHL</strong> option which is delivered the next working day after dispatch. Our last shipping date with DHL is <strong>22nd December</strong>.</p></div>';

    it worked perfectly.

    Please let me know if there is a better way of doing this?

    Many thanks
    Warren

    • This reply was modified 2 years, 3 months ago by Warren.
    Plugin Author Néstor Soriano Vilchez

    (@konamiman)

    Hi Warren. The first version of your code displays the message for all product because the echo statements aren’t enclosed in brackets. It should be like this:

    if ($bool) {
        echo ...
        echo ...
    }

    Other than that your code seems correct.

    Thread Starter Warren

    (@rabbitwordpress)

    Hi @konamiman,

    That’s brilliant, you’re a star, works perfectly now!

    Thanks very much
    Warren

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display Message on Cart Page for Specific Products Only’ is closed to new replies.