/**
  * Saving product inventory data. Product qty calculated dynamically.
  *
  * @param EventObserver $observer
  * @return $this
  */
 public function execute(EventObserver $observer)
 {
     $product = $observer->getEvent()->getProduct();
     if ($product->getStockData() === null) {
         if ($product->getIsChangedWebsites() || $product->dataHasChangedFor('status')) {
             $this->stockIndex->rebuild($product->getId(), $product->getStore()->getWebsiteId());
         }
         return $this;
     }
     $this->saveStockItemData($product);
     return $this;
 }
示例#2
0
 public function testSaveInventoryData()
 {
     $productId = 4;
     $websiteId = 5;
     $stockData = null;
     $websitesChanged = true;
     $statusChanged = true;
     $product = $this->getMock('Magento\\Catalog\\Model\\Product', ['getStockData', 'getIsChangedWebsites', 'dataHasChangedFor', 'getId', 'getStore', '__wakeup'], [], '', false);
     $product->expects($this->once())->method('getStockData')->will($this->returnValue($stockData));
     if ($stockData === null) {
         $product->expects($this->any())->method('getIsChangedWebsites')->will($this->returnValue($websitesChanged));
         $product->expects($this->any())->method('dataHasChangedFor')->will($this->returnValue($statusChanged));
         if ($websitesChanged || $statusChanged) {
             $product->expects($this->once())->method('getId')->will($this->returnValue($productId));
             $store = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId', '__wakeup'], [], '', false);
             $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId));
             $product->expects($this->once())->method('getStore')->will($this->returnValue($store));
             $this->stockIndex->expects($this->once())->method('rebuild')->will($this->returnValue(true));
         }
     } else {
         $stockItem = $this->getMockForAbstractClass('Magento\\CatalogInventory\\Api\\Data\\StockItem', ['__wakeup'], '', false);
         $this->stockRegistry->expects($this->once())->method('getStockItem')->with($productId, $websiteId)->will($this->returnValue($stockItem));
     }
     $this->event->expects($this->once())->method('getProduct')->will($this->returnValue($product));
     $this->eventObserver->expects($this->atLeastOnce())->method('getEvent')->will($this->returnValue($this->event));
     $this->assertEquals($this->observer, $this->observer->saveInventoryData($this->eventObserver));
 }
示例#3
0
 /**
  * Catalog Product website update
  *
  * @param EventObserver $observer
  * @return void
  */
 public function catalogProductWebsiteUpdate(EventObserver $observer)
 {
     $websiteIds = $observer->getEvent()->getWebsiteIds();
     $productIds = $observer->getEvent()->getProductIds();
     foreach ($websiteIds as $websiteId) {
         foreach ($productIds as $productId) {
             $this->stockIndex->rebuild($productId, $websiteId);
         }
     }
 }
 public function testSaveInventoryData()
 {
     $productId = 4;
     $websiteId = 5;
     $stockData = null;
     $websitesChanged = true;
     $statusChanged = true;
     $store = $this->getMock('Magento\\Store\\Model\\Store', ['getWebsiteId'], [], '', false);
     $store->expects($this->once())->method('getWebsiteId')->will($this->returnValue($websiteId));
     $product = $this->getMock('Magento\\Catalog\\Model\\Product', ['getStockData', 'getIsChangedWebsites', 'dataHasChangedFor', 'getId', 'getStore'], [], '', false);
     $product->expects($this->once())->method('getStockData')->will($this->returnValue($stockData));
     $product->expects($this->any())->method('getIsChangedWebsites')->will($this->returnValue($websitesChanged));
     $product->expects($this->any())->method('dataHasChangedFor')->will($this->returnValue($statusChanged));
     $product->expects($this->once())->method('getId')->will($this->returnValue($productId));
     $product->expects($this->once())->method('getStore')->will($this->returnValue($store));
     $this->stockIndex->expects($this->once())->method('rebuild')->will($this->returnValue(true));
     $this->event->expects($this->once())->method('getProduct')->will($this->returnValue($product));
     $this->observer->execute($this->eventObserver);
 }