Ejemplo n.º 1
0
 /**
  * @param int $categoryId
  * @param ProductLink $productLink
  * @param bool $isInPositions
  * @param string $stateExceptionMessage
  * @return bool
  * @throws \Magento\Framework\Exception\CouldNotSaveException
  * @throws \Magento\Framework\Exception\NoSuchEntityException
  * @throws \Magento\Framework\Exception\StateException
  */
 public function setProductToCategory($categoryId, ProductLink $productLink, $isInPositions, $stateExceptionMessage)
 {
     /** @var CategoryModel $category */
     $category = $this->categoryLoaderFactory->create()->load($categoryId);
     $productId = $this->productFactory->create()->getIdBySku($productLink->getSku());
     $productPositions = $category->getProductsPosition();
     if ($isInPositions === array_key_exists($productId, $productPositions)) {
         throw new StateException($stateExceptionMessage);
     }
     $newProductPositions = [$productId => $productLink->getPosition()] + $productPositions;
     $category->setPostedProducts($newProductPositions);
     try {
         $category->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not save product "%1" with position %2 to category %3', [$productId, $productLink->getPosition(), $categoryId], $e);
     }
     return true;
 }
Ejemplo n.º 2
0
 private function prepareMocksForAssign($categoryId, $productId = 333)
 {
     $productSku = 'sku333';
     $productsPosition = [105 => 16, 334 => 1];
     $this->productLink->expects($this->once())->method('getSku')->will($this->returnValue($productSku));
     $this->productLink->expects($this->any())->method('getPosition')->will($this->returnValue($categoryId));
     $this->categoryLoader->expects($this->once())->method('load')->with($this->equalTo($categoryId))->will($this->returnValue($this->category));
     $this->category->expects($this->once())->method('getProductsPosition')->will($this->returnValue($productsPosition));
     $newProductPositions = [$productId => $categoryId] + $productsPosition;
     $this->category->expects($this->any())->method('setPostedProducts')->with($this->equalTo($newProductPositions));
     $this->product->expects($this->once())->method('getIdBySku')->with($this->equalTo($productSku))->will($this->returnValue($productId));
 }