Created
April 13, 2022 11:40
-
-
Save mio31337/3e2b31bda3acec309cb101fed4cd889a to your computer and use it in GitHub Desktop.
Add class if only 1 item left in stock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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