/** * Get Stock Data * * @param StockItemInterface $stockItem * @return array */ private function getData(StockItemInterface $stockItem) { $result = $stockItem->getData(); $result[StockItemInterface::MANAGE_STOCK] = (int) $stockItem->getManageStock(); $result[StockItemInterface::QTY] = (double) $stockItem->getQty(); $result[StockItemInterface::MIN_QTY] = (double) $stockItem->getMinQty(); $result[StockItemInterface::MIN_SALE_QTY] = (double) $stockItem->getMinSaleQty(); $result[StockItemInterface::MAX_SALE_QTY] = (double) $stockItem->getMaxSaleQty(); $result[StockItemInterface::IS_QTY_DECIMAL] = (int) $stockItem->getIsQtyDecimal(); $result[StockItemInterface::IS_DECIMAL_DIVIDED] = (int) $stockItem->getIsDecimalDivided(); $result[StockItemInterface::BACKORDERS] = (int) $stockItem->getBackorders(); $result[StockItemInterface::NOTIFY_STOCK_QTY] = (double) $stockItem->getNotifyStockQty(); $result[StockItemInterface::ENABLE_QTY_INCREMENTS] = (int) $stockItem->getEnableQtyIncrements(); $result[StockItemInterface::QTY_INCREMENTS] = (double) $stockItem->getQtyIncrements(); $result[StockItemInterface::IS_IN_STOCK] = (int) $stockItem->getIsInStock(); return $result; }
/** * Check if is possible subtract value from item qty * * @param StockItemInterface $stockItem * @return bool */ protected function canSubtractQty(StockItemInterface $stockItem) { return $stockItem->getManageStock() && $this->stockConfiguration->canSubtractQty(); }
/** * Returns suggested qty that satisfies qty increments and minQty/maxQty/minSaleQty/maxSaleQty conditions * or original qty if such value does not exist * * @param StockItemInterface $stockItem * @param int|float $qty * @return int|float */ public function suggestQty(StockItemInterface $stockItem, $qty) { // We do not manage stock if ($qty <= 0 || !$stockItem->getManageStock()) { return $qty; } $qtyIncrements = (int) $stockItem->getQtyIncrements(); // Currently only integer increments supported if ($qtyIncrements < 2) { return $qty; } $minQty = max($stockItem->getMinSaleQty(), $qtyIncrements); $divisibleMin = ceil($minQty / $qtyIncrements) * $qtyIncrements; $maxQty = min($stockItem->getQty() - $stockItem->getMinQty(), $stockItem->getMaxSaleQty()); $divisibleMax = floor($maxQty / $qtyIncrements) * $qtyIncrements; if ($qty < $minQty || $qty > $maxQty || $divisibleMin > $divisibleMax) { // Do not perform rounding for qty that does not satisfy min/max conditions to not confuse customer return $qty; } // Suggest value closest to given qty $closestDivisibleLeft = floor($qty / $qtyIncrements) * $qtyIncrements; $closestDivisibleRight = $closestDivisibleLeft + $qtyIncrements; $acceptableLeft = min(max($divisibleMin, $closestDivisibleLeft), $divisibleMax); $acceptableRight = max(min($divisibleMax, $closestDivisibleRight), $divisibleMin); return abs($acceptableLeft - $qty) < abs($acceptableRight - $qty) ? $acceptableLeft : $acceptableRight; }
/** * Check if is possible subtract value from item qty * * @param \Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem * @return bool */ protected function _canSubtractQty(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem) { $result = $stockItem->getManageStock() && $this->_configStock->canSubtractQty(); return $result; }