0

I have created Custom attribute for product sorting in magento 2.1.1. by Setup>installData.php `namespace Vendor\Module\Setup;

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Resource\Eav\Attribute;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;


/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{
    /**
     * EAV setup factory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * Init
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        /* assign object to class global variable for use in other class methods */
    }

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
         $setup->startSetup();
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);


        /**
         * Add attributes to the eav/attribute
       */
       $eavSetup->addAttribute(
         \Magento\Catalog\Model\Product::ENTITY,
       'sorting_attribute',
       [
       'type' => 'int',
        'backend' => '',
        'frontend' => '',
        'label' => 'Sorting Attribute',
        'input' => 'text',
        'class' => '',
        'source' => '',
        'global' => 1,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => 0,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => true,
        'used_in_product_listing' => true,
        'unique' => false,
        'apply_to' => 'simple,configurable,virtual,bundle,downloadable,grouped',
        'group'=> 'General'
       ]
      );






       $setup->endSetup();
    }
}`

Expected sorting for product

    Product a: custom_attribute = 12,
    Product b: custom_attribute = 42,
    Product c: custom_attribute = 55,
    Product d: custom_attribute = 135,
    Product e: custom_attribute = 325,
    Product f: custom_attribute = 443,
    Product g: custom_attribute = 1134,
    Product h: custom_attribute = 2231

but actually it is sorting as :

  Product a: custom_attribute = 443,
    Product b: custom_attribute = 135,
    Product c: custom_attribute = 325,
    Product d: custom_attribute = 55,
    Product e: custom_attribute = 42,
    Product f: custom_attribute = 1134,
    Product g: custom_attribute = 12,
    Product h: custom_attribute = 2231

After switching with other option and refreshing the page the sorting order gets changed , How it works ?? I tried with varchar type attribute created through admin it was also behaving very similar to the same.

enter image description here

  • Problem due to attribute type varchar. change it with int. – Suresh Chikani Nov 9 '16 at 9:50
  • type is int only i said i tried by creating one attribute from admin- varchar was showing same effect – supriya mishra Nov 9 '16 at 9:51
  • something wrong with custom attribute, check directly it in table structure. – Suresh Chikani Nov 9 '16 at 9:54
  • I have same issue, changed it with int and it's working. – Suresh Chikani Nov 9 '16 at 9:54
  • updated my ques with code what i have used where is the issue please check and help – supriya mishra Nov 9 '16 at 9:57
1

remove these fields from the script

'backend' => '',
'frontend' => '',

after running setup:upgrade you will see backend_type is int in eav_attribute table. Also add this in your script

'used_for_sort_by'=>1

After the attribute is created, do not save this attribute from admin because if you save it, backend_type will again change from int to varchar.

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.