Using this code to reduce the stock from inventory by Attribute Value. It works to reduce the stock, but when you have like 100 g in stock and the customer buys 120 g. It doesn't show Out of Stock.
So the following code was created:
<?php
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 );
function filter_order_item_quantity( $quantity, $order, $item )
{
$product = $item->get_product();
$stock_quantity = $product->get_stock_quantity();
$term_name = $product->get_attribute('pa_weight');
// The 'pa_size' attribute value is "15 grams" And we keep only the numbers
$quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);
// Calculated new quantity
if( is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
$quantity *= $quantity_grams;
if($quantity > $stock_quantity) {
return $quantity;
} else {
echo '<p>Out of Stock</p>';
}
}
However, it isn't working...
Is there a way to check the stock from $quantity?
if($quantity > $stock_quantity) {
return $quantity;
} else {
echo '<p>Out of Stock</p>';
}
from Reduce and check stock of Variable Product
No comments:
Post a Comment