Skip to content

Instantly share code, notes, and snippets.

@JeroenSormani
Last active May 16, 2021 20:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JeroenSormani/f88fc05957773cae04aaa71718cfeff0 to your computer and use it in GitHub Desktop.
Save JeroenSormani/f88fc05957773cae04aaa71718cfeff0 to your computer and use it in GitHub Desktop.
Custom inventory stock reduction in WooCommerce
<?php // For implementation instructions see: https://aceplugins.com/how-to-add-a-code-snippet/
/**
* Reduce with custom stock quantity based on the settings.
*
* @param $quantity
* @param $order
* @param $item
* @return mixed
*/
function ace_custom_stock_reduction( $quantity, $order, $item ) {
/** @var WC_Order_Item_Product $product */
$multiplier = $item->get_product()->get_meta( '_stock_multiplier' );
if ( empty( $multiplier ) && $item->get_product()->is_type( 'variation' ) ) {
$product = wc_get_product( $item->get_product()->get_parent_id() );
$multiplier = $product->get_meta( '_stock_multiplier' );
}
if ( ! empty( $multiplier ) ) {
$quantity = $multiplier * $quantity;
}
return $quantity;
}
add_filter( 'woocommerce_order_item_quantity', 'ace_custom_stock_reduction', 10, 3 );
/**
* Order change stock reduction fix.
*
* @param $prevent
* @param $item
* @param $item_quantity
* @return bool|mixed
*/
function ace_custom_stock_reduction_order_change( $prevent, $item, $item_quantity ) {
$stock_reduced = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );
$stock_reduction = ace_custom_stock_reduction( $item->get_quantity(), null, $item );
if ( $stock_reduction == $stock_reduced ) {
$prevent = true;
}
return $prevent;
}
add_filter( 'woocommerce_prevent_adjust_line_item_product_stock', 'ace_custom_stock_reduction_order_change', 10, 3 );
@jason1365
Copy link

Thanks for your work. I'm happy to contribute back. I added on a bit so that the variations available were calculating correctly and disabling the add to cart button. I suspect you'll see issues with this add, but it was my first ever mod to WP, haven't used PHP in years, and didn't any any tools - just editing functions.php in the browser and hitting refresh.

function custom_product_variation_get_stock_quantity( $value, $product ) 
{ 
	if ( is_object($product) && $product->is_in_stock() ) {
		$multiplier = $product->get_meta( '_stock_multiplier' );

		if ( $product->is_type( 'variation' ) ) {
			$product_parent = wc_get_product( $product->get_parent_id() );
			$stock_quantity = $product_parent->get_stock_quantity();
		}
		else{
			$stock_quantity = $product->get_stock_quantity();
		}

		if ( empty( $multiplier ) ) {
			$multiplier = 1;
		}
	
		$adj_stock_quantity = floor((int)$stock_quantity / $multiplier);
		
		return $adj_stock_quantity;
	}
}
add_filter( 'woocommerce_product_variation_get_stock_quantity' ,'custom_product_variation_get_stock_quantity', 10, 2 );

add_filter( 'woocommerce_variation_is_purchasable', 'custom_variation_is_purchasable', 10, 2 );
function custom_variation_is_purchasable ( $purchasable, $product ){
    // For product variations (from variable products)
    if ( $product->is_type('variation') ){
        $adj_stock_quantity = custom_product_variation_get_stock_quantity(NULL, $product);
		
		if($adj_stock_quantity <= 0 && ! $product->backorders_allowed()) {
			$purchasable = false;
		}
    }

    return $purchasable;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment