/** * @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); }
/** * @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(); } } }
/** * @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)); }
/** * {@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; }
/** * 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; } } } }
/** * @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; }
/** * {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'); } }
/** * @return string */ public function getAttributeSetName() { if ($setId = $this->getProduct()->getAttributeSetId()) { $set = $this->_attributeSetFactory->create()->load($setId); return $set->getAttributeSetName(); } return ''; }
/** * {@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); }
/** * {@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; }
/** * 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); }
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); }
/** * {@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()); }
/** * 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); }
/** * {@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; }
/** * 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; }
/** * {@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 string */ protected function getCurrentAttributeSetName() { return $this->attributeSetRepository->get($this->_coreRegistry->registry('current_product')->getAttributeSetId())->getAttributeSetName(); }
/** * @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(); }