From d4af71b5300d371519b4e3f1d6e3f14b4cdfcea3 Mon Sep 17 00:00:00 2001 From: Michail Slabko Date: Wed, 18 Nov 2015 14:44:15 +0200 Subject: [PATCH] MAGETWO-44971: [GITHUB] Configurable product issues after saving #2226 --- .../Catalog/Controller/Adminhtml/Product/Save.php | 13 ++++++++++--- app/code/Magento/Catalog/Model/Product.php | 2 +- .../Model/Product/Attribute/Backend/Media.php | 3 +++ .../Initialization/Helper/Plugin/Configurable.php | 14 ++++++++++++-- .../Model/Product/Type/Configurable.php | 2 +- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php index a8ba7417aa6c5..4e81b56c453a0 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php @@ -69,6 +69,8 @@ public function execute() $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())); @@ -82,6 +84,8 @@ public function execute() $product->save(); $this->handleImageRemoveError($data, $product->getId()); $productId = $product->getId(); + $productAttributeSetId = $product->getAttributeSetId(); + $productTypeId = $product->getTypeId(); /** * Do copying data to stores @@ -132,10 +136,10 @@ public function execute() return $resultRedirect; } - if ($redirectBack === 'new' && isset($product)) { + if ($redirectBack === 'new') { $resultRedirect->setPath( 'catalog/*/new', - ['set' => $product->getAttributeSetId(), 'type' => $product->getTypeId()] + ['set' => $productAttributeSetId, 'type' => $productTypeId] ); } elseif ($redirectBack === 'duplicate' && isset($newProduct)) { $resultRedirect->setPath( @@ -143,7 +147,10 @@ public function execute() ['id' => $newProduct->getId(), 'back' => null, '_current' => true] ); } elseif ($redirectBack) { - $resultRedirect->setPath('catalog/*/edit', ['id' => $productId, '_current' => true]); + $resultRedirect->setPath( + 'catalog/*/edit', + ['id' => $productId, '_current' => true, 'set' => $productAttributeSetId] + ); } else { $resultRedirect->setPath('catalog/*/', ['store' => $storeId]); } diff --git a/app/code/Magento/Catalog/Model/Product.php b/app/code/Magento/Catalog/Model/Product.php index 34ce0f1f3f4ad..ed3f38357e25d 100644 --- a/app/code/Magento/Catalog/Model/Product.php +++ b/app/code/Magento/Catalog/Model/Product.php @@ -1516,7 +1516,7 @@ public function getMediaGalleryImages() continue; } $image['url'] = $this->getMediaConfig()->getMediaUrl($image['file']); - $image['id'] = $image['value_id']; + $image['id'] = !empty($image['value_id']) ? $image['value_id'] : null; $image['path'] = $directory->getAbsolutePath($this->getMediaConfig()->getMediaPath($image['file'])); $images->addItem(new \Magento\Framework\Object($image)); } diff --git a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Media.php b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Media.php index ed6bed093f369..5c1d1025d4089 100644 --- a/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Media.php +++ b/app/code/Magento/Catalog/Model/Product/Attribute/Backend/Media.php @@ -818,6 +818,9 @@ public function getAffectedFields($object) $images = (array)$object->getData($this->getAttribute()->getName()); $tableName = $this->_getResource()->getMainTable(); foreach ($images['images'] as $value) { + if (empty($value['value_id'])) { + continue; + } $data[$tableName][] = [ 'attribute_id' => $this->getAttribute()->getAttributeId(), 'value_id' => $value['value_id'], diff --git a/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Configurable.php b/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Configurable.php index eb4e88fdc5c06..94fefdc711f3f 100644 --- a/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Configurable.php +++ b/app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Configurable.php @@ -7,8 +7,16 @@ */ namespace Magento\ConfigurableProduct\Controller\Adminhtml\Product\Initialization\Helper\Plugin; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProduct; + class Configurable { + /** @var \Magento\Framework\App\RequestInterface */ + protected $request; + + /** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable */ + protected $productType; + /** * @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable $productType * @param \Magento\Framework\App\RequestInterface $request @@ -35,10 +43,12 @@ public function afterInitialize( \Magento\Catalog\Model\Product $product ) { $attributes = $this->request->getParam('attributes'); - if (!empty($attributes)) { + if ($product->getTypeId() == ConfigurableProduct::TYPE_CODE && !empty($attributes)) { + $setId = $this->request->getPost('new-variations-attribute-set-id'); + $product->setAttributeSetId($setId); $this->productType->setUsedProductAttributeIds($attributes, $product); - $product->setNewVariationsAttributeSetId($this->request->getPost('new-variations-attribute-set-id')); + $product->setNewVariationsAttributeSetId($setId); $associatedProductIds = $this->request->getPost('associated_product_ids', []); $variationsMatrix = $this->request->getParam('variations-matrix', []); if (!empty($variationsMatrix)) { diff --git a/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php b/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php index 25e63a80ce04c..9b66abf461c10 100644 --- a/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php +++ b/app/code/Magento/ConfigurableProduct/Model/Product/Type/Configurable.php @@ -1175,7 +1175,7 @@ protected function _fillSimpleProductData( $product->setStoreId( \Magento\Store\Model\Store::DEFAULT_STORE_ID )->setTypeId( - $postData['weight'] ? \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE : \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL + \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE )->setAttributeSetId( $parentProduct->getNewVariationsAttributeSetId() );