public function testSetUsedProductAttributeIds() { $testConfigurable = $this->_getAttributeByCode('test_configurable'); $this->_model->setUsedProductAttributeIds([$testConfigurable->getId()], $this->_product); $attributes = $this->_product->getData('_cache_instance_configurable_attributes'); $this->assertArrayHasKey(0, $attributes); $this->assertInstanceOf('Magento\\ConfigurableProduct\\Model\\Product\\Type\\Configurable\\Attribute', $attributes[0]); $this->assertSame($testConfigurable, $attributes[0]->getProductAttribute()); }
/** * Initialize data for configurable product * * @param \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject * @param \Magento\Catalog\Model\Product $product * * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterInitialize(\Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper $subject, \Magento\Catalog\Model\Product $product) { $attributes = $this->request->getParam('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($setId); $associatedProductIds = $this->request->getPost('associated_product_ids', []); $variationsMatrix = $this->request->getParam('variations-matrix', []); if (!empty($variationsMatrix)) { $generatedProductIds = $this->variationHandler->generateSimpleProducts($product, $variationsMatrix); $associatedProductIds = array_merge($associatedProductIds, $generatedProductIds); } $product->setAssociatedProductIds(array_filter($associatedProductIds)); $product->setCanSaveConfigurableAttributes((bool) $this->request->getPost('affect_configurable_product_attributes')); } return $product; }
/** * @param \Magento\Catalog\Controller\Adminhtml\Product\Builder $subject * @param callable $proceed * @param \Magento\Framework\App\RequestInterface $request * * @return \Magento\Catalog\Model\Product * @SuppressWarnings(PHPMD.UnusedFormalParameter) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundBuild(\Magento\Catalog\Controller\Adminhtml\Product\Builder $subject, \Closure $proceed, \Magento\Framework\App\RequestInterface $request) { $product = $proceed($request); if ($request->has('attributes')) { $attributes = $request->getParam('attributes'); if (!empty($attributes)) { $product->setTypeId(\Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE); $this->configurableType->setUsedProductAttributeIds($attributes, $product); } else { $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE); } } // Required attributes of simple product for configurable creation if ($request->getParam('popup') && ($requiredAttributes = $request->getParam('required'))) { $requiredAttributes = explode(",", $requiredAttributes); foreach ($product->getAttributes() as $attribute) { if (in_array($attribute->getId(), $requiredAttributes)) { $attribute->setIsRequired(1); } } } if ($request->getParam('popup') && $request->getParam('product') && !is_array($request->getParam('product')) && $request->getParam('id', false) === false) { $configProduct = $this->productFactory->create(); $configProduct->setStoreId(0)->load($request->getParam('product'))->setTypeId($request->getParam('type')); $data = []; foreach ($configProduct->getTypeInstance()->getEditableAttributes($configProduct) as $attribute) { /* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ if (!$attribute->getIsUnique() && $attribute->getFrontend()->getInputType() != 'gallery' && $attribute->getAttributeCode() != 'required_options' && $attribute->getAttributeCode() != 'has_options' && $attribute->getAttributeCode() != $configProduct->getIdFieldName()) { $data[$attribute->getAttributeCode()] = $configProduct->getData($attribute->getAttributeCode()); } } $product->addData($data); $product->setWebsiteIds($configProduct->getWebsiteIds()); } return $product; }