0

I created a module with a custom dropdown attribute, but the numbers are not displayed in the right order on the product page. How do I fix this?

enter image description here

InstallData.php

<?php
namespace Test\RingSizes\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'ring_sizes',
            [
                'type' => 'int',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'frontend' => '',
                'label' => 'Size',
                'input' => 'select',
                'class' => '',
                'source' => 'Test\RingSizes\Model\Config\Source\CustomAttributeOptions',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '',
                'searchable' => true,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'is_used_in_grid' => true,
                'is_visible_in_grid' => true,
                'is_filterable_in_grid' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

CustomAttributeOptions.php

<?php
namespace Test\RingSizes\Model\Config\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Framework\DB\Ddl\Table;

class CustomAttributeOptions extends AbstractSource
{
    /**
     * Get all options
     *
     * @return array
     */
    public function getAllOptions()
    {
        $this->_options=[
            ['label' => __('3'), 'value' => '3'],
            ['label' => __('4'), 'value' => '4'],
            ['label' => __('5'), 'value' => '5'],
            ['label' => __('6'), 'value' => '6'],
            ['label' => __('7'), 'value' => '7'],
            ['label' => __('8'), 'value' => '8'],
            ['label' => __('9'), 'value' => '9'],
            ['label' => __('10'), 'value' => '10'],
            ['label' => __('11'), 'value' => '11'],
            ['label' => __('12'), 'value' => '12'],
            ['label' => __('13'), 'value' => '13'],
            ['label' => __('14'), 'value' => '14'],
        ];
        return $this->_options;
    }

    public function getOptionText($value)
    {
        foreach ($this->getAllOptions() as $option) {
            if ($option['value'] == $value) {
                return $option['label'];
            }
        }
        return false;
    }

    public function getFlatColumns()
    {
        $attributeCode = $this->getAttribute()->getAttributeCode();
        return [
            $attributeCode => [
                'unsigned' => false,
                'default' => null,
                'extra' => null,
                'type' => Table::TYPE_INTEGER,
                'nullable' => true,
            ],
        ];
    }

    public function getFlatUpdateSelect($store)
    {
        return $this->optionFactory->create()->getFlatUpdateSelect($this->getAttribute(), $store, false);
    }
}

1
  • Please specify your Magento version and clarify what are you mean under product page? Do you mean product edit page backend? Jun 22 at 16:46

0

Your Answer

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

Browse other questions tagged or ask your own question.