Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change position of woocommerce "added to cart" message

I am using woocommerce version 2.3.13 and I want to change position of woocommerce messages like "Product was successfully added to cart", which appears on the top of the product page when I click on "add to cart" button.

I want this message to appear somewhere else.

I've tired many solutions including this by removing 'woocommerce_show_messages' on 'woocommerce_before_single_product' hook

code: remove_action( 'woocommerce_before_single_product', 'woocommerce_show_messages' );

and then calling woocommerce_show_messages(); function where I want the message to appear but with no luck.

Anyone please help me in this regard.

EDIT:

I've forgot to mention that I am using storefront theme and using custom template for single product.

And these messages are appearing at the top of page by default.

like image 268
Code Poet Avatar asked Jan 27 '26 08:01

Code Poet


2 Answers

The messages 'Product was successfully added to cart' are woocommerce notices. These messages are printed by the function

    <?php wc_print_notices(); ?>

Try changing the position of this function in template.

To add notices via hooks

add_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );
add_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );

STOREFRONT Theme Edit: ** Works with Woocoommerce: 3.1.2 + Storefront: 2.2.5 **

In storefront theme the wc_print_notices is hooked into storefront_content_top

so to remove messages from top in storefront theme you need to add following code in functions.php

remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 ); /*Archive Product*/
remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 ); /*Single Product*/
remove_action( 'storefront_content_top', 'storefront_shop_messages', 1 );

and add function

wc_print_notices();

Wherever you want notices to appear.

EDIT: The messages will not appear on Single Product Page and Archive Product Page. Though they might appear on any other woocommerce page(cart, checkout, etc).

like image 113
Akash K. Avatar answered Jan 29 '26 05:01

Akash K.


This works for me (WooCommerce 3.2+) (based on this answer for @LoicTheAztec)

add_action( 'wp_head', 'reposition_sf_messages' );
function reposition_sf_messages(){
    if( is_product() ) {
        remove_action( 'storefront_content_top','storefront_shop_messages',15 );
    }
    remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 ); /*Single Product*/
    add_action('woocommerce_product_meta_end', 'storefront_shop_messages', 1 );
}
like image 36
Louis Avatar answered Jan 29 '26 06:01

Louis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!