/** * Update the default product tax class * * @return \Magento\Tax\Model\Config\TaxClass */ protected function _afterSave() { $attributeCode = "tax_class_id"; $attribute = $this->attributeFactory->create(); $attribute->loadByCode(\Magento\Catalog\Model\Product::ENTITY, $attributeCode); if (!$attribute->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid attribute %1', $attributeCode)); } $attribute->setData("default_value", $this->getData('value')); $attribute->save(); return parent::_afterSave($this); }
/** * @param string $attributeSetId * @param string $attributeId * @return bool * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\StateException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function deleteAttribute($attributeSetId, $attributeId) { $attributeSet = $this->setFactory->create()->load($attributeSetId); if (!$attributeSet->getId()) { // Attribute set does not exist throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId); } // check that attribute set has type catalog_product $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId()); if ($setEntityType->getEntityTypeCode() != \Magento\Catalog\Model\Product::ENTITY) { throw new InputException('Attribute with wrong attribute type is provided'); } // check if attribute with requested id exists $attribute = $this->attributeFactory->create()->load($attributeId); if (!$attribute->getId()) { // Attribute set does not exist throw NoSuchEntityException::singleField('attributeId', $attributeId); } // check if attribute is in set $attribute->setAttributeSetId($attributeSet->getId())->loadEntityAttributeIdBySet(); if (!$attribute->getEntityAttributeId()) { throw new InputException('Requested attribute is not in requested attribute set.'); } if (!$attribute->getIsUserDefined()) { throw new StateException('System attribute can not be deleted'); } $attribute->deleteEntity(); return true; }
public function __construct(\Magento\Framework\App\Helper\Context $context, \Magento\Eav\Model\Entity\TypeFactory $attributeTypeFactory, \Magento\Eav\Model\Entity\AttributeFactory $attributeFactory, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\Framework\ObjectManagerInterface $objectManager) { parent::__construct($context); $this->_messageManager = $messageManager; $this->_objectManager = $objectManager; $typeId = -1; $resTypeId = $attributeTypeFactory->create()->getCollection()->addFieldToFilter('entity_type_code', ['eq' => 'catalog_product']); foreach ($resTypeId as $re) { $typeId = $re['entity_type_id']; } $attributesList = $attributeFactory->create()->getCollection()->addFieldToFilter('entity_type_id', ['eq' => $typeId]); $this->_listOfAttributes = []; foreach ($attributesList as $key => $attr) { array_push($this->_listOfAttributes, $attr['attribute_code']); } }
/** * Retrieve Purchased Separately Attribute object * * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute */ public function getPurchasedSeparatelyAttribute() { if ($this->_purchasedSeparatelyAttribute === null) { $_attributeCode = 'links_purchased_separately'; $this->_purchasedSeparatelyAttribute = $this->_attributeFactory->create()->loadByCode(\Magento\Catalog\Model\Product::ENTITY, $_attributeCode); } return $this->_purchasedSeparatelyAttribute; }
/** * Init and retrieve attribute collection * * @return \Magento\Eav\Model\Resource\Entity\Attribute\Collection */ protected function _getAttributeCollection() { $collection = $this->_attributeFactory->create()->getCollection(); $objectsModel = $this->getAttributeModel(); if ($objectsModel) { $collection->setModel($objectsModel); } return $collection; }
/** * Collect data for save * * @param array $data * @return $this * @throws LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function organizeData($data) { $modelGroupArray = []; $modelAttributeArray = []; $attributeIds = []; if ($data['attributes']) { $ids = []; foreach ($data['attributes'] as $attribute) { $ids[] = $attribute[0]; } $attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids); } if ($data['groups']) { foreach ($data['groups'] as $group) { $modelGroup = $this->initGroupModel($group); if ($data['attributes']) { foreach ($data['attributes'] as $attribute) { if ($attribute[1] == $group[0] && in_array($attribute[0], $attributeIds)) { $modelAttribute = $this->_attributeFactory->create(); $modelAttribute->setId($attribute[0])->setAttributeGroupId($attribute[1])->setAttributeSetId($this->getId())->setEntityTypeId($this->getEntityTypeId())->setSortOrder($attribute[2]); $modelAttributeArray[] = $modelAttribute; } } $modelGroup->setAttributes($modelAttributeArray); $modelAttributeArray = []; } $modelGroupArray[] = $modelGroup; } $this->setGroups($modelGroupArray); } if ($data['not_attributes']) { $modelAttributeArray = []; $data['not_attributes'] = array_filter($data['not_attributes']); foreach ($data['not_attributes'] as $entityAttributeId) { $entityAttribute = $this->_resourceAttribute->getEntityAttribute($entityAttributeId); if (!$entityAttribute) { throw new LocalizedException(__('Entity attribute with id "%1" not found', $entityAttributeId)); } $modelAttribute = $this->_eavConfig->getAttribute($this->getEntityTypeId(), $entityAttribute['attribute_id']); $modelAttribute->setEntityAttributeId($entityAttributeId); $modelAttributeArray[] = $modelAttribute; } $this->setRemoveAttributes($modelAttributeArray); } if ($data['removeGroups']) { $modelGroupArray = []; foreach ($data['removeGroups'] as $groupId) { $modelGroup = $this->_attrGroupFactory->create(); $modelGroup->setId($groupId); $modelGroupArray[] = $modelGroup; } $this->setRemoveGroups($modelGroupArray); } $this->setAttributeSetName($data['attribute_set_name'])->setEntityTypeId($this->getEntityTypeId()); return $this; }
/** * Retrieve Wee tax attribute codes * * @param null|string|bool|int|Store $store * @param bool $forceEnabled * @return array */ public function getWeeeTaxAttributeCodes($store = null, $forceEnabled = false) { if (!$forceEnabled && !$this->weeeConfig->isEnabled($store)) { return []; } if ($this->_allAttributes === null) { $this->_allAttributes = $this->_attributeFactory->create()->getAttributeCodesByFrontendType('weee'); } return $this->_allAttributes; }
/** * Retrieve Wee tax attribute codes * * @param bool $forceEnabled * @return array */ public function getWeeeTaxAttributeCodes($forceEnabled = false) { if (!$forceEnabled && !$this->weeeConfig->isEnabled()) { return array(); } if (is_null($this->_allAttributes)) { $this->_allAttributes = $this->_attributeFactory->create()->getAttributeCodesByFrontendType('weee'); } return $this->_allAttributes; }
/** * {@inheritdoc} */ public function deleteById($attributeId) { /** @var \Magento\Eav\Model\Entity\Attribute $attribute */ $attribute = $this->attributeFactory->create(); $this->eavResource->load($attribute, $attributeId); if (!$attribute->getAttributeId()) { throw new NoSuchEntityException(__('Attribute with id "%1" does not exist.', $attributeId)); } $this->delete($attribute); return true; }
/** * Collect data for save * * @param array $data * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function organizeData($data) { $modelGroupArray = []; $modelAttributeArray = []; $attributeIds = []; if ($data['attributes']) { $ids = []; foreach ($data['attributes'] as $attribute) { $ids[] = $attribute[0]; } $attributeIds = $this->_resourceAttribute->getValidAttributeIds($ids); } if ($data['groups']) { foreach ($data['groups'] as $group) { $modelGroup = $this->_attrGroupFactory->create(); $modelGroup->setId(is_numeric($group[0]) && $group[0] > 0 ? $group[0] : null)->setAttributeGroupName($group[1])->setAttributeSetId($this->getId())->setSortOrder($group[2]); if ($data['attributes']) { foreach ($data['attributes'] as $attribute) { if ($attribute[1] == $group[0] && in_array($attribute[0], $attributeIds)) { $modelAttribute = $this->_attributeFactory->create(); $modelAttribute->setId($attribute[0])->setAttributeGroupId($attribute[1])->setAttributeSetId($this->getId())->setEntityTypeId($this->getEntityTypeId())->setSortOrder($attribute[2]); $modelAttributeArray[] = $modelAttribute; } } $modelGroup->setAttributes($modelAttributeArray); $modelAttributeArray = []; } $modelGroupArray[] = $modelGroup; } $this->setGroups($modelGroupArray); } if ($data['not_attributes']) { $modelAttributeArray = []; foreach ($data['not_attributes'] as $attributeId) { $modelAttribute = $this->_attributeFactory->create(); $modelAttribute->setEntityAttributeId($attributeId); $modelAttributeArray[] = $modelAttribute; } $this->setRemoveAttributes($modelAttributeArray); } if ($data['removeGroups']) { $modelGroupArray = []; foreach ($data['removeGroups'] as $groupId) { $modelGroup = $this->_attrGroupFactory->create(); $modelGroup->setId($groupId); $modelGroupArray[] = $modelGroup; } $this->setRemoveGroups($modelGroupArray); } $this->setAttributeSetName($data['attribute_set_name'])->setEntityTypeId($this->getEntityTypeId()); return $this; }
/** * Retrieve attributes data as JSON * * @return string */ public function getAttributesBlockJson() { $result = []; if ($this->getRequest()->getParam('product_tab') == 'variations') { /** @var $attribute \Magento\Eav\Model\Entity\Attribute */ $attribute = $this->_attributeFactory->create()->load($this->getRequest()->getParam('attribute')); $result = ['tab' => $this->getRequest()->getParam('product_tab'), 'attribute' => ['id' => $attribute->getId(), 'label' => $attribute->getFrontendLabel(), 'code' => $attribute->getAttributeCode(), 'options' => $attribute->getSourceModel() ? $attribute->getSource()->getAllOptions(false) : []]]; } $newAttributeSetId = $this->getRequest()->getParam('new_attribute_set_id'); if ($newAttributeSetId) { /** @var $attributeSet \Magento\Eav\Model\Entity\Attribute\Set */ $attributeSet = $this->_setFactory->create()->load($newAttributeSetId); $result['set'] = ['id' => $attributeSet->getId(), 'label' => $attributeSet->getAttributeSetName()]; } return $this->_jsonEncoder->encode($result); }
/** * Retrieve loaded attribute by code * * @param string $attributeCode * @throws \Magento\Framework\Exception\LocalizedException * @return \Magento\Eav\Model\Entity\Attribute */ public function getAttribute($attributeCode) { $attributes = $this->getAttributes(); if (!isset($attributes[$attributeCode])) { $attribute = $this->_attributeFactory->create(); $attribute->loadByCode($this->getEntityTypeId(), $attributeCode); if (!$attribute->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('Invalid attribute %1', $attributeCode)); } $entity = $this->_eavConfig->getEntityType($this->getEntityType())->getEntity(); $attribute->setEntity($entity); return $attribute; } return $attributes[$attributeCode]; }
protected function addAttributeCount() { $this->infos['Attribute Count'] = $this->attributeFactory->create()->getCollection()->getSize(); }
/** * @return \Magento\Eav\Model\Attribute */ protected function getAttributeModel() { return $this->attributeFactory->create(); }