public function testGetUsedProductAttributes() { $testConfigurable = $this->_getAttributeByCode('test_configurable'); $attributeId = (int) $testConfigurable->getId(); $attributes = $this->_model->getUsedProductAttributes($this->_product); $this->assertArrayHasKey($attributeId, $attributes); $this->assertSame($testConfigurable, $attributes[$attributeId]); }
/** * Preparing layout * * @return \Magento\ConfigurableProduct\Block\Product\Configurable\AssociatedSelector\Backend\Grid\ColumnSet */ protected function _prepareLayout() { parent::_prepareLayout(); $product = $this->getProduct(); $attributes = $this->_productType->getUsedProductAttributes($product); foreach ($attributes as $attribute) { /** @var $attribute \Magento\Catalog\Model\Entity\Attribute */ /** @var $block \Magento\Backend\Block\Widget\Grid\Column */ $block = $this->addChild($attribute->getAttributeCode(), 'Magento\\Backend\\Block\\Widget\\Grid\\Column', array('header' => $attribute->getStoreLabel(), 'index' => $attribute->getAttributeCode(), 'type' => 'options', 'options' => $this->getOptions($attribute->getSource()), 'sortable' => false)); $block->setId($attribute->getAttributeCode())->setGrid($this); } return $this; }
/** * 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(); } } }
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; } } } }
/** * Prepare form before rendering HTML * * @return $this */ protected function _prepareForm() { /** @var \Magento\Framework\Data\Form $form */ $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('settings', ['legend' => __('Select Configurable Attributes')]); $fieldset->addField('configurable-attribute-selector', 'text', ['label' => 'Select Attribute', 'title' => 'Select Attribute']); $product = $this->getProduct(); $usedAttributes = $product->getTypeId() == Configurable::TYPE_CODE ? $this->_configurableType->getUsedProductAttributes($product) : []; foreach ($usedAttributes as $attribute) { /** @var $attribute \Magento\Catalog\Model\Resource\Eav\Attribute */ if ($this->_configurableType->canUseAttribute($attribute, $product)) { $fieldset->addField('attribute_' . $attribute->getAttributeId(), 'checkbox', ['label' => $attribute->getFrontendLabel(), 'title' => $attribute->getFrontendLabel(), 'name' => 'attributes[]', 'class' => 'configurable-attribute-checkbox', 'value' => $attribute->getAttributeId(), 'checked' => true]); } } $fieldset->addField('continue_button', 'note', ['text' => $this->getChildHtml('continue_button')]); $this->setForm($form); return parent::_prepareForm(); }
/** * Get used product attributes * * @return array */ public function getUsedAttributes() { return $this->_configurableType->getUsedProductAttributes($this->getProduct()); }
/** * Get used product attributes * * @return array */ protected function getUsedAttributes() { return $this->configurableType->getUsedProductAttributes($this->locator->getProduct()); }
/** * Get list of used attributes * * @return array */ public function getSelectedAttributes() { return $this->getProduct()->getTypeId() == Configurable::TYPE_CODE ? array_filter($this->_configurableType->getUsedProductAttributes($this->getProduct())) : array(); }