Remove the stock quantity from WooCommerce cart error messages - php

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

Related

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

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;
}

Show selected out of stock products In Woocommerce

I'm trying to find a solution to a client demand, without success. She's asking me how to show a selected number of out of stock products on her online store. By default, the Woocommerce setting is set to "hide out of stock products", but she wants to select some of her products and show them (even with 0 stock, because she wants to tell their customers that those few products will be available soon -there is a text for this-).
We have tried with a very simple snippet using the hook woocommerce_product_is_visible that we thought it would work, but there is something that we are missing...
This is de code:
// [WooCommerce] Show some out of stock products even the hide option is active
add_filter( 'woocommerce_product_is_visible', 'keep_showing_specific_out_of_stock_product_list', 10, 2 );
function keep_showing_specific_out_of_stock_product_list( $visible, $product_ID ){
$product_list = array( 18013, 18050 ); // Insert the products IDs that want to show
return in_array( $product_ID, $product_list )? true : $visible;
}
Any help is appreciated.
Why you don't simply use a Woocommerce shortcode like:
1) In the Wordpress text editor of a page or a post (or in a widget):
[products ids="18013,18050"]
2) In any PHP code file:
echo do_shortcode( "[products ids='18013,18050']" );
The products out of stock are displayed just like in this real example:

Woocommerce - Disable Add to Cart Conditionally on Custom Field

Everything I can find on removing the WooComm Add To Cart button will remove not just the add to cart button but also the pricing/variations, aka the whole add to cart area.
My goal is to enable/disable the ability to purchase a product with a checkbox/selector on the product info page. BUT I still have to be able to see the product variation pricing and the variation drop down menu.
This is important. The pricing shown under the product title, for a variation, will be something like $20.00 - $40.00 and not until you select the variation choice will it show the price next to the add to cart button.
So far I have things working wherein I can remove the add to cart area — variations and all — conditionally on my custom field, but I have no idea how to hide/disable click/remove just the add to cart button and allow variations to still be chosen with the variation price displayed.
function remove_add_to_cart(){
if(get_post_meta(get_the_ID(), 'woo_callforinfo', true)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
} add_action('woocommerce_single_product_summary','remove_add_to_cart');
Here's what I did. The conditional IF statement is because I have a RETAIL shop with variable products that I don't want affected.
function remove_add_to_cart(){
if ( has_term( 'wholesale', 'product_tag' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
add_action('woocommerce_single_variation','remove_add_to_cart');
Added this to get rid of the 'Sorry..' message if price is not set.
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text,
$untranslated_text, $domain )
{
if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
$translated_text = __( '-type anything you want here, or leave a space- ', $domain );
}
return $translated_text;
}
Just add the following code in your functions.php and you will find button hidden
I don't know whether my solution is perfect. But it works. Normally if is_purchasable is returned to the filter woocommerce_is_purchasable, the ‘Add to Cart’ button is displayed, and if false is returned the button is hidden.
So, you just need to add the following:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
// Write code to access custom field value in this function
// let $custom_value be the value from checkbox
return ($custom_value == false ? false : $is_purchasable);
}
No incompatibility issues would creep up.

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' );
}
}

How to disable or remove stock notes from showing in WooCommerce Order edit screen

I have stock management enabled on my WooCommerce store as this is critical being that we sell physical goods and products that we stock in our warehouse. Everything about the inventory management works as we need it to, but because we have it enabled, we're getting extra order notes displaying on the Edit Order screens of WooCommerce. This is causing extra data to be saved to our database and also gets based into QuickBooks as order notes that we just do not need to be there.
I've found in the core WooCommerce the function that is adding this order note, I'm just not sure how to remove it without modifying core files. I'm looking for some sort of way to disable or remove it with a hook, filter, or class extension that can be placed in my sites utility plugin.
Screenshot showing sidebar of Edit Order screen with numerous "stock reduced" messages displayed
The code is in the abstract-wc-order.php file (/woocommerce/abstracts/abstract-wc-order.php) starting at lines 2460:
if ( isset( $item['variation_id'] ) && $item['variation_id'] ) {
$this->add_order_note( sprintf( __( 'Item #%s variation #%s stock reduced from %s to %s.', 'woocommerce' ), $item['product_id'], $item['variation_id'], $new_stock + $qty, $new_stock) );
} else {
$this->add_order_note( sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $item['product_id'], $new_stock + $qty, $new_stock) );
}
You can grab the comment id and content via the wp_insert_comment action hook and delete it, which would likely prevent it from being sent to Quickbooks. Using a simple strpos match for "stock reduced from" we can check if the comment is for stock reduction.
add_action('wp_insert_comment', 'remove_stock_comment', 10, 2);
function remove_stock_comment($id, $comment) {
if( strpos($comment->comment_content, 'stock reduced from') !== false ) {
wp_delete_comment( $id );
}
}
To further check whether the comment belongs to an order, you can get the comment's POST ID and check its post_type.
Note: I'm not sure whether this solution would break any other functionality, but this is the only solution I could come up with.

Categories