コード例 #1
0
ファイル: AttributeService.php プロジェクト: aiesh/magento2
 /**
  * Add attribute to attribute set and group
  *
  * @param int $attributeSetId
  * @param Data\Eav\AttributeSet\Attribute $data
  * @return int
  * @throws \Magento\Framework\Exception\InputException
  */
 public function addAttribute($attributeSetId, \Magento\Catalog\Service\V1\Data\Eav\AttributeSet\Attribute $data)
 {
     $attributeSet = $this->setFactory->create()->load($attributeSetId);
     if (!$attributeSet->getId()) {
         throw new InputException('Attribute set does not exist');
     }
     $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
     if ($setEntityType->getEntityTypeCode() != \Magento\Catalog\Model\Product::ENTITY) {
         throw new InputException('Wrong attribute set id provided');
     }
     if (!$this->groupFactory->create()->load($data->getAttributeGroupId())->getId()) {
         throw new InputException('Attribute group does not exist');
     }
     $attribute = $this->attributeFactory->create();
     if (!$attribute->load($data->getAttributeId())->getId()) {
         throw new InputException('Attribute does not exist');
     }
     $attribute->setId($data->getAttributeId());
     $attribute->setEntityTypeId($setEntityType->getId());
     $attribute->setAttributeSetId($attributeSetId);
     $attribute->setAttributeGroupId($data->getAttributeGroupId());
     $attribute->setSortOrder($data->getSortOrder());
     $this->attributeResource->saveInSetIncluding($attribute);
     return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function assign($entityTypeCode, $attributeSetId, $attributeGroupId, $attributeCode, $sortOrder)
 {
     try {
         $attributeSet = $this->setRepository->get($attributeSetId);
     } catch (NoSuchEntityException $ex) {
         throw new NoSuchEntityException(__('AttributeSet with id "%1" does not exist.', $attributeSetId));
     }
     $setEntityType = $this->entityTypeFactory->create()->getEntityType($attributeSet->getEntityTypeId());
     if ($setEntityType->getEntityTypeCode() != $entityTypeCode) {
         throw new InputException(__('Wrong attribute set id provided'));
     }
     //Check if group exists. If not - expected exception
     $attributeGroup = $this->groupRepository->get($attributeGroupId);
     if ($attributeGroup->getAttributeSetId() != $attributeSetId) {
         throw new InputException(__('Attribute group does not belong to attribute set'));
     }
     /** @var \Magento\Eav\Api\Data\AttributeInterface $attribute */
     $attribute = $this->attributeRepository->get($entityTypeCode, $attributeCode);
     $this->attributeResource->saveInSetIncluding($attribute, $attribute->getAttributeId(), $attributeSetId, $attributeGroupId, $sortOrder);
     $attribute->setAttributeSetId($attributeSetId);
     return $attribute->loadEntityAttributeIdBySet()->getData('entity_attribute_id');
 }