1

So I've created 6 custom attributes in my modules using installData.php let's take two of them:

  $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'badge1text',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Badge 1 Text',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => '',
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );

        /**
         * Insert/Create a seletbox attribute with custom options
         */
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'badge1image',
            [
                'type' => 'int', // data type to be saved in database table
                'backend' => '',
                'frontend' => '',
                'label' => 'Badge 1 Background',
                'input' => 'select', // form element type displayed in the form
                'class' => '',
                'source' => 'UNBXD\ProductAttributes\Model\Config\Source\MyCustomOptions',
                'global' =>\Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE, ,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );

and here's my custom select source:

class MyCustomOptions extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        if ($this->_options === null) {
            $this->_options = [
                ['value' => '',  'label' => __('Please Select Background Color')],
                ['value' => '1', 'label' => __('Purple')],
                ['value' => '2', 'label' => __('Blue')],
                ['value' => '3', 'label' => __('Pink')],
                ['value' => '4', 'label' => __('Green')]
            ];
        }
        return $this->_options;
    }

    /**
     * Get text of the option value
     * 
     * @param string|integer $value
     * @return string|bool
     */
    public function getOptionValue($value) 
    { 
        foreach ($this->getAllOptions() as $option) {
            if ($option['value'] == $value) {
                return $option['label'];
            }
        }
        return false;
    }
}

The problem is that 'Badge 1 Background' value isn't visible on catalog / category page. Only 1st value placeholder is accessible 'Please Select Background ' ...not real value. On product page all is fine. I've try to used those code to display value on catalog:

$attribute = $_product->getResource()->getAttribute('badge1image'); 
$attr_value = $attribute ->getFrontend()->getValue($_product); 
echo $attr_value; 

What I've missed on InstallData file? Looks like text field is visible but not select with dropdown options...

1 Answer 1

0

I found a way to get product attributr (SELECT) value from $_productCollection even if it's not available from foreach loop.

 $productName = $_product->getName();
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
   $my_value = $product->getData('my_attribute');

Problem SOLVED!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.