mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/76850575/woocommerce-product-custom-displayed-stock-status-based-on-stock-quantity/76851275
23 lines
919 B
Text
23 lines
919 B
Text
add_filter('woocommerce_get_stock_html', 'filter_wc_get_stock_html', 10, 2 )
|
|
function filter_wc_get_stock_html($html, $product) {
|
|
$current_stock = $product->get_stock_quantity();
|
|
$manage_stock = $product->get_manage_stock();
|
|
$stock_status = $product->get_stock_status();
|
|
|
|
if ( $manage_stock ) {
|
|
if ( $current_stock <= 0 ) {
|
|
$new_html = __('Out of stock', 'txtdomain');
|
|
} else if ($current_stock < 20) {
|
|
$new_html = sprintf('Less than 20 %s', __('in stock', 'txtdomain'));
|
|
} else if ($current_stock > 19) {
|
|
$new_html = sprintf('Up to 20 %s', __('in stock', 'txtdomain'));
|
|
}
|
|
} else {
|
|
if ( $stock_status === 'outofstock' ) {
|
|
$new_html = __('Out of stock', 'txtdomain');
|
|
} else {
|
|
$new_html = __('In stock', 'txtdomain');
|
|
}
|
|
}
|
|
return sprintf('<p class="stock">%s</p>', $new_html);
|
|
}
|