示例#1
6
 /**
  * @return void
  */
 protected function _prepareForm()
 {
     /** @var \Magento\Framework\Data\Form $form */
     $form = $this->_formFactory->create();
     $collection = $this->_setFactory->create()->getResourceCollection()->load()->toOptionArray();
     $form->addField('set_switcher', 'select', ['name' => 'set_switcher', 'required' => true, 'class' => 'left-col-block', 'no_span' => true, 'values' => $collection, 'onchange' => 'this.form.submit()']);
     $form->setUseContainer(true);
     $form->setMethod('post');
     $this->setForm($form);
 }
示例#2
0
 /**
  * @return \Magento\Eav\Model\Entity\Attribute\Set
  * @throws AlreadyExistsException
  */
 public function getAttributeSet()
 {
     $this->validateParameters();
     /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
     $attributeSet = $this->attributeSetFactory->create();
     $attributeSet->setEntityTypeId($this->entityTypeId)->load($this->name, 'attribute_set_name');
     if ($attributeSet->getId()) {
         throw new AlreadyExistsException(__('Attribute Set already exists.'));
     }
     $attributeSet->setAttributeSetName($this->name)->validate();
     $attributeSet->save();
     $attributeSet->initFromSkeleton($this->skeletonId)->save();
     return $attributeSet;
 }
 /**
  * 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();
         }
     }
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function getList($attributeSetId)
 {
     if (!$this->attributeSetFactory->create()->load($attributeSetId)->getId()) {
         throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
     }
     $collection = $this->groupListFactory->create();
     $collection->setAttributeSetFilter($attributeSetId);
     $collection->setSortOrder();
     $groups = array();
     /** @var $group \Magento\Eav\Model\Entity\Attribute\Group */
     foreach ($collection->getItems() as $group) {
         $this->groupBuilder->setId($group->getId())->setName($group->getAttributeGroupName());
         $groups[] = $this->groupBuilder->create();
     }
     return $groups;
 }
示例#5
0
文件: Type.php 项目: aiesh/magento2
 /**
  * Retrieve entity tpe sets collection
  *
  * @return \Magento\Eav\Model\Resource\Entity\Attribute\Set\Collection
  */
 public function getAttributeSetCollection()
 {
     if (empty($this->_sets)) {
         $this->_sets = $this->_attSetFactory->create()->getResourceCollection()->setEntityTypeFilter($this->getId());
     }
     return $this->_sets;
 }
 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;
             }
         }
     }
 }
示例#7
0
 /**
  * @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;
 }
示例#8
0
 /**
  * {inheritdoc}
  */
 public function create($attributeSetId, \Magento\Catalog\Service\V1\Data\Eav\AttributeGroup $groupData)
 {
     if (!$this->setFactory->create()->load($attributeSetId)->getId()) {
         throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
     }
     try {
         /** @var Group $attributeGroup */
         $attributeGroup = $this->groupFactory->create();
         $attributeGroup->setAttributeGroupName($groupData->getName());
         $attributeGroup->setAttributeSetId($attributeSetId);
         $attributeGroup->save();
         return $this->groupBuilder->setId($attributeGroup->getId())->setName($attributeGroup->getAttributeGroupName())->create();
     } catch (\Exception $e) {
         throw new CouldNotSaveException('Could not create attribute group. Maybe group with such name already exists');
     }
 }
示例#9
0
 /**
  * @return string
  */
 public function getAttributeSetName()
 {
     if ($setId = $this->getProduct()->getAttributeSetId()) {
         $set = $this->_attributeSetFactory->create()->load($setId);
         return $set->getAttributeSetName();
     }
     return '';
 }
 /**
  * {@inheritdoc}
  */
 public function get($attributeSetId)
 {
     /** @var AttributeSet $attributeSet */
     $attributeSet = $this->attributeSetFactory->create();
     $this->attributeSetResource->load($attributeSet, $attributeSetId);
     if (!$attributeSet->getId()) {
         throw NoSuchEntityException::singleField('id', $attributeSetId);
     }
     return $attributeSet;
 }
示例#11
0
 /**
  * {@inheritdoc}
  */
 public function validate($object)
 {
     //validate attribute set entity type
     $entityType = $this->typeFactory->create()->loadByCode(\Magento\Catalog\Model\Product::ENTITY);
     $attributeSet = $this->setFactory->create()->load($object->getAttributeSetId());
     if ($attributeSet->getEntityTypeId() != $entityType->getId()) {
         return ['attribute_set' => 'Invalid product template entity type'];
     }
     return parent::validate($object);
 }
示例#12
0
 /**
  * Prepares attribute set form
  *
  * @return void
  */
 protected function _prepareForm()
 {
     $data = $this->_setFactory->create()->load($this->getRequest()->getParam('id'));
     /** @var \Magento\Framework\Data\Form $form */
     $form = $this->_formFactory->create();
     $fieldset = $form->addFieldset('set_name', ['legend' => __('Edit Attribute Set Name')]);
     $fieldset->addField('attribute_set_name', 'text', ['label' => __('Name'), 'note' => __('For internal use'), 'name' => 'attribute_set_name', 'required' => true, 'class' => 'required-entry validate-no-html-tags', 'value' => $data->getAttributeSetName()]);
     if (!$this->getRequest()->getParam('id', false)) {
         $fieldset->addField('gotoEdit', 'hidden', ['name' => 'gotoEdit', 'value' => '1']);
         $sets = $this->_setFactory->create()->getResourceCollection()->setEntityTypeFilter($this->_coreRegistry->registry('entityType'))->load()->toOptionArray();
         $fieldset->addField('skeleton_set', 'select', ['label' => __('Based On'), 'name' => 'skeleton_set', 'required' => true, 'class' => 'required-entry', 'values' => $sets]);
     }
     $form->setMethod('post');
     $form->setUseContainer(true);
     $form->setId('set-prop-form');
     $form->setAction($this->getUrl('catalog/*/save'));
     $form->setOnsubmit('return false;');
     $this->setForm($form);
 }
示例#13
0
 /**
  * {@inheritdoc}
  */
 public function types($attributeSetId)
 {
     $attributeSet = $this->setFactory->create()->load($attributeSetId);
     if (!$attributeSet->getId()) {
         throw NoSuchEntityException::singleField('attribute_set_id', $attributeSetId);
     }
     $productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
     if ($attributeSet->getEntityTypeId() != $productEntityId) {
         throw InputException::invalidFieldValue('entity_type_id', $attributeSetId);
     }
     $collection = $this->collectionFactory->create();
     $collection->setAttributeSetFilter($attributeSetId);
     $collection->setFrontendInputTypeFilter('media_image');
     $collection->addStoreLabel($this->storeManager->getStore()->getId());
     return $this->prepareData($collection->getItems());
 }
示例#14
0
 /**
  * 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);
 }
示例#15
0
 /**
  * {@inheritdoc}
  */
 public function getAttributeList($attributeSetId)
 {
     /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
     $attributeSet = $this->setFactory->create()->load($attributeSetId);
     $requiredEntityTypeId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
     if (!$attributeSet->getId() || $attributeSet->getEntityTypeId() != $requiredEntityTypeId) {
         // Attribute set does not exist
         throw NoSuchEntityException::singleField('attributeSetId', $attributeSetId);
     }
     $attributeCollection = $this->attributeCollection->setAttributeSetFilter($attributeSet->getId())->load();
     $attributes = array();
     /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
     foreach ($attributeCollection as $attribute) {
         $attributes[] = $this->attributeBuilder->setId($attribute->getAttributeId())->setCode($attribute->getAttributeCode())->setFrontendLabel($attribute->getData('frontend_label'))->setDefaultValue($attribute->getDefaultValue())->setIsRequired((bool) $attribute->getData('is_required'))->setIsUserDefined((bool) $attribute->getData('is_user_defined'))->setFrontendInput($attribute->getData('frontend_input'))->create();
     }
     return $attributes;
 }
示例#16
0
 /**
  * Loads attribute set by name if attribute with such name exists
  * Otherwise creates the attribute set with $setName name and return it
  *
  * @param string $setName
  * @return \Magento\Eav\Model\Entity\Attribute\Set
  * @throws \Exception
  * @throws \Magento\Framework\Model\Exception
  */
 protected function processAttributeSet($setName)
 {
     /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
     $attributeSet = $this->attributeSetFactory->create();
     $setCollection = $attributeSet->getResourceCollection()->addFieldToFilter('entity_type_id', $this->getEntityTypeId())->addFieldToFilter('attribute_set_name', $setName)->load();
     $attributeSet = $setCollection->fetchItem();
     if (!$attributeSet) {
         $attributeSet = $this->attributeSetFactory->create();
         $attributeSet->setEntityTypeId($this->getEntityTypeId());
         $attributeSet->setAttributeSetName($setName);
         $attributeSet->save();
         $defaultSetId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getDefaultAttributeSetId();
         $attributeSet->initFromSkeleton($defaultSetId);
         $attributeSet->save();
     }
     return $attributeSet;
 }
示例#17
0
 /**
  * {@inheritdoc}
  */
 public function remove($attributeSetId)
 {
     $id = intval($attributeSetId);
     if (0 == $id) {
         throw InputException::invalidFieldValue('id', $id);
     }
     /** @var \Magento\Eav\Model\Entity\Attribute\Set $attributeSet */
     $attributeSet = $this->setFactory->create()->load($id);
     $defaultAttributeSetId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getDefaultAttributeSetId();
     $loadedData = $attributeSet->getData();
     if (empty($loadedData)) {
         throw NoSuchEntityException::singleField('id', $attributeSetId);
     }
     $productEntityId = $this->eavConfig->getEntityType(\Magento\Catalog\Model\Product::ENTITY)->getId();
     if ($attributeSet->getEntityTypeId() != $productEntityId) {
         throw InputException::invalidFieldValue('id', $attributeSetId);
     }
     if ($attributeSetId == $defaultAttributeSetId) {
         throw new StateException('Default attribute set can not be deleted');
     }
     $attributeSet->delete();
     return true;
 }
 /**
  * @return \Magento\Eav\Model\Entity\Attribute\Set
  */
 protected function getAttributeSetModel()
 {
     return $this->attributeSetFactory->create();
 }
 /**
  * {@inheritdoc}
  */
 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
 {
     /** @var CustomerSetup $customerSetup */
     $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
     $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
     $attributeSetId = $customerEntity->getDefaultAttributeSetId();
     /** @var $attributeSet AttributeSet */
     $attributeSet = $this->attributeSetFactory->create();
     $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
     $customerSetup->addAttribute(Customer::ENTITY, 'magento_username', ['type' => 'varchar', 'label' => 'Magento Username', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'sort_order' => 1000, 'position' => 1000, 'system' => 0]);
     $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'magento_username')->addData(['attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer']]);
     $attribute->save();
 }