cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Stock Inventory

Custom Stock Inventory

Hello.

 

I have a customer who sells supplies B2B. She needs to be able to sell some products in individual quantity or box quantity of 32.

 

The problem is she builds the boxes up out of the core inventory so I have an issue I cant seem to find a solution to.

 

- She has 200 jars in stock for "Blue Jars"

- Customer purchases a box quantity of 32 using a product variation.

- Stock needs to reduce the 32 purchased items as 32 individual quantities from the core inventory of that product.

 

Since the customer is keeping an eye on inventory for the whole product and not each possible variation using bundles doesnt seem to be an option as each bundle would need its inventory defined.

 

Ideally id be looking for a solution which would allow me to "decrease stock quantity by x" when setting up a variation.

 

Any ideas?

2 REPLIES 2

Re: Custom Stock Inventory

There is no standard way to do it, best solution if is to add observer sales_order_invoice_pay this way you are making sure that your order is paid. Then in your observer you can get parent product 

/**
 * @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable $_configurableType
 */
$parentIds = $this->_configurableType->getParentIdsByChild($productId);
$parentId = array_shift($parentIds);

then you can load parent product and update it's stock:

/**
 * @var \Magento\Catalog\Model\ProductFactory $_productloader
 */
$product = $this->_productloader->create()->load($parentId);
$sku = $product->getSku();
/**
 * @var \Magento\CatalogInventory\Api\StockRegistryInterface $_stockRegistry
 */
$stockItem = $this->_stockRegistry->getStockItemBySku($sku);
$qty = $stockItem->getQty() - $qty_ordered;
$stockItem->setQty($qty);
$stockItem->setIsInStock((bool)$qty); // this line
$this->_stockRegistry->updateStockItemBySku($sku, $stockItem);

Re: Custom Stock Inventory

Thank you for this. I will give it a try!