Esempio n. 1
0
 /**
  * Save product action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $storeId = $this->getRequest()->getParam('store');
     $redirectBack = $this->getRequest()->getParam('back', false);
     $productId = $this->getRequest()->getParam('id');
     $resultRedirect = $this->resultRedirectFactory->create();
     $data = $this->getRequest()->getPostValue();
     if ($data) {
         try {
             $product = $this->initializationHelper->initialize($this->productBuilder->build($this->getRequest()));
             $this->productTypeManager->processProduct($product);
             if (isset($data['product'][$product->getIdFieldName()])) {
                 throw new \Magento\Framework\Exception\LocalizedException(__('Unable to save product'));
             }
             $originalSku = $product->getSku();
             $product->save();
             $productId = $product->getId();
             /**
              * Do copying data to stores
              */
             if (isset($data['copy_to_stores'])) {
                 foreach ($data['copy_to_stores'] as $storeTo => $storeFrom) {
                     $this->_objectManager->create('Magento\\Catalog\\Model\\Product')->setStoreId($storeFrom)->load($productId)->setStoreId($storeTo)->save();
                 }
             }
             $this->messageManager->addSuccess(__('You saved the product.'));
             if ($product->getSku() != $originalSku) {
                 $this->messageManager->addNotice(__('SKU for product %1 has been changed to %2.', $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($product->getName()), $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($product->getSku())));
             }
             $this->_eventManager->dispatch('controller_action_catalog_product_save_entity_after', ['controller' => $this]);
             if ($redirectBack === 'duplicate') {
                 $newProduct = $this->productCopier->copy($product);
                 $this->messageManager->addSuccess(__('You duplicated the product.'));
             }
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
             $this->_session->setProductData($data);
             $redirectBack = $productId ? true : 'new';
         } catch (\Exception $e) {
             $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
             $this->messageManager->addError($e->getMessage());
             $this->_session->setProductData($data);
             $redirectBack = $productId ? true : 'new';
         }
     } else {
         $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
         $this->messageManager->addError('No data to save');
         return $resultRedirect;
     }
     if ($redirectBack === 'new' && isset($product)) {
         $resultRedirect->setPath('catalog/*/new', ['set' => $product->getAttributeSetId(), 'type' => $product->getTypeId()]);
     } elseif ($redirectBack === 'duplicate' && isset($newProduct)) {
         $resultRedirect->setPath('catalog/*/edit', ['id' => $newProduct->getId(), 'back' => null, '_current' => true]);
     } elseif ($redirectBack) {
         $resultRedirect->setPath('catalog/*/edit', ['id' => $productId, '_current' => true]);
     } else {
         $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
     }
     return $resultRedirect;
 }
Esempio n. 2
0
 /**
  * Post new product
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $resultRedirect = $this->resultRedirectFactory->create();
     $request = $this->getRequest();
     $post = $request->getPostValue();
     if (!$post) {
         $resultRedirect->setPath('*/*/');
         return $resultRedirect;
     }
     /** @var \TurgayOzgur\C2C\Model\ProductFromCustomer $product */
     $product = $this->initializationHelper->initialize($this->productBuilder->build($request));
     $this->productTypeManager->processProduct($product);
     $imagePaths = $request->getParam('image');
     if ($imagePaths) {
         $isFirst = true;
         foreach ($imagePaths as $path) {
             $product->addImageToMediaGallery($path, $isFirst ? ['image', 'small_image', 'thumbnail'] : null, false, false);
             $isFirst = false;
         }
     }
     $product->setTypeId('simple');
     // Simple.
     $product->setAttributeSetId(4);
     // Default.
     $product->setStatus(1);
     // 1 - Enable, 2 - Disable.
     $product->setVisibility(4);
     $product->setSku($product->getName());
     $product->setCustomerId($this->_getSession()->getCustomerId());
     $product->save();
     $resultRedirect->setPath('C2C/customerproducts');
     return $resultRedirect;
 }
Esempio n. 3
0
 /**
  * @param bool $isVirtual
  * @param string $currentTypeId
  * @param string $expectedTypeId
  * @dataProvider processProductDataProvider
  */
 public function testProcessProduct($isVirtual, $currentTypeId, $expectedTypeId)
 {
     $this->productMock->expects($this->any())->method('hasIsVirtual')->will($this->returnValue($isVirtual));
     $this->productMock->expects($this->once())->method('getTypeId')->will($this->returnValue($currentTypeId));
     $this->productMock->expects($this->once())->method('setTypeInstance')->with(null);
     $this->productMock->expects($this->once())->method('setTypeId')->with($expectedTypeId);
     $this->model->processProduct($this->productMock);
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function processProduct(\Magento\Catalog\Model\Product $product)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'processProduct');
     if (!$pluginInfo) {
         return parent::processProduct($product);
     } else {
         return $this->___callPlugins('processProduct', func_get_args(), $pluginInfo);
     }
 }
Esempio n. 5
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();
 }
Esempio n. 6
0
 /**
  * Save product action
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $storeId = $this->getRequest()->getParam('store', 0);
     $store = $this->getStoreManager()->getStore($storeId);
     $this->getStoreManager()->setCurrentStore($store->getCode());
     $redirectBack = $this->getRequest()->getParam('back', false);
     $productId = $this->getRequest()->getParam('id');
     $resultRedirect = $this->resultRedirectFactory->create();
     $data = $this->getRequest()->getPostValue();
     $productAttributeSetId = $this->getRequest()->getParam('set');
     $productTypeId = $this->getRequest()->getParam('type');
     if ($data) {
         try {
             $product = $this->initializationHelper->initialize($this->productBuilder->build($this->getRequest()));
             $this->productTypeManager->processProduct($product);
             if (isset($data['product'][$product->getIdFieldName()])) {
                 throw new \Magento\Framework\Exception\LocalizedException(__('Unable to save product'));
             }
             $originalSku = $product->getSku();
             $product->save();
             $this->handleImageRemoveError($data, $product->getId());
             $this->getCategoryLinkManagement()->assignProductToCategories($product->getSku(), $product->getCategoryIds());
             $productId = $product->getEntityId();
             $productAttributeSetId = $product->getAttributeSetId();
             $productTypeId = $product->getTypeId();
             $this->copyToStores($data, $productId);
             $this->messageManager->addSuccess(__('You saved the product.'));
             $this->getDataPersistor()->clear('catalog_product');
             if ($product->getSku() != $originalSku) {
                 $this->messageManager->addNotice(__('SKU for product %1 has been changed to %2.', $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($product->getName()), $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($product->getSku())));
             }
             $this->_eventManager->dispatch('controller_action_catalog_product_save_entity_after', ['controller' => $this, 'product' => $product]);
             if ($redirectBack === 'duplicate') {
                 $newProduct = $this->productCopier->copy($product);
                 $this->messageManager->addSuccess(__('You duplicated the product.'));
             }
         } catch (\Magento\Framework\Exception\LocalizedException $e) {
             $this->messageManager->addError($e->getMessage());
             $this->getDataPersistor()->set('catalog_product', $data);
             $redirectBack = $productId ? true : 'new';
         } catch (\Exception $e) {
             $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
             $this->messageManager->addError($e->getMessage());
             $this->getDataPersistor()->set('catalog_product', $data);
             $redirectBack = $productId ? true : 'new';
         }
     } else {
         $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
         $this->messageManager->addError('No data to save');
         return $resultRedirect;
     }
     if ($redirectBack === 'new') {
         $resultRedirect->setPath('catalog/*/new', ['set' => $productAttributeSetId, 'type' => $productTypeId]);
     } elseif ($redirectBack === 'duplicate' && isset($newProduct)) {
         $resultRedirect->setPath('catalog/*/edit', ['id' => $newProduct->getEntityId(), 'back' => null, '_current' => true]);
     } elseif ($redirectBack) {
         $resultRedirect->setPath('catalog/*/edit', ['id' => $productId, '_current' => true, 'set' => $productAttributeSetId]);
     } else {
         $resultRedirect->setPath('catalog/*/', ['store' => $storeId]);
     }
     return $resultRedirect;
 }
 /**
  * @return void
  */
 public function testProcessProductWithWrongTypeId()
 {
     $this->productMock->expects($this->once())->method('getTypeId')->will($this->returnValue('wrong-type'));
     $this->weightResolver->expects($this->never())->method('resolveProductHasWeight');
     $this->model->processProduct($this->productMock);
 }