Change "No shipping method has been selected.…" from WooCommerce checkout notice - php

I am trying to tackle an issue by renaming the default message below when there are no shipping methods available, either set by rule based plugins or there simply isn't any methods set in WooCommerce.
No shipping method has been selected. Please double check your address, or contact us if you need any help.
I have used the below function using the snippets plugin and I can confirm it changes the message in the cart and checkout underneath the shipping label.
However, if someone then attempts to checkout they get a red WooCommerce error message at the top of the screen which is still the default message above.
How do I also change this error message ? I would like to change it to something different than what my code below shows to give me the flexibility to put a short message in the cart totals box and a longer more informational message as to why no methods are available.
My Function:
add_filter( 'woocommerce_cart_no_shipping_available_html', 'no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html', 'no_shipping_available_html' );
function no_shipping_available_html( $message ) {
$country = WC()->customer->get_shipping_country();
if ( !empty( $country ) ) {
$all_countries = WC()->countries->get_countries();
return sprintf( 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.', $all_countries[ $country ] );
}
return 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.';
}

This text is located in WC_Checkout Class inside validate_checkout() method. There are no filters to make changes to it, but you can use WordPress gettext filter for that as follows:
add_filter( 'gettext', 'change_checkout_no_shipping_method_text', 10, 3 );
function change_checkout_no_shipping_method_text( $translated_text, $text, $domain ) {
if ( is_checkout() && ! is_wc_endpoint_url() ) {
$original_text = 'No shipping method has been selected. Please double check your address, or contact us if you need any help.';
$new_text = 'Here your own text replacement.';
if ( $text === $original_text ) {
$translated_text = $new_text;
}
}
return $translated_text;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

You could use a plugin like Loco translate if you want, that makes easier to do your job.. but you could try this code, similar to #LoicTheAztec inside functions.php (either on your theme or child theme file)
add_filter( 'gettext', 'ds_translate_woocommerce_strings', 999, 3 );
function ds_translate_woocommerce_strings( $translated,
$untranslated, $domain ) {
if ( ! is_admin() ) {
switch ($translated) {
case 'the message you want to be translated exactly as you see it in WP':
$translated = 'your new message';
break;
}
}
return $translated;
}

Related

Change Text Strings On Specific WooCommerce Endpoints

I'm trying to conditionally change strings on WooCommerce My Account Endpoints. Each time I try to add an endpoint condition it either breaks the site or doesn't work; however when I don't use the condition the string replace works. Maybe someone can point out where I'm going wrong.
Working Snippet - Without Condition
function change_endpoint_text( $translated ) {
$translated = str_ireplace( 'List of coupons which are valid & available for use. Click on the coupon to use it. The coupon discount will be visible only when at least one product is present in the cart.', 'List of vouchers which are valid & available for use. Click on a voucher to use it. The discount will be visible only when at least one product is present in the cart.', $translated );
return $translated;
}
add_filter( 'gettext', 'change_endpoint_text' );
Conditional Snippet Test - Not Working
Note: I also tried using an is_account_page condition that seemed to break the site.
function change_endpoint_text( $translated ) {
if( is_wc_endpoint_url('wc-smart-coupons') ) {
$translated = str_ireplace( 'List of coupons which are valid & available for use. Click on the coupon to use it. The coupon discount will be visible only when at least one product is present in the cart.', 'List of vouchers which are valid & available for use. Click on a voucher to use it. The discount will be visible only when at least one product is present in the cart.', $translated );
}
return $translated;
}
add_filter( 'gettext', 'change_endpoint_text' );
Any help would be appreciated.
You can also check for end points using $wp->query_vars['custom_endpoint'].
For example,
function change_endpoint_text ( $translated_text, $text, $domain ) {
global $wp;
if ( isset( $wp->query_vars['wc-smart-coupons'] ) ) {
if ($translated_text == 'some existing text') {
$translated_text = __( 'The new replaced text', 'woocommerce');
}
}
return $translated_text;
}
add_filter( 'gettext', 'change_endpoint_text' );

Change add to cart stock error notice in WooCommerce

When a customer tries to add too many of an item to cart, they see this error notice:
You cannot add that amount to the cart - we have X in stock and you already have Y in your cart.
This behavior is included on WC_Cart add_to_cart() method source code at line 1067.
I will like to hide stock details and replace it with:
You cannot add that amount to the cart — we don't have enough in stock.
How to change this add to cart stock error notice in WooCommerce, without overwriting source code?
That is possible using gettext filter hook in a custom hooked function this way:
add_filter( 'gettext', 'custom_add_to_cart_stock_error_notice', 10, 3 );
function custom_add_to_cart_stock_error_notice( $translated, $text, $domain ) {
if ( $text === 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.' && 'woocommerce' === $domain ) {
$translated = __("You cannot add that amount to the cart — we don't have enough in stock.", $domain );
}
return $translated;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You can change the said text without modifying the plugin files with the help of "Say What" Plugin. Follow the following steps to change the text:
Install Say What plugin
Visit Tools > Text Changes
Click Add New
Fill the fields as per your need e.g.
Original string: You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.
Text domain: woocommerce
Text context:
Replacement string: You cannot add that amount to the cart — we don't have enough in stock.
Click Add button to save the changes.
Note that #loictheaztec's code works except that the quotes need to be changed to single quote marks in the fourth line if you want to use quantities
add_filter( 'gettext', 'custom_add_to_cart_stock_error_notice', 10, 3 );
function custom_add_to_cart_stock_error_notice( $translated, $text, $domain ) {
if ( $text === 'You cannot add that amount to the cart — we have %1$s in stock and you already have %2$s in your cart.' && 'woocommerce' === $domain ) {
$translated = __('You cannot add that to the cart — we have %1$s in stock and you already have %2$s in your cart.', $domain );
}

Add Applied Coupon Code in Admin New Order Email Template - WooCommerce

Let me clear my question:
I have downloaded & activated WooCommerce Plugin for E-Commerce Functionality.
I want to add "Applied coupon code" in Admin New Order Email Template using my custom plugin.
Now:
Can you tell me that exact Hook or Function which is actually setting up that New Order Email Template so that i will override it?
Can you tell me how to call applied coupon code, so that i will display this in email template?
It would be great, if you help me please.
This can be done using a custom function hooked in woocommerce_email_order_details action hook (for example) that will display in admin emails notifications the used coupons in the order:
// The email function hooked that display the text
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;
foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
}
// For multiple coupons
else {
$coupon_codes = implode( ', ', $coupon_codes);
echo '<p>'.__( 'Coupons Used: ').$coupon_codes.'<p>';
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works...
This is because $coupon_codes is not an array() define it with $coupon_codes=array();

Remove the stock quantity from WooCommerce cart error messages

Iin WooCommerce, I've set woocommerce->settings->products->inventory->stock display format to "Never show quantity remaining in stock".
However if a customer ads a product to the cart, continues to cart or checkout page and enter a value higher than we have in stock they get this error message:
Sorry, we do not have enough "{product_name}" in stock to fulfill your order ({available_stock_amount} in stock). Please edit your cart and try again. We apologize for any inconvenience caused.
What filter can I use to edit this output? I don't want it to show (absolutely anywhere on the front end shop) the actual available stock amount.
I've found that this is handled in a function (check_cart_item_stock) in, [root]->wp-content->plugins->woocommerce->includes->class-wc-cart.php on line 491:
if ( ! $product->has_enough_stock( $product_qty_in_cart[ $product->get_stock_managed_by_id() ] ) ) {
/* translators: 1: product name 2: quantity in stock */
$error->add( 'out-of-stock', sprintf( __( 'Sorry, we do not have enough "%1$s" in stock to fulfill your order (%2$s in stock). Please edit your cart and try again. We apologize for any inconvenience caused.', 'woocommerce' ), $product->get_name(), wc_format_stock_quantity_for_display( $product->get_stock_quantity(), $product ) ) );
return $error;
}
So what I want to filter out is the "(%2$s in stock)" part. But I can't find any filter for this.
This messages displayed are located in WC_Cart class source code at line 493 and 522 …
What you can try is to replace the text by your custom text version in this function hooked in WordPress gettex filter hook:
add_filter( 'gettext', 'wc_replacing_cart_stock_notices_texts', 50, 3 );
function wc_replacing_cart_stock_notices_texts( $replacement_text, $source_text, $domain ) {
// Here the sub string to search
$substring_to_search = 'Sorry, we do not have enough';
// The text message replacement
if( strpos( $source_text, $substring_to_search ) ) {
// define here your replacement text
$replacement_text = __( 'Sorry, we do not have enough products in stock to fulfill your order…', $domain );
}
return $replacement_text;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should works (unstested)…
Thank you #LoicTheAztec for your reply, but I actually found a filter for this after all, woocommerce_add_error
So my final filter (in functions.php) is this:
function remove_stock_info_error($error){
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $item) {
$product_id = isset($item['variation_id']) ? $item['variation_id'] : $item['product_id'];
$product = new \WC_Product_Factory();
$product = $product->get_product($product_id);
if ($item['quantity'] > $product->get_stock_quantity()){
$name = $product->get_name();
$error = 'Sorry, we do not have enough "'.$name.'" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.';
return $error;
}
}
}add_filter( 'woocommerce_add_error', 'remove_stock_info_error' );
This should resolve it across the board.
NOTE! I also found that the input boxes have a max attribut, which in turn means that anyone can still see the actual total available amount (by either simply using the built in increment (which will stop when reaching max value) or just enter to high of a value, click update cart and you will get a notice that the amount has to be equal to or less than X (max value)).
To combat this I added a simple JS in my pre-existing "woo-xtra.js":
var qty = $('form.woocommerce-cart-form').find('input.qty');
// Reset max value for quantity input box to hide real stock
qty.attr('max', '');
This way there is no max value, but the user will still get the error (if over the limit) from above :)
I.e. Problem solved

How to hide specific messages in woocommerce?

i want to hide/remove specific messages from woo commerce without modifying basic woocommerce plugin.
There are several types of messages related to coupon like
Coupon code already applied!
Sorry! Coupon 12345 already applied to your cart. (here i essentially want to hide coupon code)
and several others similar to these coupon codes.
i just want to hide these type of coupon/cart messages, others are fine like "Product successfully added!" or any other error messages.
Basically the aim is to show all other messages (Error and success messages) but dont wanna show coupon messages and a coupon code in to these messages.
So, is there any way to do this by doing any hook etc, like one i've found to eliminate all message strings (if i am not wrong).
add_filter( 'woocommerce_coupon_message', '__return_empty_string' );
One more thing is, one message is repeating on cart page several times when i add product in to the car. "Coupon code already applied!" 2,3, to 4 times.
Okay, found solution
go to woocommerce tempaltes, copy notices folder and edit the desired template, in my case its error.php
copy/edit code
<ul class="woocommerce-error">
<?php
foreach ( $messages as $message ) :
if ( $message == "Coupon code already applied!" ) {
$message = "";//empty error string
} else if (strpos($message, 'does not exist!') !== false) {
$message = ""; //empty error string
}
else if (strpos($message, 'Sorry, it seems the coupon') !== false) {
$message = "";//empty error string
}
else if (strpos($message, 'Sorry, this coupon is not applicable to your cart contents') !== false) {
$message = "Sorry, the discount is not applicable to your cart contents"; //updated error string
}
?>
<li><?php echo wp_kses_post( $message ); ?></li>
<?php
break;
endforeach; ?>
</ul>
I know this is an old thread, but I think I have just worked out how to do this for the coupon error messages.
On my store I use WooCommerce Smart Coupons which allows coupons to be sold as gift cards. I don't want people to be able to purchase gift cards using a gift card, so I have added the gift card category to the exclude category list on the coupon usage restriction.
Anyway, I wanted to alter the error message if somebody tried to use a gift card coupon code when there was a gift card in the cart. This is the code I used:
function filter_woocommerce_coupon_error( $err, $err_code, $instance ) {
if ( $err_code == '114' ) {
global $woocommerce;
$categories = $instance->get_excluded_product_categories();
if ( in_array( '15', $categories ) ) {
$err = sprintf( __( 'Sorry, you cannot use a Gift Card to purchase another Gift Card.' ) );
}
}
return $err;
};
add_filter( 'woocommerce_coupon_error', 'filter_woocommerce_coupon_error', 10, 3 );
The $err_code for specific error messages can be found in the file woocommerce/includes/class-wc-coupon.php.
In my case, I wanted to edit error message for error code 114 (E_WC_COUPON_EXCLUDED_CATEGORIES). I also wanted my custom message to only appear if a 114 error was triggered by a gift card being in the cart, not for every 114 error. To solve this I added in the if ( in_array( '15', $categories ) ). What this does is check to see if the error message has been triggered by a product that is in category 15 (this being the gift card category for my store. Change 15 to whichever category you are using).
The $instance variable is what WooCommerce uses to pass the details of the cart/coupon back to the function.
I'm very new to coding, so I am sorry if my code and my explanation isn't great, but it definitely appears to work for me. I added it into functions.php.
So the crux of your question is - how can I customise WooCommerce coupon messages?
I have half an answer - I've customised coupon messages (the green boxed ones) using the "woocommerce_coupon_message" filter. BUT I've not yet been able to get the coupon error messages (the red boxed ones) working using the "woocommerce_coupon_error" filter.
I've tried conditional statements based on a few different methods from Class WC_Cart to no avail. I just cant seem to "intercept" (and then customise) the error messages before they're printed. If somebody has a solution to Coupon Errors I'd be happy to hear it.
Anyhow... the below function is hooked into the "woocommerce_before_cart" & "woocommerce_before_checkout_form" actions so the function works for either page.
It can obviously be customised to no end, but my example basically tests for a valid coupon and then you can change the message or return nothing. You can also test for other conditions to throw up custom notices of all sorts! Much better then modifying template files! :-)
add_action( 'woocommerce_before_cart', 'custom_coupon_messages' );
add_action( 'woocommerce_before_checkout_form', 'custom_coupon_messages' );
function custom_coupon_messages() {
global $woocommerce;
//Set coupon codes.
$coupon_code = 'Bigly-Yuge';
//Set coupon objects.
$coupon_test = new WC_Coupon( 'Bigly-Yuge' );
//Get the cart subtotal. Should return as a Double.
$cart_subtotal = WC()->cart->subtotal;
//If coupon test is passed add coupon.
if ( $coupon_test->is_valid() && $woocommerce->cart->has_discount( $coupon_code ) ) {
//Filter the coupon success message to display a custom message.
add_filter( 'woocommerce_coupon_message', 'filter_woocommerce_coupon_message', 10, 3 );
function filter_woocommerce_coupon_message( $msg, $msg_code, $instance ) {
//Set a custom coupon message.
$msg_code = 'case self::WC_COUPON_SUCCESS';
$msg = __( 'You saved BIGLY YUGE!', 'woocommerce' );
return $msg;
return $msg_code;
//Or return nothing (no message will be displayed - comment out the above/uncomment below).
// return '';
};
//Print the above notice to screen.
wc_print_notices();
}
elseif ( $cart_subtotal > 499 ) {
//Print a notice (the blue boxed one)
wc_print_notice( 'Spend $500 to qualify for the BIGLY YUGE discount!!!', 'notice' );
}
}

Categories