Created
October 18, 2017 17:46
-
-
Save TimBHowe/d192af9ff7ebf0d9d4f8b69d727f10a0 to your computer and use it in GitHub Desktop.
Add a filter the the WooCommerce `wc_reduce_stock_levels` function to not reduce the stock if more then what is in stock is ordered.
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
<?php | |
/** | |
* Do not reduce stock if more then what is instock is ordered as all items will be back-ordered/made to order/shipped together, and someone else can have the item instock shipped immediately. | |
* TODO: Should probably check if the item allows backordering. | |
*/ | |
function wccustom_alter_order_item_quantity( $item_qty_ordered, $order, $item ) { | |
// Get the product object from the order item object. | |
$product = $item->get_product(); | |
// Some products (variations) can have their stock managed by their parent. Get the correct ID and then product object. | |
$product_id_with_stock = $product->get_stock_managed_by_id(); | |
$product_with_stock = wc_get_product( $product_id_with_stock ); | |
// Get the product current stock level before the order is processed. | |
$product_stock_lvl = $product_with_stock->get_stock_quantity(); | |
// If the item quantity ordered is greater then the current items stock, all items will be made to order so do not reduce the stock. | |
if( $item_qty_ordered > $product_stock_lvl ) { | |
$item_qty_ordered = 0; | |
} | |
return $item_qty_ordered; | |
} | |
add_filter( 'woocommerce_order_item_quantity', 'wccustom_alter_order_item_quantity', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment