Skip to content

Instantly share code, notes, and snippets.

@mio31337
Created April 13, 2022 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mio31337/3e2b31bda3acec309cb101fed4cd889a to your computer and use it in GitHub Desktop.
Save mio31337/3e2b31bda3acec309cb101fed4cd889a to your computer and use it in GitHub Desktop.
Add class if only 1 item left in stock
/**
* Add class if only 1 item left in stock
* Author: MarioMarkovic.com
*/
function filter_get_stock_html( $html, $product ) {
// Low stock quantity amount
$low_stock_qty = 1;
$availability = $product->get_availability();
if ( ! empty( $availability['availability'] ) ) {
$class = esc_attr( $availability['class'] );
$avail_text = wp_kses_post( $availability['availability'] );
$stock_qty = $product->get_stock_quantity();
if( $stock_qty == $low_stock_qty ){
$class .= ' one-in-stock';
$avail_text = __('Samo še 1 na zalogi', 'woocommerce');
}
ob_start();
// Make your changes below
?>
<p class="stock <?php echo $class; ?>"><?php echo $avail_text; ?></p>
<?php
$html = ob_get_clean();
}
return $html;
}
add_filter( 'woocommerce_get_stock_html', 'filter_get_stock_html', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment