/** * 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; }
/** * 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; }
/** * @covers Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testInitialize() { $this->websiteMock->expects($this->once())->method('getId')->will($this->returnValue($this->websiteId)); $this->storeMock->expects($this->once())->method('getWebsite')->will($this->returnValue($this->websiteMock)); $this->storeManagerMock->expects($this->once())->method('getStore')->with(true)->will($this->returnValue($this->storeMock)); $this->jsHelperMock = $this->getMock('\\Magento\\Backend\\Helper\\Js', [], [], '', false); $this->helper = new Helper($this->requestMock, $this->storeManagerMock, $this->stockFilterMock, $this->productLinksMock, $this->jsHelperMock); $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('options_use_default')->will($this->returnValue(true)); $this->requestMock->expects($this->at(4))->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)); }
/** * 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(); }
/** * {@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); } }
/** * 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; }
/** * {@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(); }
/** * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testInitialize() { $this->websiteMock->expects($this->once())->method('getId')->will($this->returnValue($this->websiteId)); $this->storeMock->expects($this->once())->method('getWebsite')->will($this->returnValue($this->websiteMock)); $this->storeManagerMock->expects($this->once())->method('getStore')->with(true)->will($this->returnValue($this->storeMock)); $this->jsHelperMock = $this->getMock('\\Magento\\Backend\\Helper\\Js', [], [], '', false); $customOptionFactory = $this->getMockBuilder('Magento\\Catalog\\Api\\Data\\ProductCustomOptionInterfaceFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $customOption = $this->getMockBuilder('Magento\\Catalog\\Api\\Data\\ProductCustomOptionInterface')->disableOriginalConstructor()->getMockForAbstractClass(); $this->helper = new Helper($this->requestMock, $this->storeManagerMock, $this->stockFilterMock, $this->productLinksMock, $this->jsHelperMock, $this->dateFilterMock, $customOptionFactory, $this->productLinkFactoryMock, $this->productRepositoryMock); $productData = ['stock_data' => ['stock_data'], 'options' => ['option1' => ['is_delete' => true], 'option2' => ['is_delete' => false]]]; $customOptionFactory->expects($this->once())->method('create')->with(['data' => ['is_delete' => false]])->willReturn($customOption); $customOption->expects($this->once())->method('setProductSku'); $customOption->expects($this->once())->method('setOptionId'); $attributeNonDate = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], [], '', false); $attributeDate = $this->getMock('Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute', [], [], '', false); $attributeNonDateBackEnd = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\DefaultBackend', [], [], '', false); $attributeDateBackEnd = $this->getMock('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\Datetime', [], [], '', false); $attributeNonDate->expects($this->any())->method('getBackend')->will($this->returnValue($attributeNonDateBackEnd)); $attributeDate->expects($this->any())->method('getBackend')->will($this->returnValue($attributeDateBackEnd)); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]); $attributeNonDateBackEnd->expects($this->any())->method('getType')->will($this->returnValue('non-datetime')); $attributeDateBackEnd->expects($this->any())->method('getType')->will($this->returnValue('datetime')); $attributesArray = [$attributeNonDate, $attributeDate]; $useDefaults = ['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('options_use_default')->will($this->returnValue(true)); $this->requestMock->expects($this->at(4))->method('getPost')->with('affect_product_custom_options')->will($this->returnValue(true)); $this->stockFilterMock->expects($this->once())->method('filter')->with(['stock_data'])->will($this->returnValue(['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->any())->method('getProductLinks')->willReturn([]); $this->productMock->expects($this->once())->method('lockAttribute')->with('media'); $this->productMock->expects($this->once())->method('getAttributes')->will($this->returnValue($attributesArray)); $productData['category_ids'] = []; $productData['website_ids'] = []; $this->productMock->expects($this->once())->method('addData')->with($productData); $this->productMock->expects($this->once())->method('getSku')->willReturn('sku'); $this->productMock->expects($this->once())->method('setWebsiteIds')->with([$this->websiteId]); $this->productMock->expects($this->any())->method('getOptionsReadOnly')->will($this->returnValue(false)); $this->productMock->expects($this->once())->method('setOptions')->with([$customOption]); $this->productMock->expects($this->once())->method('setCanSaveCustomOptions')->with(true); $this->assertEquals($this->productMock, $this->helper->initialize($this->productMock)); }
/** * 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; }
/** * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testInitialize() { $this->websiteMock->expects($this->once())->method('getId')->willReturn($this->websiteId); $this->storeMock->expects($this->once())->method('getWebsite')->willReturn($this->websiteMock); $this->storeManagerMock->expects($this->once())->method('getStore')->with(true)->willReturn($this->storeMock); $this->customOptionMock->expects($this->once())->method('setProductSku'); $this->customOptionMock->expects($this->once())->method('setOptionId'); $optionsData = ['option1' => ['is_delete' => true, 'name' => 'name1', 'price' => 'price1'], 'option2' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1']]; $productData = ['stock_data' => ['stock_data'], 'options' => $optionsData]; $attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)->disableOriginalConstructor()->getMock(); $attributeDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)->disableOriginalConstructor()->getMock(); $attributeNonDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class)->disableOriginalConstructor()->getMock(); $attributeDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class)->disableOriginalConstructor()->getMock(); $attributeNonDate->expects($this->any())->method('getBackend')->willReturn($attributeNonDateBackEnd); $attributeDate->expects($this->any())->method('getBackend')->willReturn($attributeDateBackEnd); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]); $attributeNonDateBackEnd->expects($this->any())->method('getType')->willReturn('non-datetime'); $attributeDateBackEnd->expects($this->any())->method('getType')->willReturn('datetime'); $attributesArray = [$attributeNonDate, $attributeDate]; $useDefaults = ['attributeCode1', 'attributeCode2']; $this->requestMock->expects($this->at(0))->method('getPost')->with('product')->willReturn($productData); $this->requestMock->expects($this->at(1))->method('getPost')->with('use_default')->willReturn($useDefaults); $this->linkResolverMock->expects($this->once())->method('getLinks')->willReturn([]); $this->stockFilterMock->expects($this->once())->method('filter')->with(['stock_data'])->willReturn(['stock_data']); $this->storeManagerMock->expects($this->once())->method('hasSingleStore')->willReturn(true); $this->productMock->expects($this->once())->method('isLockedAttribute')->with('media')->willReturn(true); $this->productMock->expects($this->once())->method('unlockAttribute')->with('media'); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]); $this->productMock->expects($this->once())->method('lockAttribute')->with('media'); $this->productMock->expects($this->once())->method('getAttributes')->willReturn($attributesArray); $productData['category_ids'] = []; $productData['website_ids'] = []; unset($productData['options']); $this->productMock->expects($this->once())->method('addData')->with($productData); $this->productMock->expects($this->once())->method('getSku')->willReturn('sku'); $this->productMock->expects($this->once())->method('setWebsiteIds')->with([$this->websiteId]); $this->productMock->expects($this->any())->method('getOptionsReadOnly')->willReturn(false); $this->customOptionFactoryMock->expects($this->any())->method('create')->with(['data' => $optionsData['option2']])->willReturn($this->customOptionMock); $this->productMock->expects($this->once())->method('setOptions')->with([$this->customOptionMock]); $this->assertEquals($this->productMock, $this->helper->initialize($this->productMock)); }
/** * 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 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()); }