I have coded a custom 'Added to cart' message for WooCommerce and after that I used the conditional tag 'is_cart()' to disable the message on the cart page but it still shows up.
How can I disable the message being shown on cart page?
Here is my code example:
function iw_add_to_cart_message_function( $message, $product_id ) {
if (!is_cart()) {
$message = sprintf(esc_html__('%s has been added by to your cart pompidompidom.','iwebbers'), get_the_title( $product_id ) );
return $message;
}
}
add_filter( 'wc_add_to_cart_message', 'iw_add_to_cart_message_function', 10, 2 );
I just tried the following IF STATEMENT and it works:
global $woocommerce;
if (get_option('woocommerce_cart_redirect_after_add')=='no') {
Function wc_add_to_cart_message
it Return message rather than add it.
go refer link: docs.woocommerce
Related
I would like to edit the text on the checkout page. There are some issues with the items in your cart. Please go back to the cart page and resolve these issues before checking out.
I've been searching for ways to edit it without editing the source file. Is there a filter to edit the text on checkout page?
tried this
add_action( 'woocommerce_after_checkout_validation', 'quadlayers', 9999, 2);
function quadlayers( $fields, $errors ){
// in case any validation errors
if( !empty( $errors->get_error_codes() ) ) {
// omit all existing error messages
foreach( $errors->get_error_codes() as $code ) {
$errors->remove( $code );
}
// display custom single error message
$errors->add( 'validation', 'Your Custom Message Goes Here!!!' );
}
}
add_filter('gettext_woocommerce', 'modify_cart_error_message', 10, 3);
function modify_cart_error_message($translation, $text, $domain) {
if ('There are some issues with the items in your cart. Please go back to the cart page and resolve these issues before checking out.' === $translation) {
$translation = 'Your Custom Message Goes Here!!!';
}
return $translation;
}
The cart error message mentioned is coming from /woocommerce/templates/checkout/cart-errors.php file. Either you can override the template in themes or use the above code snippet in the active theme functions.php file
When adding product to cart user is redirected to cart/payment window and has 'added to cart' notice present. This is the default way I would like to keep, but with addition of my custom method.
My custom button allows customer to stay on shop site while adding another products. However when he enters cart/payment he gets flooded with multiple 'added to cart' messages, which looks just bad:
multiple messages
I want to know if there is option to turn off messages but only if user clicked my custom button.
I've tried attaching get parameter to button and reading it in my code:
add_filter( 'wc_add_to_cart_message_html', 'filter_wc_add_to_cart_message_html', 10, 2 );
function filter_wc_add_to_cart_message_html( $message, $products ) {
$showMSG = get_query_var('show-cmsg');
if ( $showMSG ){
return $message;
}
return null;
}
But the get_query_var always results in null, no matter the get parameters. I've registered it:
function add_custom_query_vars( $vars ){
$vars[] = "show-cmsg";
return $vars;
}
Is there a way to hide the messages depending on which button user pressed?
Or if you can block woocommerce from genereting messages if you pressed custom button?
Or just a way to even limit the messages to 1 most recent instead of showing all at once?
I would also do same for some other error mesasges, but If i would find answer to this then I quess i would manage errors.
You should use session because add_custom_query_vars just add key not value that's why you getting always null.
So for the session first check session_start() or not, then set the value in the session after that you can get anywhere accordingly.
Example:
function woo_register_session() {
if(!session_id() || session_status() == PHP_SESSION_NONE){
session_start();
}
$_SESSION['show_cmsg'] = true;
}
add_action( 'init', 'woo_register_session' );
Then get the session and use it in your function accordingly
add_filter( 'wc_add_to_cart_message_html', 'filter_wc_add_to_cart_message_html', 10, 2 );
function filter_wc_add_to_cart_message_html( $message, $products ) {
$showMSG = $_SESSION['show_cmsg'] ? true : false;
if ( $showMSG ){
return $message;
}
return null;
}
Another alternate function for message
add_filter( 'wc_add_to_cart_message', 'woo_add_to_cart_message' );
function woo_add_to_cart_message() {
$showMSG = $_SESSION['show_cmsg'] ? true : false;
if($showMSG){
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
$return_to = get_permalink(woocommerce_get_page_id('shop'));
$message = sprintf('%s %s', $return_to, __('Continue Shopping →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
else :
$message = sprintf('%s %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
endif;
}else{
$message = null;
}
return $message;
}
I managed to find solution:
function filter_wc_add_to_cart_message_html( $message, $products ) {
$urlp = explode('/', $_SERVER['REQUEST_URI']);
$urlp = $urlp[1];
if ( $urlp == 'potwierdzenie'){
return $message;
}
return null;
}
I just check if user in checkout url when buying product, then I'm showing the notice. If user is still on shop page or any other page, notice won't show. This prevents the multiple 'added to cart' messages in checkout.
I am trying to add an action that will check whether or not a product that is currently only available on back order is being checked out - and in the case that one or more is, I want to display a message before the checkout form.
I've gotten this far:
add_action( 'woocommerce_before_checkout_form', 'wnd_checkout_message', 10 );
function wnd_checkout_message( ) {
echo '<div class="wnd-checkout-message"><h3>The message goes here!</h3></div>';}
But how do I check whether or not a back ordered product is currently being checked out/is in the cart?
Add this code to your functions.php file. It will show a notice if one of the products in your cart is on backorder.
add_action( 'woocommerce_before_checkout_form', 'es_checkout_add_cart_notice' );
function es_checkout_add_cart_notice() {
$message = "You have a backorder product in your cart.";
if ( es_check_cart_has_backorder_product() )
wc_add_notice( $message, 'error' );
}
function es_check_cart_has_backorder_product() {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$cart_product = wc_get_product( $values['data']->get_id() );
if( $cart_product->is_on_backorder() )
return true;
}
return false;
}
I'm trying to redirect people who click on the add to cart button to the checkout page directly. The add to cart button only is adding the product to cart but not redirecting at all.
The code I use is only working for product with no variations.
I'm using the latest version of woocommerce 3.0.1.
//REDIRECT TO CHECKOUT
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
}
Try with this:
function add_to_cart_checkout_redirect() {
wp_safe_redirect( get_permalink( get_option( 'woocommerce_checkout_page_id' ) ) );
die();
}
add_action( 'woocommerce_add_to_cart', 'add_to_cart_checkout_redirect', 11 );
I have no cart link set in the backend of WooCommerce. Instead I have the cart redirected to the checkout page. This works fine, however now I end up with empty urls. When I add a product to the cart I get the message 'successfully added to the cart, see the cart here'. 'See the cart here' is linked to wc_get_page_permalink( 'cart' ), but this is not set.
Is it possible through a function to set the wc_get_page_permalink( 'cart' ) ONLY when items are in the cart?
I tried something like:
function custom_continue_shopping_redirect_url ( $product_id ) {
$url = "http://www.url.com"; // Add your link here
return $url;
}
add_filter('wc_add_to_cart_message', 'custom_continue_shopping_redirect_url');
But that is obviously replacing the whole add to cart message.
Thanks.
You missed some code. try it this way:
add_filter( 'wc_add_to_cart_message', 'custom_continue_shopping_redirect_url', 10, 2 );
function custom_continue_shopping_redirect_url( $message, $product_id ) {
global $woocommerce;
// $permalink = get_permalink(woocommerce_get_page_id('shop')); // replaced by:
$permalink = get_permalink(woocommerce_get_page_id('cart')); // your link here.
$message = sprintf('%s %s', $permalink, __('Continue Shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
return $message;
}
You will have to fine tune your replacement link. You can also change the text…
References:
Hookr: wc_add_to_cart_message
Alternative for the wc_add_to_cart_message hook in Woocommerce for WP
Custom Add To Cart Messages - GitHub