/** * 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; }
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; } } } }
/** * 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; }