I tried this function to make a custom stock status for variable products in WooCommerce but doesn`t work
add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
function custom_get_availability( $availability, $product ) {
// Only for variable products
if ( $product->is_type( 'variable' ) ) {
// Get the stock status of the product
$stock_status = $product->get_stock_status();
// Custom stock status
if( $stock_status == 'outofstock' ) {
$availability['availability'] = __('Custom Out of Stock Message', 'woocommerce');
}
}
return $availability;
}
add_filter('woocommerce_out_of_stock_message', 'change_variable_out_of_stock_availability', 10, 1);
function change_variable_out_of_stock_availability($out_of_stock_message) {
return __('Custom Out of Stock Message', 'woocommerce');
}
WooCommerce has the template for variable product single-product/add-to-cart/variable.php.This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/variable.php. Or this message can be altered with the dedicated hook (woocommerce_out_of_stock_message) for the variable product out-of-stock message
Related
so everybody knows that in order to change the text for In stock and out of stock in the individual product pages you do
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;
}
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
However, in the product catalog page, where you search for products, the message doesn't change there.
What do I have to do in functions.php to change that text?
woocommerce_after_shop_loop_item_title can be changed depending where you want it placed.
Try the following function -
function show_product_status_catalog() {
global $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');
}
echo $availability['availability'];
}
add_action( 'woocommerce_after_shop_loop_item_title', 'show_product_status_catalog' );
I would like to change the text "in stock" behind the number in stock. I tried adding this PHP code in my wordpress php editor but it doesn't work.
Do you have any idea why?
Thank you !
add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
function bbloomer_custom_get_availability_text( $availability, $product ) {
$stock = $product->get_stock_quantity();
if ( $product->is_in_stock() && $product->managing_stock() ) $availability = $stock. 'remaining copies' ;
return $availability;
}
Try the following instead:
add_filter( 'woocommerce_get_availability', 'filter_product_availability', 10, 2 );
function filter_product_availability( $availability, $product ) {
$stock_quantity = $product->get_stock_quantity(); // get stock quantity
if ( $product->is_in_stock() && $stock_quantity > 0 ) {
$availability['availability'] = sprintf( __('%d remaining copies'), $stock_quantity );
}
return $availability;
}
Or also you can try this:
add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability_text, $product ) {
return str_replace( 'in stock', __('remaining copies', 'woocommerce'), $availability_text );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
You can use the in build option for this go to WooCommerce->Preferences->Products->Inventar
and choose the first option. Show available Products and save.
I want to show the stock status for every product in cart with the status "few in stock".
Like this:
Productname
Size: L
Only 4 left <- this is new
I found a snippet to show the stock status on shop pages:
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_show_stock_shop', 10 );
function bbloomer_show_stock_shop() {
global $product;
echo wc_get_stock_html( $product );
}
Source: https://businessbloomer.com/woocommerce-add-stock-quantity-on-shop-page/
I tried to change the hook from woocommerce_after_shop_loop_item to woocommerce_after_cart_item_name to display the status below the title in the cart.
But the snippet doesn't work.
Also I have no idea how to limit it to items with a low stock.
please try below code in function.php file
add_filter( 'woocommerce_cart_item_name', 'showing_stock_in_cart_items', 99, 3 );
function showing_stock_in_cart_items( $item_name, $cart_item, $cart_item_key ) {
// The WC_Product object
$product = $cart_item['data'];
if (empty($product)) {
return $item_name;
}
// Get the stock
$stock = $product->get_stock_quantity();
// When stock doesn't exist
if (empty($stock)) {
return $item_name;
}
// display the stock
if ($stock <= '50') :
$item_name .='<br><p style="color:green;" class="product-stock">'.__( "only " .$stock. " left","woocommerce").'</p>';
endif;
return $item_name;
}
also refer action and filter hook:-documentation here
In Woocommerce I am trying to change the add to cart text from "Add to cart" to "Out of stock" on single product pages when the product is out of stock, for simple products and the product variations on variable products.
We are currently not using any sort of stock management because all of our products are hand made & we do set stock status manually.
Is there any possible way to achieve that?
For all products except variable products, is quite simpleā¦ But for variable products and it's variations, it requires some more code and the usage of javascript/jQuery.
So the following code will change add to cart text to "Out of stock" when the current product is out of stock even for selected product variations on variable products:
// For all products except variable product
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_single_add_to_cart_text_filter_callback', 20, 2 );
function product_single_add_to_cart_text_filter_callback( $button_text, $product ) {
if( ! $product->is_in_stock() && ! $product->is_type('variable') ) {
$button_text = __("Out of stock", "woocommerce");
}
return $button_text;
}
// For all product variations (on a variable product)
add_action( 'woocommerce_after_add_to_cart_button', 'after_add_to_cart_button_action_callback', 0 );
function after_add_to_cart_button_action_callback() {
global $product;
if( $product->is_type('variable') ) :
$data = [];
// Loop through variation Ids
foreach( $product->get_visible_children() as $variation_id ){
$variation = wc_get_product( $variation_id );
$data[$variation_id] = $variation->is_in_stock();
}
$outofstock_text = __("Out of Stock", "woocommerce");
?>
<script type="text/javascript">
jQuery(function($){
var b = 'button.single_add_to_cart_button',
t = $(b).text();
$('form.variations_form').on('show_variation hide_variation found_variation', function(){
$.each(<?php echo json_encode($data); ?>, function(j, r){
var i = $('input[name="variation_id"]').val();
if(j == i && i != 0 && !r ) {
$(b).html('<?php echo $outofstock_text; ?>');
return false;
} else {
$(b).html(t);
}
});
});
});
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Tried to write as comment but seems code formatting not working, so writing as an answer.
For a single variation add to cart shortcode this hack will help.
// For variation product
add_filter( 'woocommerce_product_add_to_cart_text', 'product_variation_add_to_cart_text_filter_callback', 20, 2 );
function product_variation_add_to_cart_text_filter_callback( $button_text, $product ) {
if ( ! $product->is_in_stock() && $product->is_type( 'variation' ) ) {
$button_text = __( "Out of stock", "woocommerce" );
}
return $button_text;
}
In WooCommerce and I have added a custom field "description" for each product.
I was able to find a way to show both, the label name and the value:
add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
$special_item = get_post_meta( $product_id , 'description',true );
if(!empty($special_item)) {
$cart_item_data[ 'description' ] = $special_item;
// below statement make sure every add to cart action as unique line item
$cart_item_data['unique_key'] = md5( microtime().rand() );
WC()->session->set( 'description', $special_item );
}
return $cart_item_data;
}
// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
if( isset( $cart_item['description'] ) ) {
$cart_item_data[] = array( "name" => __( "Description", "woocommerce" ), "value" => $cart_item['description'] );
}
return $cart_item_data;
}
Now I need to display ONLY the value (not the label name "Description") of this custom field in the cart and checkout table. I need to display with <small>, like the attribute I am displaying with this code:
add_filter('woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2);
function wp_woo_cart_attributes($cart_item, $cart_item_key){
$productId = $cart_item_key['product_id'];
$product = wc_get_product($productId);
$taxonomy = 'pa_color';
$value = $product->get_attribute($taxonomy);
if ($value) {
$label = get_taxonomy($taxonomy)->labels->singular_name;
$cart_item .= "<small>$value</small>";
}
return $cart_item;
}
How can I make it for this custom field, displaying the value only?
You don't need to include a product custom field as custom cart item data, as it's directly accessible from the product object (or the product ID).
Note: On a cart item variable $cart_item, the WC_Product Object is included and available using $cart_item['data'].
Try the following to add a custom field after the item name in cart and checkout pages:
// Display in cart and checkout pages
add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_name', 10, 3 );
function customizing_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data']; // Get the WC_Product Object
if ( $value = $product->get_meta('description') ) {
$product_name .= '<small>'.$value.'</small>';
}
return $product_name;
}
To display it on orders and email notifications use:
// Display in orders and email notifications
add_filter( 'woocommerce_order_item_name', 'customizing_order_item_name', 10, 2 );
function customizing_order_item_name( $product_name, $item ) {
$product = $item->get_product(); // Get the WC_Product Object
if ( $value = $product->get_meta('description') ) {
$product_name .= '<small>'.$value.'</small>';
}
return $product_name;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.