/** * Prepare attribute set comprising all selected configurable attributes * * @param \Magento\Catalog\Model\Product $product * * @return void */ protected function prepareAttributeSetToBeBaseForNewVariations(\Magento\Catalog\Model\Product $product) { $attributes = $this->configurableProduct->getUsedProductAttributes($product); $attributeSetId = $product->getNewVariationsAttributeSetId(); /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */ $attributeSet = $this->attributeSetFactory->create()->load($attributeSetId); $attributeSet->addSetInfo($this->entityFactory->create()->setType(\Magento\Catalog\Model\Product::ENTITY)->getTypeId(), $attributes); foreach ($attributes as $attribute) { /* @var $attribute \Magento\Catalog\Model\Entity\Attribute */ if (!$attribute->isInSet($attributeSetId)) { $attribute->setAttributeSetId($attributeSetId)->setAttributeGroupId($attributeSet->getDefaultGroupId($attributeSetId))->save(); } } }
/** * Initialize select object * * @return $this */ protected function _initSelect() { $entityTypeId = (int) $this->_eavEntityFactory->create()->setType(\Magento\Catalog\Model\Product::ENTITY)->getTypeId(); $columns = $this->getConnection()->describeTable($this->getResource()->getMainTable()); unset($columns['attribute_id']); $retColumns = []; foreach ($columns as $labelColumn => $columnData) { $retColumns[$labelColumn] = $labelColumn; if ($columnData['DATA_TYPE'] == \Magento\Framework\DB\Ddl\Table::TYPE_TEXT) { $retColumns[$labelColumn] = 'main_table.' . $labelColumn; } } $this->getSelect()->from(['main_table' => $this->getResource()->getMainTable()], $retColumns)->join(['additional_table' => $this->getTable('catalog_eav_attribute')], 'additional_table.attribute_id = main_table.attribute_id')->where('main_table.entity_type_id = ?', $entityTypeId); return $this; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testGenerateSimpleProducts() { $productsData = [6 => ['image' => 'image.jpg', 'name' => 'config-red', 'configurable_attribute' => '{"new_attr":"6"}', 'sku' => 'config-red', 'quantity_and_stock_status' => ['qty' => ''], 'weight' => '333']]; $stockData = ['manage_stock' => '0', 'use_config_enable_qty_increments' => '1', 'use_config_qty_increments' => '1', 'use_config_manage_stock' => 0, 'is_decimal_divided' => 0]; $parentProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'getNewVariationsAttributeSetId', 'getStockData', 'getQuantityAndStockStatus', 'getWebsiteIds'])->disableOriginalConstructor()->getMock(); $newSimpleProductMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['__wakeup', 'save', 'getId', 'setStoreId', 'setTypeId', 'setAttributeSetId', 'getTypeInstance', 'getStoreId', 'addData', 'setWebsiteIds', 'setStatus', 'setVisibility'])->disableOriginalConstructor()->getMock(); $attributeMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['isInSet', 'setAttributeSetId', 'setAttributeGroupId', 'save'])->disableOriginalConstructor()->getMock(); $attributeSetMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->setMethods(['load', 'addSetInfo', 'getDefaultGroupId'])->disableOriginalConstructor()->getMock(); $eavEntityMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity')->setMethods(['setType', 'getTypeId'])->disableOriginalConstructor()->getMock(); $productTypeMock = $this->getMockBuilder('Magento\\Catalog\\Model\\Product\\Type')->setMethods(['getSetAttributes'])->disableOriginalConstructor()->getMock(); $editableAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['getIsUnique', 'getAttributeCode', 'getFrontend', 'getIsVisible'])->disableOriginalConstructor()->getMock(); $frontendAttributeMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend')->setMethods(['getInputType'])->disableOriginalConstructor()->getMock(); $this->configurableProduct->expects($this->once())->method('getUsedProductAttributes')->willReturn([$attributeMock]); $parentProductMock->expects($this->any())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $this->_attributeSetFactory->expects($this->once())->method('create')->willReturn($attributeSetMock); $attributeSetMock->expects($this->once())->method('load')->with('new_attr_set_id')->willReturnSelf(); $this->_entityFactoryMock->expects($this->once())->method('create')->willReturn($eavEntityMock); $eavEntityMock->expects($this->once())->method('setType')->with('catalog_product')->willReturnSelf(); $eavEntityMock->expects($this->once())->method('getTypeId')->willReturn('type_id'); $attributeSetMock->expects($this->once())->method('addSetInfo')->with('type_id', [$attributeMock]); $attributeMock->expects($this->once())->method('isInSet')->with('new_attr_set_id')->willReturn(false); $attributeMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $attributeSetMock->expects($this->once())->method('getDefaultGroupId')->with('new_attr_set_id')->willReturn('default_group_id'); $attributeMock->expects($this->once())->method('setAttributeGroupId')->with('default_group_id')->willReturnSelf(); $attributeMock->expects($this->once())->method('save')->willReturnSelf(); $this->_productFactoryMock->expects($this->once())->method('create')->willReturn($newSimpleProductMock); $newSimpleProductMock->expects($this->once())->method('setStoreId')->with(0)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setTypeId')->with('simple')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getTypeInstance')->willReturn($productTypeMock); $productTypeMock->expects($this->once())->method('getSetAttributes')->with($newSimpleProductMock)->willReturn([$editableAttributeMock]); $editableAttributeMock->expects($this->once())->method('getIsUnique')->willReturn(false); $editableAttributeMock->expects($this->once())->method('getAttributeCode')->willReturn('some_code'); $editableAttributeMock->expects($this->any())->method('getFrontend')->willReturn($frontendAttributeMock); $frontendAttributeMock->expects($this->any())->method('getInputType')->willReturn('input_type'); $editableAttributeMock->expects($this->any())->method('getIsVisible')->willReturn(false); $parentProductMock->expects($this->once())->method('getStockData')->willReturn($stockData); $parentProductMock->expects($this->once())->method('getQuantityAndStockStatus')->willReturn(['is_in_stock' => 1]); $newSimpleProductMock->expects($this->once())->method('getStoreId')->willReturn('store_id'); $this->_stockConfiguration->expects($this->once())->method('getManageStock')->with('store_id')->willReturn(1); $newSimpleProductMock->expects($this->once())->method('addData')->willReturnSelf(); $parentProductMock->expects($this->once())->method('getWebsiteIds')->willReturn('website_id'); $newSimpleProductMock->expects($this->once())->method('setWebsiteIds')->with('website_id')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('setVisibility')->with(1)->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('save')->willReturnSelf(); $newSimpleProductMock->expects($this->once())->method('getId')->willReturn('product_id'); $this->assertEquals(['product_id'], $this->_model->generateSimpleProducts($parentProductMock, $productsData)); }
private function validateConfigurable($product, $attributeSetId, $storeId) { $type = $product->getTypeInstance(); //var_dump(\Magento\Store\Model\ScopeInterface::SCOPE_STORE);die(); $auto = $this->scopeConfig->getValue('catalog/configurable_value/auto_create', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); if (!$type instanceof Configurable) { return true; } $attributeSet = $this->attributeSetFactory->create()->load($attributeSetId); $attributes = $this->configurableProduct->getUsedProductAttributes($product); $attributeSet->addSetInfo($this->entityFactory->create()->setType(\Magento\Catalog\Model\Product::ENTITY)->getTypeId(), $attributes); foreach ($type->getConfigurableAttributes($product) as $configAattribute) { $attribute = $configAattribute->getProductAttribute(); if (!is_null($attribute)) { //var_dump($attribute->isInSet($attributeSetId));die(); if (!$attribute->isInSet($attributeSetId)) { if ($auto) { $attribute->setAttributeSetId($attributeSetId)->setAttributeGroupId($attributeSet->getDefaultGroupId($attributeSetId))->save(); return true; } else { $this->messageManager->addError(__('The configurable attribute ' . $attribute->getAttributeCode() . ' is not available in the targeted attribute set. Please create it first! Or allow create it from global config Catalog->Change Attribte Set')); return false; } } else { return true; } } } }
public function testPrepareAttributeSet() { $productMock = $this->getMockBuilder('\\Magento\\Catalog\\Model\\Product')->setMethods(['getNewVariationsAttributeSetId'])->disableOriginalConstructor()->getMock(); $attributeMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity\\Attribute')->setMethods(['isInSet', 'setAttributeSetId', 'setAttributeGroupId', 'save'])->disableOriginalConstructor()->getMock(); $attributeSetMock = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Set')->setMethods(['load', 'addSetInfo', 'getDefaultGroupId'])->disableOriginalConstructor()->getMock(); $eavEntityMock = $this->getMockBuilder('\\Magento\\Eav\\Model\\Entity')->setMethods(['setType', 'getTypeId'])->disableOriginalConstructor()->getMock(); $productMock->expects($this->once())->method('getNewVariationsAttributeSetId')->willReturn('new_attr_set_id'); $this->configurableProduct->expects($this->once())->method('getUsedProductAttributes')->with($productMock)->willReturn([$attributeMock]); $this->attributeSetFactory->expects($this->once())->method('create')->willReturn($attributeSetMock); $attributeSetMock->expects($this->once())->method('load')->with('new_attr_set_id')->willReturnSelf(); $this->entityFactoryMock->expects($this->once())->method('create')->willReturn($eavEntityMock); $eavEntityMock->expects($this->once())->method('setType')->with('catalog_product')->willReturnSelf(); $eavEntityMock->expects($this->once())->method('getTypeId')->willReturn('type_id'); $attributeSetMock->expects($this->once())->method('addSetInfo')->with('type_id', [$attributeMock]); $attributeMock->expects($this->once())->method('isInSet')->with('new_attr_set_id')->willReturn(false); $attributeMock->expects($this->once())->method('setAttributeSetId')->with('new_attr_set_id')->willReturnSelf(); $attributeSetMock->expects($this->once())->method('getDefaultGroupId')->with('new_attr_set_id')->willReturn('default_group_id'); $attributeMock->expects($this->once())->method('setAttributeGroupId')->with('default_group_id')->willReturnSelf(); $attributeMock->expects($this->once())->method('save')->willReturnSelf(); $this->model->prepareAttributeSet($productMock); }
/** * Main select object initialization. * Joins catalog/eav_attribute table * * @return $this */ protected function _initSelect() { $this->getSelect()->from(['main_table' => $this->getResource()->getMainTable()])->where('main_table.entity_type_id=?', $this->_eavEntityFactory->create()->setType(\Magento\Catalog\Model\Category::ENTITY)->getTypeId())->join(['additional_table' => $this->getTable('catalog_eav_attribute')], 'additional_table.attribute_id = main_table.attribute_id'); return $this; }
/** * Add attribute from joined entity to select * * Examples: * ('billing_firstname', 'customer_address/firstname', 'default_billing') * ('billing_lastname', 'customer_address/lastname', 'default_billing') * ('shipping_lastname', 'customer_address/lastname', 'default_billing') * ('shipping_postalcode', 'customer_address/postalcode', 'default_shipping') * ('shipping_city', $cityAttribute, 'default_shipping') * * Developer is encouraged to use existing instances of attributes and entities * After first use of string entity name it will be cached in the collection * * @todo connect between joined attributes of same entity * @param string $alias alias for the joined attribute * @param string|\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute * @param string $bind attribute of the main entity to link with joined $filter * @param string $filter primary key for the joined entity (entity_id default) * @param string $joinType inner|left * @param null $storeId * @return $this * @throws EavException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function joinAttribute($alias, $attribute, $bind, $filter = null, $joinType = 'inner', $storeId = null) { // validate alias if (isset($this->_joinAttributes[$alias])) { throw new EavException(__('Invalid alias, already exists in joint attributes')); } $bindAttribute = null; // validate bind attribute if (is_string($bind)) { $bindAttribute = $this->getAttribute($bind); } if (!$bindAttribute || !$bindAttribute->isStatic() && !$bindAttribute->getId()) { throw new EavException(__('Invalid foreign key')); } // try to explode combined entity/attribute if supplied if (is_string($attribute)) { $attrArr = explode('/', $attribute); if (isset($attrArr[1])) { $entity = $attrArr[0]; $attribute = $attrArr[1]; } } // validate entity if (empty($entity) && $attribute instanceof \Magento\Eav\Model\Entity\Attribute\AbstractAttribute) { $entity = $attribute->getEntity(); } elseif (is_string($entity)) { // retrieve cached entity if possible if (isset($this->_joinEntities[$entity])) { $entity = $this->_joinEntities[$entity]; } else { $entity = $this->_eavEntityFactory->create()->setType($attrArr[0]); } } if (!$entity || !$entity->getTypeId()) { throw new EavException(__('Invalid entity type')); } // cache entity if (!isset($this->_joinEntities[$entity->getType()])) { $this->_joinEntities[$entity->getType()] = $entity; } // validate attribute if (is_string($attribute)) { $attribute = $entity->getAttribute($attribute); } if (!$attribute) { throw new EavException(__('Invalid attribute type')); } if (empty($filter)) { $filter = $entity->getEntityIdField(); } // add joined attribute $this->_joinAttributes[$alias] = ['bind' => $bind, 'bindAttribute' => $bindAttribute, 'attribute' => $attribute, 'filter' => $filter, 'store_id' => $storeId]; $this->_addAttributeJoin($alias, $joinType); return $this; }