how to get a dynamic customer attribute option from eav_atrribute_option _value table in magento 2
-
06-03-2021 - |
Pergunta
how to get a dynamic attribute options
from eav_atrribute_option _value table in magento 2
app/code/Cm/CustomerAttribute/Model/Plugin/Checkout/LayoutProcessor.php
<?php
namespace Cm\CustomerAttribute\Model\Plugin\Checkout;
class LayoutProcessor
{
/**
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
public function __construct(\Bootsgrid\CustomerAttribute\Helper\Customerattribute $helper)
{
$this->helper = $helper;
}
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
$attributeCollection = $this->helper->getUserDefinedAttribures();
if ($attributeCollection->getSize() > 0)
{
foreach ($attributeCollection as $attribute)
{
if ($this->helper->isAttribureForCheckoutRegister($attribute->getAttributeCode()))
{
$frontEndLabel = $attribute->getStoreLabel($this->helper->getStoreId());
$attributeCode = $attribute->getAttributeCode();
$getAttributeCodeFunction = "get" . str_replace(' ', '', ucwords(str_replace('_', ' ', $attribute->getAttributeCode())));
$fieldRequiredClass = ($attribute->getIsRequired()) ? 'true' : '' ;
$fieldFrontendClass = ($attribute->getFrontendClass()) ? $attribute->getFrontendClass() : '';
$fieldInput =$attribute->getFrontendInput();
if($fieldInput == 'text')
{
$fieldInputType = 'input';
$fieldAbstract = 'abstract';
}
elseif ($fieldInput == 'date') {
$fieldInputType = 'date';
$fieldAbstract = 'abstract';
}
elseif ($fieldInput == 'select') {
$fieldInputType = 'select';
$fieldAbstract = 'select';
}
elseif ($fieldInput == 'multiselect') {
$fieldInputType = 'multiselect';
$fieldAbstract = 'abstract';
}
elseif ($fieldInput == 'boolean') {
$fieldInputType = 'boolean';
$fieldAbstract = 'abstract';
}
elseif ($fieldInput == 'textarea') {
$fieldInputType = 'textarea';
$fieldAbstract = 'abstract';
}
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children'][$attributeCode] = [
'component' => 'Magento_Ui/js/form/element/'.$fieldAbstract.'',
'config' => [
'customScope' => 'shippingAddress.custom_attributes',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/'.$fieldInputType.'',
'id' => $attributeCode
],
'dataScope' => 'shippingAddress.custom_attributes.'.$attributeCode.'',
'label' => $frontEndLabel,
'provider' => 'checkoutProvider',
'visible' => true,
'validation' =>[$fieldFrontendClass , 'required-entry' => $fieldRequiredClass],
'sortOrder' => 250,
'options' => [
[
'value' => '',
'label' => 'Please select a value ',
],
[
'value' => '1',
'label' => 'Silver level ',
],
],
'id' => $attributeCode
];
}
}
}
return $jsLayout;
}
}
image:
Solução
One of the example of fetching options of customer attribute
Object Manager Approach
/**
* Get attribute info by attribute code and entity type
*/
$attributeCode = 'gender';
$entityType = 'customer';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeInfo = $objectManager->get(\Magento\Eav\Model\Entity\Attribute::class)
->loadByCode($entityType, $attributeCode);
/**
* Get all options name and value of the attribute
*/
$attributeId = $attributeInfo->getAttributeId();
$attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->setStoreFilter()
->load();
var_dump($attributeOptionAll->getData());
Block Approach
if you want to do it through block then add this block in your module
<?php
namespace YourCompanyName\YourModuleName\Block;
class YourCustomBlock extends \Magento\Framework\View\Element\Template {
/**
* @var \Magento\Eav\Model\Entity\Attribute
*/
protected $_entityAttribute;
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection
*/
protected $_entityAttributeCollection;
/**
* @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
*/
protected $_entityAttributeOptionCollection;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Eav\Model\Entity\Attribute $entityAttribute
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $eavEntityAttributeCollection
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $entityAttributeOptionCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Eav\Model\Entity\Attribute $entityAttribute,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection $entityAttributeCollection,
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection $entityAttributeOptionCollection,
array $data = []
) {
$this->_entityAttribute = $entityAttribute;
$this->_entityAttributeCollection = $entityAttributeCollection;
$this->_entityAttributeOptionCollection = $entityAttributeOptionCollection;
parent::__construct($context, $data);
}
/**
* Get attribute info by attribute code and entity type
*
* @param mixed $entityType can be integer, string, or instance of class Mage\Eav\Model\Entity\Type
* @param string $attributeCode
* @return \Magento\Eav\Model\Entity\Attribute
*/
public function getAttributeInfo($entityType, $attributeCode)
{
return $this->_entityAttribute->loadByCode($entityType, $attributeCode);
}
/**
* Get all options name and value of the attribute
*
* @param int $attributeId
* @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
*/
public function getAttributeOptionAll($attributeId)
{
return $this->_attributeOptionCollection
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->setStoreFilter()
->load();
}
/**
* Get particular option's name and value of the attribute
*
* @param int $attributeId
* @param int $optionId
* @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
*/
public function getAttributeOptionById($attributeId, $optionId)
{
return $this->_attributeOptionCollection
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->setIdFilter($optionId)
->setStoreFilter()
->load();
}
/**
* Get the first option's name and value of the attribute
*
* @param int $attributeId
* @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection
*/
public function getAttributeFirstOption($attributeId)
{
return $this->_attributeOptionCollection
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->load()
->getFirstItem();
}
}
I hope this will help
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange