Exemplo n.º 1
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;
 }
Exemplo 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;
 }
Exemplo n.º 3
0
 /**
  * @param string $exceptionType
  * @return void
  * @dataProvider exceptionTypeDataProvider
  */
 public function testExecuteSetsProductDataToSessionAndRedirectsToNewActionOnError($exceptionType)
 {
     $productData = ['product' => ['name' => 'test-name']];
     $this->request->expects($this->any())->method('getPostValue')->willReturn($productData);
     $this->initializationHelper->expects($this->any())->method('initialize')->willReturn($this->product);
     $this->product->expects($this->any())->method('getSku')->willThrowException(new $exceptionType(__('message')));
     $this->resultRedirect->expects($this->once())->method('setPath')->with('catalog/*/new');
     $this->action->execute();
 }
Exemplo n.º 4
0
 public function testExecuteObtainsProductDataFromSession()
 {
     $this->action->getRequest()->expects($this->any())->method('getParam')->willReturn(true);
     $this->action->getRequest()->expects($this->any())->method('getFullActionName')->willReturn('catalog_product_new');
     $productData = ['name' => 'test-name', 'stock_data' => null];
     $this->session->expects($this->any())->method('getProductData')->willReturn(['product' => $productData]);
     $this->initializationHelper->expects($this->once())->method('initializeFromData')->with($this->product, $productData)->willReturn($this->product);
     $this->action->execute();
 }
Exemplo n.º 5
0
 public function testExecute()
 {
     $product = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['_wakeup', 'getId'])->getMock();
     $layout = $this->getMock('\\Magento\\Framework\\View\\LayoutInterface');
     $block = $this->getMockBuilder('Magento\\Bundle\\Block\\Adminhtml\\Catalog\\Product\\Edit\\Tab\\Bundle')->disableOriginalConstructor()->setMethods(['setIndex', 'toHtml'])->getMock();
     $this->productBuilder->expects($this->once())->method('build')->with($this->request)->willReturn($product);
     $this->initializationHelper->expects($this->any())->method('initialize')->willReturn($product);
     $this->response->expects($this->once())->method('setBody')->willReturnSelf();
     $this->view->expects($this->once())->method('getLayout')->willReturn($layout);
     $layout->expects($this->once())->method('createBlock')->willReturn($block);
     $block->expects($this->once())->method('toHtml')->willReturnSelf();
     $this->controller->execute();
 }
Exemplo n.º 6
0
 /**
  * @param array $productOptions
  * @param array $defaultOptions
  * @param array $expectedResults
  * @dataProvider mergeProductOptionsDataProvider
  */
 public function testMergeProductOptions($productOptions, $defaultOptions, $expectedResults)
 {
     $this->jsHelperMock = $this->getMock('\\Magento\\Backend\\Helper\\Js', [], [], '', false);
     $this->helper = new Helper($this->requestMock, $this->storeManagerMock, $this->stockFilterMock, $this->productLinksMock, $this->jsHelperMock, $this->dateFilterMock);
     $result = $this->helper->mergeProductOptions($productOptions, $defaultOptions);
     $this->assertEquals($expectedResults, $result);
 }
Exemplo n.º 7
0
 public function testInitializeFromData()
 {
     $productData = ['name' => 'test-name', 'stock_data' => ['use_config_manage_stock' => 0]];
     $this->request->expects($this->any())->method('getPost')->willReturnMap([['product', [], $productData]]);
     $this->initializationHelper->expects($this->once())->method('initializeFromData')->with($this->product, $productData)->willReturn($this->product);
     $this->action->execute();
 }
Exemplo n.º 8
0
 /**
  * @covers Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize
  */
 public function testInitialize()
 {
     $productData = array('stock_data' => array('stock_data'), 'url_key_create_redirect' => true, 'options' => array('option1', 'option2'));
     $useDefaults = array('attributeCode1', 'attributeCode2');
     $this->requestMock->expects($this->at(0))->method('getPost')->with('product')->will($this->returnValue($productData));
     $this->requestMock->expects($this->at(1))->method('getPost')->with('use_default')->will($this->returnValue($useDefaults));
     $this->requestMock->expects($this->at(3))->method('getPost')->with('affect_product_custom_options')->will($this->returnValue(true));
     $this->stockFilterMock->expects($this->once())->method('filter')->with(array('stock_data'))->will($this->returnValue(array('stock_data')));
     $this->storeManagerMock->expects($this->once())->method('hasSingleStore')->will($this->returnValue(true));
     $this->productLinksMock->expects($this->once())->method('initializeLinks')->with($this->productMock)->will($this->returnValue($this->productMock));
     $this->productMock->expects($this->once())->method('isLockedAttribute')->with('media')->will($this->returnValue(true));
     $this->productMock->expects($this->once())->method('unlockAttribute')->with('media');
     $this->productMock->expects($this->once())->method('lockAttribute')->with('media');
     $productData['category_ids'] = array();
     $productData['website_ids'] = array();
     $this->productMock->expects($this->once())->method('addData')->with($productData);
     $this->productMock->expects($this->once())->method('getId')->will($this->returnValue(true));
     $this->productMock->expects($this->once())->method('setWebsiteIds')->with(array($this->websiteId));
     $this->productMock->expects($this->at(6))->method('setData')->with('save_rewrites_history', true);
     $this->productMock->expects($this->at(7))->method('setData')->with('attributeCode1', false);
     $this->productMock->expects($this->at(8))->method('setData')->with('attributeCode2', false);
     $this->productMock->expects($this->any())->method('getOptionsReadOnly')->will($this->returnValue(false));
     $this->productMock->expects($this->once())->method('setProductOptions')->with($productData['options']);
     $this->productMock->expects($this->once())->method('setCanSaveCustomOptions')->with(true);
     $this->assertEquals($this->productMock, $this->helper->initialize($this->productMock));
 }
Exemplo n.º 9
0
 /**
  * Generate product variations matrix
  *
  * @return void
  */
 public function execute()
 {
     $this->_saveAttributeOptions();
     $this->getRequest()->setParam('variations-matrix', array());
     $this->initializationHelper->initialize($this->productBuilder->build($this->getRequest()));
     $this->_view->loadLayout();
     $this->_view->renderLayout();
 }
Exemplo n.º 10
0
 /**
  * @param array $productOptions
  * @param array $defaultOptions
  * @param array $expectedResults
  * @dataProvider mergeProductOptionsDataProvider
  */
 public function testMergeProductOptions($productOptions, $defaultOptions, $expectedResults)
 {
     $this->jsHelperMock = $this->getMock('\\Magento\\Backend\\Helper\\Js', [], [], '', false);
     $customOptionFactory = $this->getMockBuilder('Magento\\Catalog\\Api\\Data\\ProductCustomOptionInterfaceFactory')->disableOriginalConstructor()->getMockForAbstractClass();
     $this->helper = new Helper($this->requestMock, $this->storeManagerMock, $this->stockFilterMock, $this->productLinksMock, $this->jsHelperMock, $this->dateFilterMock, $customOptionFactory, $this->productLinkFactoryMock, $this->productRepositoryMock);
     $result = $this->helper->mergeProductOptions($productOptions, $defaultOptions);
     $this->assertEquals($expectedResults, $result);
 }
Exemplo n.º 11
0
 /**
  * {@inheritdoc}
  */
 public function mergeProductOptions($productOptions, $overwriteOptions)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'mergeProductOptions');
     if (!$pluginInfo) {
         return parent::mergeProductOptions($productOptions, $overwriteOptions);
     } else {
         return $this->___callPlugins('mergeProductOptions', func_get_args(), $pluginInfo);
     }
 }
Exemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function initialize(\Magento\Catalog\Model\Product $product)
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'initialize');
     if (!$pluginInfo) {
         return parent::initialize($product);
     } else {
         return $this->___callPlugins('initialize', func_get_args(), $pluginInfo);
     }
 }
Exemplo n.º 13
0
 /**
  * Merge data from DB and updates from request
  *
  * @param array $productData
  * @param bool $createNew
  * @return \Magento\Catalog\Api\Data\ProductInterface|Product
  * @throws NoSuchEntityException
  */
 protected function initializeProductData(array $productData, $createNew)
 {
     if ($createNew) {
         $product = $this->productFactory->create();
     } else {
         $product = $this->get($productData['sku']);
         $this->initializationHelper->initialize($product);
     }
     foreach ($productData as $key => $value) {
         $product->setData($key, $value);
     }
     return $product;
 }
Exemplo n.º 14
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();
 }
Exemplo n.º 15
0
 /**
  * Merge data from DB and updates from request
  *
  * @param array $productData
  * @param bool $createNew
  * @return \Magento\Catalog\Api\Data\ProductInterface|Product
  * @throws NoSuchEntityException
  */
 protected function initializeProductData(array $productData, $createNew)
 {
     if ($createNew) {
         $product = $this->productFactory->create();
         if ($this->storeManager->hasSingleStore()) {
             $product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
         }
     } else {
         unset($this->instances[$productData['sku']]);
         $product = $this->get($productData['sku']);
         $this->initializationHelper->initialize($product);
     }
     foreach ($productData as $key => $value) {
         $product->setData($key, $value);
     }
     return $product;
 }
Exemplo n.º 16
0
 /**
  * @param array $productOptions
  * @param array $defaultOptions
  * @param array $expectedResults
  * @dataProvider mergeProductOptionsDataProvider
  */
 public function testMergeProductOptions($productOptions, $defaultOptions, $expectedResults)
 {
     $result = $this->helper->mergeProductOptions($productOptions, $defaultOptions);
     $this->assertEquals($expectedResults, $result);
 }
Exemplo n.º 17
0
 /**
  * @return void
  */
 public function executeInternal()
 {
     $product = $this->initializationHelper->initialize($this->productBuilder->build($this->getRequest()));
     $this->getResponse()->setBody($this->_view->getLayout()->createBlock('Magento\\Bundle\\Block\\Adminhtml\\Catalog\\Product\\Edit\\Tab\\Bundle', 'admin.product.bundle.items')->setProductId($product->getId())->toHtml());
 }
Exemplo n.º 18
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;
 }