Issue
I'm trying to change the stock management
availability text on single product page based on different conditions in WooCommerce.
There are 5 situations, depending on whether 'Enable stock management at product level' is turned on
or off
.
- Turned on, Quantity: 0
- Turned on, Quantity: 2
- Turned on, Quantity: 3+
- Turned off, Out of stock
- Turned off, In stock
I'm using
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
global $product;
// Case 3
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('3. Turned on, Quantity: 3+', 'woocommerce');
}
// Case 2
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 2 ) {
$availability['availability'] = sprintf( __('2. Turned on, Quantity: 2', 'woocommerce');
}
// Case 5
if ( $_product->is_in_stock() && $product->get_stock_quantity() <= 0 ) {
$availability['availability'] = sprintf( __('5. Turned off, In stock', 'woocommerce'));
}
// Case 4 and 1 - out of stock
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __('4. Turned off, Out of stock / 1. Turned on, Quantity: 0', 'woocommerce');
}
return $availability;
}
But how to make it work for variable products?
For simple product it works perfect but for variable products 2 (out of 5) situations shows wrong results:
- Turned on, Quantity: 0 [Works ok]
- Turned on, Quantity: 2 [Wrong - shows case no.5]
- Turned on, Quantity: 3+ [Wrong - shows case no.5]
- Turned off, Out of stock [Works ok]
- Turned off, In stock [Works ok]
Solution
if $product->get_stock_quantity() <= 0
than $_product->is_in_stock()
will always be false because the stock number is below 0 or equal to 0, so there is the error in your logic.
It is also not necessary to use and mix global $product
and $_product
. Because $_product
already contains the product object.
$product->managing_stock()
might come in handy
So you could use instead:
// Change In Stock Text
function filter_woocommerce_get_availability( $availability, $product ) {
// Managing stock is activated
if ( $product->managing_stock() ) {
// Stock quantity
$stock_quantity = $product->get_stock_quantity();
// Compare
if ( $stock_quantity <= 0 ) {
$availability['availability'] = __('1. Turned on, Quantity: less then or equal to 0', 'woocommerce' );
} elseif ( $stock_quantity > 0 && $stock_quantity <= 2 ) {
$availability['availability'] = __('2. Turned on, Quantity: 1 or 2', 'woocommerce' );
} elseif ( $stock_quantity > 3 ) {
$availability['availability'] = __('3. Turned on, Quantity: 3+', 'woocommerce' );
}
} else {
// In stock
if ( $product->is_in_stock() ) {
$availability['availability'] = __('4. Turned off, In stock', 'woocommerce' );
} else {
$availability['availability'] = __('5. Turned off, Out of stock', 'woocommerce' );
}
}
return $availability;
}
add_filter( 'woocommerce_get_availability', 'filter_woocommerce_get_availability', 10, 2 );
Answered By - 7uc1f3r Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.