/** * Validate based on min and max qty and stock levels. * * @param Market_LineItemModel $lineItem * * @return mixed */ public function validateLineItem(Market_LineItemModel $lineItem) { if (!$this->unlimitedStock && $lineItem->qty > $this->stock) { $error = sprintf('There are only %d items left in stock', $this->stock); $lineItem->addError('qty', $error); } if ($lineItem->qty < $this->minQty) { $error = sprintf('Minimal order qty for this variant is %d', $this->minQty); $lineItem->addError('qty', $error); } if ($this->maxQty != 0) { if ($lineItem->qty > $this->maxQty) { $error = sprintf('Maximum order qty for this variant is %d', $this->maxQty); $lineItem->addError('qty', $error); } } }
/** * @param int $purchasableId * @param int $orderId * @param int $qty * * @return Market_LineItemModel */ public function create($purchasableId, $orderId, $qty) { $lineItem = new Market_LineItemModel(); $lineItem->purchasableId = $purchasableId; $lineItem->qty = $qty; $lineItem->orderId = $orderId; /** @var \Market\Interfaces\Purchasable $purchasable */ $purchasable = craft()->elements->getElementById($purchasableId); if ($purchasable && $purchasable instanceof Purchasable) { $lineItem->fillFromPurchasable($purchasable); } else { $lineItem->addError('purchasableId', Craft::t('Item not found or is not a purchasable.')); } return $lineItem; }