WooCommerce Stock number display - wordpress

I've been trying to get rid of our exact stock numbers on product pages on our wordpress/woocommerce website. Ideally it shows In stock, Only 1 or 2 left in stock when theres only 1 or 2 available, or just sold out. I've tried many of the code snippets found here on stack overflow but none of them seem to change anything. Also the standard woocommerce settings at Products > Inventory to don't show any stock numbers doesn't do anything. On the front end it keeps showing Availability: Exact stock amount. Can anyone help me out?
Adding these kind of snippets to my child theme's functions.php or even in a code snippet plugin doesn't seem to do anything for me:
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Sold Out', 'woocommerce');
}
return $availability;
}

A better way is to use woocommerce_get_availability_text instead of woocommerce_get_availability. This filters only the availability text and is called after woocommerce_get_availability.
add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text', 10, 2 );
function set_custom_availability_text( $availability, $product ) {
if ( $product->is_in_stock() ) {
$availability = __( 'Available!', 'woocommerce' );
} elseif ( ! $product->is_in_stock() ) {
$availability = __( 'Out of stock', 'woocommerce' );
}
return $availability;
}
If the above code doesn't do anything, test if the filter is being called by using the die() function. This will stop the execution of any code that comes after it and prints whatever string you put in between the parentheses:
add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text_test', 10, 2 );
function set_custom_availability_text_test( $availability, $product ) {
die('Filter is called');
}
So if the filter is called correctly your page should render up to the availabitly text and display 'Filter is called' where the availabilty text should be. If nothing happens this means the filter isn't called. So then check if your functions.php is being loaded, or use a plugin like Code Snippets.

This snippet help you to change text for all product or particular one:
<?php
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability',1,2);
function wcs_custom_get_availability($availability,$_product ) {
global $product;
switch($_product->slug){
// Enter product slug for case
case "test-1":
// text for in stock mode
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
}
// text for out of stock mode
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('Enter your custom text here', 'woocommerce');
}
break;
case "test2":
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
}
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __(' Enter your custom text here', 'woocommerce');
}
break;
case "test3":
if($_product->manage_stock = true){
$availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity نفر",$availability['availability']);
}
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __(' Enter your custom text here', 'woocommerce');
}
break;
}
return $availability;
}

Related

Change frontend text for “backorder allowed”

WooCommerce has a product option to “allow backorder but inform customer” which shows a notice on the frontend product page. How can I change the text of this notice?
I have seen this helpful post https://storepro.io/learn/how-to-change-out-of-stock-text-in-woocommerce/ sharing how to change "out of stock" text, but that is different from the "backorder allowed" text.
Code snippet for function.php from the above linked page:
add_filter('woocommerce_get_availability_text', 'themeprefix_change_soldout', 10, 2 );
/* Change Sold Out Text to Something Else */
function themeprefix_change_soldout ( $text, $product) {
if ( !$product->is_in_stock() ) {
$text = '<div class="">My custom sold out message.</div>';
}
return $text;
}
You can copy the code from original source code for the backorder validation and rewrite the status text and return it from the function.
Here is the code:
function vh_wc_custom_get_availability_text( $availability, $_product ) {
if ( $_product->managing_stock() && $_product->is_on_backorder( 1 ) ) {
$availability = $_product->backorders_require_notification() ? __( 'Your new text goes here', 'your-child-theme-text-domain' ) : '';
} elseif ( ! $_product->managing_stock() && $_product->is_on_backorder( 1 ) ) {
$availability = __( 'Your new text goes here', 'your-child-theme-text-domain' );
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'vh_wc_custom_get_availability_text', 10, 2 );
You can check this page for more detail on woocommerce_get_availability_text filter hook.

Issue changing WooCommerce coupon label on mobile

I am trying to change the coupon name/label in the WooCommerce cart and checkout tables with:
add_filter( 'woocommerce_cart_totals_coupon_label', 'custom_woocommerce_cart_totals_coupon_label', 10, 2 );
function custom_woocommerce_cart_totals_coupon_label( $label, $coupon ) {
if ( $coupon->get_code() !== 'coupon1' ) {
return 'Rabattcode'. strtoupper( $coupon->code );
}
return $label;
}
Now I need to add a -class around the actual coupon code name, so strtoupper( $coupon->code ) should be wrapped inside <small></small>. How can I do this?
I tried
return 'Rabattcode'. '<small>' . strtoupper( $coupon->code ) . '</small>';
but this does not work on mobile screens, there the actual HTML is displayed like: Rabattcode<small>COUPON2</small>

How to Add custom price on single product without affecting to related product?

I have code to changes the custom message on all product with 2 different messages. Please take a look.
add_filter('woocommerce_empty_price_html', 'show_alert_info_if_no_price');
function show_alert_info_if_no_price ($product){
if (is_product()) {
global $product;
$price = $product->get_price();
if ($price == '') {
ob_start();
// return for the product page
return '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
} else {
// otherwise return short text as kontak kami
return 'Contact us';
}
}
}
Now I have a problem on related product. I need the related product price will appear like :
//short text as kontak kami
Now I am stuck how to add another code when using hooked.
$woocommerce_loop['name'] != 'related').
any help will appreciated!
There is an mistake in your code, because this hook is only executed for empty prices. So going to check in the hook again for an empty price and if it isn't, running an else condition is useless.
To display a different text on the single product page for related products you can use global $woocommerce_loop
So you get:
function filter_woocommerce_empty_price_html( $html, $product ) {
global $woocommerce_loop;
// True on a single product page
if ( is_product() ) {
$html = '<div class="alert-info">Produk ini hanya dapat diproses melakukan pemesanan pembelian (PO). Segera hubungi tim kami. Kontak kami</div>';
// Related
if ( $woocommerce_loop['name'] == 'related' ) {
$html = __( 'Some other text', 'woocommerce' );
}
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );

Automatically add woocommerce product short description on creation

I want to add a product short description by default whenever is the new product is being created. All the products will have the same short description, so there is no point keep copying and pasting it. So it should just be there when I click on the add a new product.
I would appreciate any help.
add_filter( 'woocommerce_short_description',
'single_product_short_description', 10, 1 );
function single_product_short_description( $post_excerpt )
{
global $product;
if ( is_single( $product->id ) )
$post_excerpt = '<div class="product-message"><p>' . __( "Article only
available in the store.", "woocommerce" ) . '</p></div>' . $post_excerpt;
return $post_excerpt;
}
I found the above code but couldn't get it to work :(
Thank you.
Regards,
Emre.
Add this code inside your themes function.php
Change the short description content as per your need - "Here goes your short desc."
add_filter( 'wp_insert_post_data' , 'cdx_add_product_short_desc' , '99', 1 );
function cdx_add_product_short_desc( $data )
{
//only for product post type
if($data['post_type'] == 'product' ) {
//only if short description is not present
if( '' == trim($data['post_excerpt']) ):
$short_desc = 'Here goes your short desc.';
$data['post_excerpt'] = $short_desc ;
endif;
}
// Returns the modified data.
return $data;
}
This will work to automatically override anything put into a product's short description field on the front-end only. It will not add the text to the backend field itself, which is good because it keeps it globalized if you need to change it later.
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 );
function filter_woocommerce_short_description( $post_excerpt ) {
$post_excerpt = '<div class="product-message"><p>' . __( "Article only available in the store.", "woocommerce" ) . '</p></div>';
return $post_excerpt;
}

How to skip "You cannot add another to your cart" Error in woocommerce and directly jump to Checkout?

What Woocommerce Does is...
When products are Sold Individually, and when the product already exists in the Cart and customer clicks on Add to Cart, Woocommerce shows Error Message "You cannot add another to your cart.....View Cart"
Instead of the above flow I want..
When customer clicks on Add to Cart and if the product already exists in the Cart then woocommerce should redirect to Checkout page straightway.
I think this can be achieved by editing few lines of code in class-wc-cart.php of Woocommerce Plugin.
The is given below:
// Force quantity to 1 if sold individually and check for existing item in cart
if ( $product_data->is_sold_individually() ) {
$quantity = apply_filters( 'woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data );
$in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;
if ( $in_cart_quantity > 0 ) {
throw new Exception( sprintf( '%s %s', wc_get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_title() ) ) );
}
}
I was just looking into how to do this myself. user1072884's answer is okay, however, it only works for one productID at a time. Not ideal if you have multiple products.
Here's a simplified solution that will work for all products.
Simply add the following code to your child theme functions.php and swap the placeholder text with your checkout page URL.
/** If multiple items are in the cart redirect to the checkout page instead of throwing an error **/
add_filter( 'woocommerce_add_to_cart_validation', 'check_cart' );
function check_cart( $cart_item_data ) {
global $woocommerce;
if ( $woocommerce->cart->cart_contents_count > 0 ) {
$direct_url = home_url( 'xyz' ); // Change the value of your actual redirect url.
wp_redirect( $direct_url );
exit();
}
}
Old thread, but maybe someone else will find this helpful.
Try this it will work:
add_filter( 'woocommerce_add_to_cart_sold_individually_quantity', 'vipcomment_change_quantity_to_zero', 10, 5 );
function vipcomment_change_quantity_to_zero( $one, $quantity, $product_id, $variation_id, $cart_item_data ) {
$your_product_id = 96;
if ( $product_id == $your_product_id ) {
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
return 0;
} else {
return $quantity;
}
} else {
return $quantity;
}
}
add_filter( 'woocommerce_add_to_cart_sold_individually_found_in_cart', 'vipcomment_is_product_exist_in_cart', 10, 5 );
function vipcomment_is_product_exist_in_cart( $exist, $product_id, $variation_id, $cart_item_data, $cart_id ) {
$your_product_id = 96;
if ( $product_id == $your_product_id ) {
return false;
} else {
return $exist;
}
}
Simplest way would be:
throw new Exception( sprintf( '%s %s',
wc_get_cart_url(), __( 'View cart', 'woocommerce' ),
header( "Location: https://www.example.com/cart/" ) ) );
instead of that error. Be advised: this will redirect the user to the cart page ( where the product should already be in place ).

Resources