/** * @param array $inputStockData * @param array $outputStockData * * @covers Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter::filter * @dataProvider filterDataProvider */ public function testFilter(array $inputStockData, array $outputStockData) { if (isset($inputStockData['use_config_manage_stock']) && $inputStockData['use_config_manage_stock'] === 1) { $this->stockConfiguration->expects($this->once())->method('getManageStock')->will($this->returnValue($outputStockData['manage_stock'])); } $this->assertEquals($outputStockData, $this->stockDataFilter->filter($inputStockData)); }
/** * Initialize product before saving * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\Product */ public function initialize(\Magento\Catalog\Model\Product $product) { $productData = $this->request->getPost('product'); if ($productData) { $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : array(); $productData['stock_data'] = $this->stockFilter->filter($stockData); } foreach (array('category_ids', 'website_ids') as $field) { if (!isset($productData[$field])) { $productData[$field] = array(); } } $wasLockedMedia = false; if ($product->isLockedAttribute('media')) { $product->unlockAttribute('media'); $wasLockedMedia = true; } $product->addData($productData); if ($wasLockedMedia) { $product->lockAttribute('media'); } if ($this->storeManager->hasSingleStore()) { $product->setWebsiteIds(array($this->storeManager->getStore(true)->getWebsite()->getId())); } /** * Create Permanent Redirect for old URL key */ if ($product->getId() && isset($productData['url_key_create_redirect'])) { $product->setData('save_rewrites_history', (bool) $productData['url_key_create_redirect']); } /** * Check "Use Default Value" checkboxes values */ $useDefaults = $this->request->getPost('use_default'); if ($useDefaults) { foreach ($useDefaults as $attributeCode) { $product->setData($attributeCode, false); } } $links = $this->request->getPost('links'); $links = is_array($links) ? $links : array(); $linkTypes = array('related', 'upsell', 'crosssell'); foreach ($linkTypes as $type) { if (isset($links[$type])) { $links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]); } } $product = $this->productLinks->initializeLinks($product, $links); /** * Initialize product options */ if (isset($productData['options']) && !$product->getOptionsReadonly()) { // mark custom options that should to fall back to default value $options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default')); $product->setProductOptions($options); } $product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly()); return $product; }
/** * Initialize product before saving * * @param \Magento\Catalog\Model\Product $product * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function initialize(\Magento\Catalog\Model\Product $product) { $productData = $this->request->getPost('product'); unset($productData['custom_attributes']); unset($productData['extension_attributes']); if ($productData) { $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : []; $productData['stock_data'] = $this->stockFilter->filter($stockData); } foreach (['category_ids', 'website_ids'] as $field) { if (!isset($productData[$field])) { $productData[$field] = []; } } $wasLockedMedia = false; if ($product->isLockedAttribute('media')) { $product->unlockAttribute('media'); $wasLockedMedia = true; } $dateFieldFilters = []; $attributes = $product->getAttributes(); foreach ($attributes as $attrKey => $attribute) { if ($attribute->getBackend()->getType() == 'datetime') { if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') { $dateFieldFilters[$attrKey] = $this->dateFilter; } } } $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData); $productData = $inputFilter->getUnescaped(); $product->addData($productData); if ($wasLockedMedia) { $product->lockAttribute('media'); } if ($this->storeManager->hasSingleStore()) { $product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]); } /** * Check "Use Default Value" checkboxes values */ $useDefaults = $this->request->getPost('use_default'); if ($useDefaults) { foreach ($useDefaults as $attributeCode) { $product->setData($attributeCode, false); } } $links = $this->request->getPost('links'); $links = is_array($links) ? $links : []; $linkTypes = ['related', 'upsell', 'crosssell']; foreach ($linkTypes as $type) { if (isset($links[$type])) { $links[$type] = $this->jsHelper->decodeGridSerializedInput($links[$type]); } } $product = $this->productLinks->initializeLinks($product, $links); /** * Initialize product options */ if (isset($productData['options']) && !$product->getOptionsReadonly()) { // mark custom options that should to fall back to default value $options = $this->mergeProductOptions($productData['options'], $this->request->getPost('options_use_default')); $product->setProductOptions($options); } $product->setCanSaveCustomOptions((bool) $this->request->getPost('affect_product_custom_options') && !$product->getOptionsReadonly()); return $product; }