Ejemplo n.º 1
0
 public function testCopy()
 {
     $this->productMock->expects($this->atLeastOnce())->method('getWebsiteIds');
     $this->productMock->expects($this->atLeastOnce())->method('getCategoryIds');
     $resourceMock = $this->getMock('\\Magento\\Catalog\\Model\\ResourceModel\\Product', [], [], '', false);
     $this->productMock->expects($this->once())->method('getResource')->will($this->returnValue($resourceMock));
     $duplicateMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['__wakeup', 'setData', 'setIsDuplicate', 'setOriginalId', 'setStatus', 'setCreatedAt', 'setUpdatedAt', 'setId', 'setStoreId', 'getEntityId', 'save', 'setUrlKey', 'getUrlKey'], [], '', false);
     $this->productFactoryMock->expects($this->once())->method('create')->will($this->returnValue($duplicateMock));
     $duplicateMock->expects($this->once())->method('setIsDuplicate')->with(true);
     $duplicateMock->expects($this->once())->method('setOriginalId')->with(1);
     $duplicateMock->expects($this->once())->method('setStatus')->with(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
     $duplicateMock->expects($this->once())->method('setCreatedAt')->with(null);
     $duplicateMock->expects($this->once())->method('setUpdatedAt')->with(null);
     $duplicateMock->expects($this->once())->method('setId')->with(null);
     $duplicateMock->expects($this->once())->method('setStoreId')->with(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
     $duplicateMock->expects($this->once())->method('setData')->with('product data');
     $this->copyConstructorMock->expects($this->once())->method('build')->with($this->productMock, $duplicateMock);
     $duplicateMock->expects($this->once())->method('getUrlKey')->willReturn('urk-key-1');
     $duplicateMock->expects($this->once())->method('setUrlKey')->with('urk-key-2');
     $duplicateMock->expects($this->once())->method('save');
     $duplicateMock->expects($this->any())->method('getEntityId')->willReturn(2);
     $this->optionRepositoryMock->expects($this->once())->method('duplicate')->with($this->productMock, $duplicateMock);
     $resourceMock->expects($this->once())->method('duplicate')->with(1, 2);
     $this->assertEquals($duplicateMock, $this->_model->copy($this->productMock));
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 /**
  * Create product duplicate
  *
  * @return void
  */
 public function execute()
 {
     $product = $this->productBuilder->build($this->getRequest());
     try {
         $newProduct = $this->productCopier->copy($product);
         $this->messageManager->addSuccess(__('You duplicated the product.'));
         $this->_redirect('catalog/*/edit', array('_current' => true, 'id' => $newProduct->getId()));
     } catch (\Exception $e) {
         $this->_objectManager->get('Magento\\Framework\\Logger')->logException($e);
         $this->messageManager->addError($e->getMessage());
         $this->_redirect('catalog/*/edit', array('_current' => true));
     }
 }
Ejemplo n.º 4
0
 /**
  * Create product duplicate
  *
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultRedirectFactory->create();
     $product = $this->productBuilder->build($this->getRequest());
     try {
         $newProduct = $this->productCopier->copy($product);
         $this->messageManager->addSuccess(__('You duplicated the product.'));
         $resultRedirect->setPath('catalog/*/edit', ['_current' => true, 'id' => $newProduct->getId()]);
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
         $this->messageManager->addError($e->getMessage());
         $resultRedirect->setPath('catalog/*/edit', ['_current' => true]);
     }
     return $resultRedirect;
 }
Ejemplo n.º 5
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;
 }