Search code examples
phpwordpresswoocommercecartstorefront

Add product quantity, name and checkout link to WooCommerce Added To Cart Message


I am not sure how to exactly do this, and yes - I have been searching all over SO and the internet. I did find various solutions, but none of them allowed my limited experience to fix this problem on my own.

What I need is this:

"You just added QTY PRODUCT NAME to your cart. Your order subtotal is SUBTOTAL. Wanna checkout or do you want to continue shopping? BUTTON | BUTTON"

So, to explain it even further: "You just added 3 Bluetooth Headset to your cart. Your order subtotal is $299. Wanna checkout or do you want to continue shopping? Continue | Go To Checkout"

In other words, it should count how many of the product added and it should display the subtotal. On the right side, the "Continue" button links to the shop page and the "Checkout" button links to the Checkout page.

Now - and this is where my problem begins.. When on mobile, the layout over all is kind of messed up, so I decided to make a two versions of the same, allowing for the text and button to be differently aligned/placed.

This is my code, which I am sure comes from somewhere else - but I did my best trying to modify it, did my best to make it work.

If someone can please help me - thank you!

add_filter( 'wc_add_to_cart_message_html', 'atcm_with_checkout_button_including_mobile_control', 10, 3 );
function atcm_with_checkout_button_including_mobile_control( $message, $qty, $count ) {

    $subtotal = wc_price(WC()->cart->subtotal);
    $count += $qty;
    
    $direct_to_shop = wc_get_shop_url();
    $direct_checkout = is_checkout() ? '#customer_details' : wc_get_checkout_url() . '#customer_details';

        if ( wp_is_mobile() && !is_checkout() ) {

        return sprintf (__( '
            <div style="font-size:20px">You just placed QTY of PRODUCT NAME in your cart.
            Your order total right now is %s. Wanna continue shopping %s or do you want to checkout %s? 
            </div>
        ' ), 
        
        $count, $subtotal, 

        '<a style="width:100%; border-left:none; margin-bottom:10%;" 
        class="shopping button alt" href="' . $direct_to_shop . '">' . __(" Continue Shopping ") . '</a>' );

        '<a style="width:100%; border-left:none; margin-top:10%;" 
        class="checkout button alt" href="' . $direct_checkout . '">' . __(" Go to Checkout ") . '</a>' );
    
        } else {

            if ( ! wp_is_mobile() && ! is_checkout() ) {
            
                return sprintf(__('
            <div style="font-size:20px">Your just placed QTY of PRODUCT NAME in your cart, brining your order total to %s. 
            Wanna continue shopping%s or do you want to checkout?%s
            </div>
            '), 
            $count, $subtotal, 
            '<a class="shopping button alt" href="' . $direct_to_shop . '">' . __(" Continue Shopping ") . '</a>' );            
            '<a class="checkout button alt" href="' . $direct_checkout . '">' . __(" Go to Checkout ") . '</a>' );
        
        }
    }
}

I searched and tried different alternatives, trying to learn and modify on my own. I just cannot make this work.


Solution

  • There are some mistakes and errors in your code, use the following revisited code:

    add_filter( 'wc_add_to_cart_message_html', 'atcm_with_checkout_button_including_mobile_control', 10, 3 );
    function atcm_with_checkout_button_including_mobile_control( $message, $products, $show_qty ) {
        $subtotal     = WC()->cart->subtotal;
        $product_ids  = array_keys($products);
        $product      = wc_get_product( reset($product_ids) );
        $quantity     = $products[$product->get_id()];
        $shop_url     = esc_url( wc_get_page_permalink('shop') );
        $checkout_url = wc_get_checkout_url() . '#customer_details';
        $html_string  = '<a class="button" href="%s">%s</a>';
        $shop_btn     = sprintf( $html_string, $shop_url, __('Continue Shopping') );
        $checkout_btn = sprintf( $html_string, $checkout_url, __('Go to Checkout') );
    
        if (  ! is_checkout() ) {
            if ( wp_is_mobile() ) {
                $message = sprintf ( '<div style="font-size:20px">' . 
                    __('You just placed %d &times; %s in your cart. Your order total right now is %s. %s %s') .
                    '</div>', $quantity, $product->get_name(), wc_price($subtotal), $checkout_btn, $shop_btn
                );
            } else {
                $message = sprintf( '<div style="font-size:20px">' . 
                    __('You just placed %d &times; %s in your cart, %s %s <br>bringing your order total to %s.') .
                    '</div>', $quantity, $product->get_name(), $checkout_btn, $shop_btn, wc_price($subtotal)
                );
            }
        }
        return $message;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    enter image description here