Skip to content

Instantly share code, notes, and snippets.

@timelsass
Last active May 21, 2021
Embed
What would you like to do?
Add and use a new stock status in WooCommerce
<?php
/**
* This code adds a lowstock stock status to WooCommerce products. There is more code
* included here for examples of usage, such as hiding the lowstock status products from
* queries so they aren't displayed in shop pages,add badges to products, and update the
* availability messages on product pages.
*
* Please see this WordPress Stack Exchange question for more information and background:
*
* https://wordpress.stackexchange.com/questions/348759/exclude-products-with-a-stock-lower-than-the-low-stock-threshold-from-the-shop-l
*/
/**
* Add low stock to WooCommerce stock status options.
*/
function add_low_stock_option( $options ) {
$options['lowstock'] = __( 'Low Stock', 'textdomain' );
return $options;
}
add_filter( 'woocommerce_product_stock_status_options', 'add_low_stock_option' );
/**
* Save the low stock status.
*/
function save_low_stock_status( $product_id ) {
$product = wc_get_product( $product_id );
// Stock managed for product individually and between "Out of Stock" and "Low Stock Amount".
if ( $product->get_manage_stock() &&
( $product->get_stock_quantity() <= $product->get_low_stock_amount() )
) {
// Update to our low stock status.
update_post_meta( $product_id, '_stock_status', 'lowstock' );
}
}
add_action( 'woocommerce_process_product_meta', 'save_low_stock_status', 20 );
/**
* Get availablity status.
*/
function get_custom_availability( $data, $product ) {
$stock_status = get_post_meta( $product->id , '_stock_status' , true );
if ( 'lowstock' === $stock_status ) {
$data = [
'availability' => __( 'Low stock', 'textdomain' ),
'class' => 'low-stock'
];
}
return $data;
}
add_action( 'woocommerce_get_availability', 'get_custom_availability', 20, 2 );
/**
* Hide the lowstock stock status products in queries.
*/
function hide_low_stock_products( $q ) {
$meta_query = $q->get( 'meta_query' );
$meta_query[] = [
'key' => '_stock_status',
'value' => 'lowstock',
'compare' => '!=',
];
$q->set( 'meta_query', $meta_query );
}
add_action( 'woocommerce_product_query', 'hide_low_stock_products' );
/**
* Add a low stock badge on shop pages.
*/
function add_low_stock_badge() {
global $product;
$stock_status = $product->get_stock_status();
if ( 'lowstock' === $stock_status ) {
echo '<span class="low-stock-badge">' . __( 'Low stock', 'textdomain' ) . '</span>';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'add_low_stock_badge' );
/**
* Update all product stock statuses that meet the criteria.
*
* NOTE: Only run this to update your products if the query below
* satisfies your desired results. Especially if running on an active site.
*/
function update_product_stock_status(){
global $wpdb;
// Low Quantity threshold.
$threshold = '2';
// SQL statement to update _stock_status to lowstock on all products where _stock >= 1 AND _stock <= $threshold set for lowstock status.
$sql .= "UPDATE $wpdb->postmeta stock, (SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = '_stock' AND meta_value BETWEEN 1 AND $threshold ) id SET stock.meta_value = 'lowstock' WHERE stock.post_id = id.post_id AND stock.meta_key = '_stock_status';";
// run queries
$wpdb->query( $sql );
}
@outrank-james

This comment has been minimized.

Copy link

@outrank-james outrank-james commented Apr 27, 2021

Line 44 - $product->id
This needs to be updated to

$product->get_id()

Else you'll get a notice that ID is called incorrectly within WooCommerce

@h08831n

This comment has been minimized.

Copy link

@h08831n h08831n commented May 15, 2021

I want to add SoldOUT status.
but I don't know how to set out of stock and when to use in_stock must return false.
can you help me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment