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.