Пример #1
0
 /**
  * @param int $at
  * @return \PHPUnit_Framework_MockObject_MockObject
  */
 protected function getGroupProductMock($at)
 {
     $product = $this->getMock('Magento\\Catalog\\Model\\Product', ['getStockQty', '__wakeup'], [], '', false);
     $product->expects($this->once())->method('getStockQty')->will($this->returnValue(2));
     $this->stockItemRegistry->expects($this->at($at))->method('retrieve')->will($this->returnValue($product));
     return $product;
 }
Пример #2
0
 /**
  * @param int $stockData
  * @return array
  */
 public function processIsInStock($stockData)
 {
     $stockItem = $this->stockItemRegistry->retrieve($stockData['product_id']);
     $stockItem->setData($stockData);
     $stockItem->processIsInStock();
     return $stockItem->getData();
 }
Пример #3
0
 /**
  * Init stock item
  *
  * @param \Magento\Sales\Model\Quote\Item\Option $option
  * @param \Magento\Sales\Model\Quote\Item $quoteItem
  *
  * @return \Magento\CatalogInventory\Model\Stock\Item
  * @throws \Magento\Framework\Model\Exception
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function getStockItem(\Magento\Sales\Model\Quote\Item\Option $option, \Magento\Sales\Model\Quote\Item $quoteItem)
 {
     $stockItem = $this->stockItemRegistry->retrieve($option->getProduct()->getId());
     if (!$stockItem->getId()) {
         throw new \Magento\Framework\Model\Exception(__('The stock item for Product in option is not valid.'));
     }
     /**
      * define that stock item is child for composite product
      */
     $stockItem->setIsChildItem(true);
     /**
      * don't check qty increments value for option product
      */
     $stockItem->setSuppressCheckQtyIncrements(true);
     return $stockItem;
 }
Пример #4
0
 public function testAddInventoryData()
 {
     $productId = 4;
     $stockId = 6;
     $stockStatus = true;
     $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getId', 'getStockStatus', '__wakeup'])->getMock();
     $product->expects($this->once())->method('getId')->will($this->returnValue($productId));
     $product->expects($this->once())->method('getStockStatus')->will($this->returnValue($stockStatus));
     $this->event->expects($this->once())->method('getProduct')->will($this->returnValue($product));
     $stockItem = $this->getMockBuilder('Magento\\CatalogInventory\\Model\\Stock\\Item')->disableOriginalConstructor()->getMock();
     $stockItem->expects($this->once())->method('getStockId')->will($this->returnValue($stockId));
     $this->stockItemRegistry->expects($this->once())->method('retrieve')->with($productId)->will($this->returnValue($stockItem));
     $this->stockStatus->expects($this->once())->method('assignProduct')->with($product, $stockId, $stockStatus)->will($this->returnSelf());
     $this->assertEquals($this->model, $this->model->addInventoryData($this->eventObserver));
 }
 /**
  * @param int $productId
  * @return \PHPUnit_Framework_MockObject_MockObject
  */
 protected function getStockItemModel($productId)
 {
     $stockItemModel = $this->getMockBuilder('Magento\\CatalogInventory\\Model\\Stock\\Item')->disableOriginalConstructor()->getMock();
     $this->stockItemRegistry->expects($this->once())->method('retrieve')->with($productId)->will($this->returnValue($stockItemModel));
     return $stockItemModel;
 }
Пример #6
0
 /**
  * Retrieve stock qty whether product is composite or no
  *
  * @return float
  */
 public function getStockQty()
 {
     if (!$this->hasStockQty()) {
         $this->setStockQty(0);
         /** @var Product $product */
         $product = $this->productFactory->create();
         $product->load($this->getProductId());
         // prevent possible recursive loop
         if (!$product->isComposite()) {
             $stockQty = $this->getQty();
         } else {
             $stockQty = null;
             $productsByGroups = $product->getTypeInstance()->getProductsToPurchaseByReqGroups($product);
             foreach ($productsByGroups as $productsInGroup) {
                 $qty = 0;
                 foreach ($productsInGroup as $childProduct) {
                     $qty += $this->stockItemRegistry->retrieve($childProduct->getId())->getStockQty();
                 }
                 if (null === $stockQty || $qty < $stockQty) {
                     $stockQty = $qty;
                 }
             }
         }
         $stockQty = (double) $stockQty;
         if ($stockQty < 0 || !$this->getManageStock() || !$this->getIsInStock() || !$product->isSaleable()) {
             $stockQty = 0;
         }
         $this->setStockQty($stockQty);
     }
     return (double) $this->getData('stock_qty');
 }
Пример #7
0
 /**
  * Adds stock item qty to $items (creates new entry or increments existing one)
  * $items is array with following structure:
  * array(
  *  $productId  => array(
  *      'qty'   => $qty,
  *      'item'  => $stockItems|null
  *  )
  * )
  *
  * @param QuoteItem $quoteItem
  * @param array &$items
  * @return void
  */
 protected function _addItemToQtyArray($quoteItem, &$items)
 {
     $productId = $quoteItem->getProductId();
     if (!$productId) {
         return;
     }
     if (isset($items[$productId])) {
         $items[$productId]['qty'] += $quoteItem->getTotalQty();
     } else {
         $stockItem = null;
         if ($quoteItem->getProduct()) {
             /** @var Item $stockItem */
             $stockItem = $this->stockItemRegistry->retrieve($quoteItem->getProduct()->getId());
         }
         $items[$productId] = array('item' => $stockItem, 'qty' => $quoteItem->getTotalQty());
     }
 }