Exemple #1
0
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     $qty = $cartItem->getQty();
     if (!is_numeric($qty) || $qty <= 0) {
         throw InputException::invalidFieldValue('qty', $qty);
     }
     $cartId = $cartItem->getQuoteId();
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     $itemId = $cartItem->getItemId();
     try {
         /** update item qty */
         if (isset($itemId)) {
             $cartItem = $quote->getItemById($itemId);
             if (!$cartItem) {
                 throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item  %2', $cartId, $itemId));
             }
             $product = $this->productRepository->get($cartItem->getSku());
             $cartItem->setData('qty', $qty);
         } else {
             $product = $this->productRepository->get($cartItem->getSku());
             $quote->addProduct($product, $qty);
         }
         $this->quoteRepository->save($quote->collectTotals());
     } catch (\Exception $e) {
         if ($e instanceof NoSuchEntityException) {
             throw $e;
         }
         throw new CouldNotSaveException(__('Could not save quote'));
     }
     return $quote->getItemByProduct($product);
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
 {
     $qty = $cartItem->getQty();
     if (!is_numeric($qty) || $qty <= 0) {
         throw InputException::invalidFieldValue('qty', $qty);
     }
     $cartId = $cartItem->getQuoteId();
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     $itemId = $cartItem->getItemId();
     try {
         /** update item */
         if (isset($itemId)) {
             $item = $quote->getItemById($itemId);
             if (!$item) {
                 throw new NoSuchEntityException(__('Cart %1 doesn\'t contain item  %2', $cartId, $itemId));
             }
             $productType = $item->getProduct()->getTypeId();
             $buyRequestData = $this->getBuyRequest($productType, $cartItem);
             if (is_object($buyRequestData)) {
                 /** update item product options */
                 /** @var  \Magento\Quote\Model\Quote\Item $cartItem */
                 $cartItem = $quote->updateItem($itemId, $buyRequestData);
             } else {
                 /** update item qty */
                 $item->setData('qty', $qty);
             }
         } else {
             /** add item to shopping cart */
             $product = $this->productRepository->get($cartItem->getSku());
             $productType = $product->getTypeId();
             /** @var  \Magento\Quote\Model\Quote\Item|string $cartItem */
             $cartItem = $quote->addProduct($product, $this->getBuyRequest($productType, $cartItem));
             if (is_string($cartItem)) {
                 throw new \Magento\Framework\Exception\LocalizedException(__($cartItem));
             }
         }
         $this->quoteRepository->save($quote->collectTotals());
     } catch (\Exception $e) {
         if ($e instanceof NoSuchEntityException || $e instanceof LocalizedException) {
             throw $e;
         }
         throw new CouldNotSaveException(__('Could not save quote'));
     }
     $itemId = $cartItem->getId();
     foreach ($quote->getAllItems() as $quoteItem) {
         if ($itemId == $quoteItem->getId()) {
             $cartItem = $this->addProductOptions($productType, $quoteItem);
             return $this->applyCustomOptions($cartItem);
         }
     }
     throw new CouldNotSaveException(__('Could not save quote'));
 }
 /**
  * @param CartInterface $quote
  * @param CartItemInterface $item
  * @return CartItemInterface
  * @throws CouldNotSaveException
  * @throws InputException
  * @throws LocalizedException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function save(CartInterface $quote, CartItemInterface $item)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $qty = $item->getQty();
     if (!is_numeric($qty) || $qty <= 0) {
         throw InputException::invalidFieldValue('qty', $qty);
     }
     $cartId = $item->getQuoteId();
     $itemId = $item->getItemId();
     try {
         /** Update existing item */
         if (isset($itemId)) {
             $currentItem = $quote->getItemById($itemId);
             if (!$currentItem) {
                 throw new NoSuchEntityException(__('Cart %1 does not contain item %2', $cartId, $itemId));
             }
             $productType = $currentItem->getProduct()->getTypeId();
             $buyRequestData = $this->cartItemOptionProcessor->getBuyRequest($productType, $item);
             if (is_object($buyRequestData)) {
                 /** Update item product options */
                 $item = $quote->updateItem($itemId, $buyRequestData);
             } else {
                 if ($item->getQty() !== $currentItem->getQty()) {
                     $currentItem->setQty($qty);
                 }
             }
         } else {
             /** add new item to shopping cart */
             $product = $this->productRepository->get($item->getSku());
             $productType = $product->getTypeId();
             $item = $quote->addProduct($product, $this->cartItemOptionProcessor->getBuyRequest($productType, $item));
             if (is_string($item)) {
                 throw new LocalizedException(__($item));
             }
         }
     } catch (NoSuchEntityException $e) {
         throw $e;
     } catch (LocalizedException $e) {
         throw $e;
     } catch (\Exception $e) {
         throw new CouldNotSaveException(__('Could not save quote'));
     }
     $itemId = $item->getId();
     foreach ($quote->getAllItems() as $quoteItem) {
         /** @var \Magento\Quote\Model\Quote\Item $quoteItem */
         if ($itemId == $quoteItem->getId()) {
             $item = $this->cartItemOptionProcessor->addProductOptions($productType, $quoteItem);
             return $this->cartItemOptionProcessor->applyCustomOptions($item);
         }
     }
     throw new CouldNotSaveException(__('Could not save quote'));
 }
 /**
  * {@inheritdoc}
  */
 public function updateItem($cartId, $itemId, \Magento\Checkout\Service\V1\Data\Cart\Item $data)
 {
     $qty = $data->getQty();
     if (!is_numeric($qty) || $qty <= 0) {
         throw InputException::invalidFieldValue('qty', $qty);
     }
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteRepository->get($cartId);
     $quoteItem = $quote->getItemById($itemId);
     if (!$quoteItem) {
         throw new NoSuchEntityException("Cart {$cartId} doesn't contain item  {$itemId}");
     }
     $quoteItem->setData('qty', $qty);
     try {
         $quote->collectTotals()->save();
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not update quote item');
     }
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function create($entityTypeCode, AttributeSetInterface $attributeSet, $skeletonId)
 {
     /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
     if ($attributeSet->getId() !== null) {
         throw InputException::invalidFieldValue('id', $attributeSet->getId());
     }
     if ($skeletonId == 0) {
         throw InputException::invalidFieldValue('skeletonId', $skeletonId);
     }
     // Make sure that skeleton attribute set is valid (try to load it)
     $this->repository->get($skeletonId);
     try {
         $attributeSet->setEntityTypeId($this->eavConfig->getEntityType($entityTypeCode)->getId());
         $attributeSet->validate();
     } catch (\Exception $exception) {
         throw new InputException(__($exception->getMessage()));
     }
     $this->repository->save($attributeSet);
     $attributeSet->initFromSkeleton($skeletonId);
     return $this->repository->save($attributeSet);
 }
Exemple #6
0
 /**
  * Validate Frontend Input Type
  *
  * @param  string $frontendInput
  * @return void
  * @throws \Magento\Framework\Exception\InputException
  */
 protected function validateFrontendInput($frontendInput)
 {
     /** @var \Magento\Eav\Model\Adminhtml\System\Config\Source\Inputtype\Validator $validator */
     $validator = $this->inputtypeValidatorFactory->create();
     if (!$validator->isValid($frontendInput)) {
         throw InputException::invalidFieldValue('frontend_input', $frontendInput);
     }
 }
 /**
  * {@inheritdoc}
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
 {
     if ($saveOptions) {
         $productOptions = $product->getProductOptions();
     }
     $isDeleteOptions = $product->getIsDeleteOptions();
     $tierPrices = $product->getData('tier_price');
     $productId = $this->resourceModel->getIdBySku($product->getSku());
     $ignoreLinksFlag = $product->getData('ignore_links_flag');
     $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface');
     $productLinks = null;
     if (!$ignoreLinksFlag && $ignoreLinksFlag !== null) {
         $productLinks = $product->getProductLinks();
     }
     $product = $this->initializeProductData($productDataArray, empty($productId));
     if (isset($productDataArray['options'])) {
         if (!empty($productDataArray['options']) || $isDeleteOptions) {
             $this->processOptions($product, $productDataArray['options']);
             $product->setCanSaveCustomOptions(true);
         }
     }
     $this->processLinks($product, $productLinks);
     if (isset($productDataArray['media_gallery_entries'])) {
         $this->processMediaGallery($product, $productDataArray['media_gallery_entries']);
     }
     $validationResult = $this->resourceModel->validate($product);
     if (true !== $validationResult) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult)));
     }
     try {
         if ($saveOptions) {
             $product->setProductOptions($productOptions);
             $product->setCanSaveCustomOptions(true);
         }
         if ($tierPrices !== null) {
             $product->setData('tier_price', $tierPrices);
         }
         $this->resourceModel->save($product);
     } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) {
         throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception);
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product'));
     }
     unset($this->instances[$product->getSku()]);
     unset($this->instancesById[$product->getId()]);
     return $this->get($product->getSku());
 }
Exemple #8
0
 /**
  * {@inheritdoc}
  */
 public function update($id, \Magento\Catalog\Service\V1\Data\Product $product)
 {
     $productModel = $this->productLoader->load($id);
     try {
         $this->productMapper->toModel($product, $productModel);
         $this->initializationHelper->initialize($productModel);
         $this->productTypeManager->processProduct($productModel);
         $productModel->validate();
         $productModel->save();
     } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) {
         throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $productModel->getData($exception->getAttributeCode()), $exception);
     }
     return $productModel->getSku();
 }
Exemple #9
0
 /**
  * {@inheritdoc}
  */
 public function remove($attributeSetId)
 {
     $id = intval($attributeSetId);
     if (0 == $id) {
         throw InputException::invalidFieldValue('id', $id);
     }
     /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
     $attributeSet = $this->setFactory->create()->load($id);
     $defaultAttributeSetId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getDefaultAttributeSetId();
     $loadedData = $attributeSet->getData();
     if (empty($loadedData)) {
         throw NoSuchEntityException::singleField('id', $attributeSetId);
     }
     $productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
     if ($attributeSet->getEntityTypeId() != $productEntityId) {
         throw InputException::invalidFieldValue('id', $attributeSetId);
     }
     if ($attributeSetId == $defaultAttributeSetId) {
         throw new StateException('Default attribute set can not be deleted');
     }
     $attributeSet->delete();
     return true;
 }
 /**
  * Verifies that the tax class model exists and is a customer tax class type.
  *
  * @param int $taxClassId The id of the tax class model to check
  * @param CustomerGroup $group The original group parameters
  * @return void
  * @throws InputException Thrown if the tax class model is invalid
  */
 protected function _verifyTaxClassModel($taxClassId, $group)
 {
     try {
         /* @var TaxClass $taxClassData */
         $taxClassData = $this->_taxClassService->getTaxClass($taxClassId);
     } catch (NoSuchEntityException $e) {
         throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
     }
     if ($taxClassData->getClassType() !== TaxClassServiceInterface::TYPE_CUSTOMER) {
         throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
     }
 }
Exemple #11
0
 /**
  * Verifies that the tax class model exists and is a customer tax class type.
  *
  * @param int $taxClassId The id of the tax class model to check
  * @param \Magento\Customer\Api\Data\GroupInterface $group The original group parameters
  * @return void
  * @throws InputException Thrown if the tax class model is invalid
  */
 protected function _verifyTaxClassModel($taxClassId, $group)
 {
     try {
         /* @var TaxClassInterface $taxClassData */
         $taxClassData = $this->taxClassRepository->get($taxClassId);
     } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
         throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
     }
     if ($taxClassData->getClassType() !== TaxClassManagementInterface::TYPE_CUSTOMER) {
         throw InputException::invalidFieldValue('taxClassId', $group->getTaxClassId());
     }
 }
 /**
  * {@inheritdoc}
  */
 public function types($attributeSetId)
 {
     $attributeSet = $this->setFactory->create()->load($attributeSetId);
     if (!$attributeSet->getId()) {
         throw NoSuchEntityException::singleField('attribute_set_id', $attributeSetId);
     }
     $productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
     if ($attributeSet->getEntityTypeId() != $productEntityId) {
         throw InputException::invalidFieldValue('entity_type_id', $attributeSetId);
     }
     $collection = $this->collectionFactory->create();
     $collection->setAttributeSetFilter($attributeSetId);
     $collection->setFrontendInputTypeFilter('media_image');
     $collection->addStoreLabel($this->storeManager->getStore()->getId());
     return $this->prepareData($collection->getItems());
 }
 /**
  * Validate Frontend Input Type
  *
  * @param  string $frontendInput
  * @return void
  * @throws \Magento\Framework\Exception\InputException
  */
 protected function validateFrontendInput($frontendInput)
 {
     $validator = $this->inputtypeValidatorFactory->create();
     if (!$validator->isValid($frontendInput)) {
         throw InputException::invalidFieldValue('frontend_input', $frontendInput);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function updateTaxClass($taxClassId, TaxClassDataObject $taxClass)
 {
     if ($taxClass->getClassId()) {
         throw new InputException(self::CLASS_ID_NOT_ALLOWED);
     }
     $this->validateTaxClassData($taxClass);
     if (!$taxClassId) {
         throw InputException::invalidFieldValue('taxClassId', $taxClassId);
     }
     $originalTaxClassModel = $this->classModelRegistry->retrieve($taxClassId);
     $taxClassModel = $this->converter->createTaxClassModel($taxClass);
     $taxClassModel->setId($taxClassId);
     /* should not be allowed to switch the tax class type */
     if ($originalTaxClassModel->getClassType() !== $taxClassModel->getClassType()) {
         throw new InputException('Updating classType is not allowed.');
     }
     try {
         $taxClassModel->save();
     } catch (\Exception $e) {
         return false;
     }
     $this->classModelRegistry->registerTaxClass($taxClassModel);
     return true;
 }
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false)
 {
     if ($saveOptions) {
         $productOptions = $product->getProductOptions();
     }
     $groupPrices = $product->getData('group_price');
     $tierPrices = $product->getData('tier_price');
     $productId = $this->resourceModel->getIdBySku($product->getSku());
     $productDataArray = $this->extensibleDataObjectConverter->toNestedArray($product, [], 'Magento\\Catalog\\Api\\Data\\ProductInterface');
     $product = $this->initializeProductData($productDataArray, empty($productId));
     $validationResult = $this->resourceModel->validate($product);
     if (true !== $validationResult) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Invalid product data: %1', implode(',', $validationResult)));
     }
     try {
         if ($saveOptions) {
             $product->setProductOptions($productOptions);
             $product->setCanSaveCustomOptions(true);
         }
         $product->setData('group_price', $groupPrices);
         $product->setData('tier_price', $tierPrices);
         $this->resourceModel->save($product);
     } catch (\Magento\Eav\Model\Entity\Attribute\Exception $exception) {
         throw \Magento\Framework\Exception\InputException::invalidFieldValue($exception->getAttributeCode(), $product->getData($exception->getAttributeCode()), $exception);
     } catch (\Exception $e) {
         throw new \Magento\Framework\Exception\CouldNotSaveException(__('Unable to save product'));
     }
     unset($this->instances[$product->getSku()]);
     unset($this->instancesById[$product->getId()]);
     return $product;
 }