Question

Stores > Attributes > Product > My Custom Attribute > Storefront Properties tab

I'm not able to change the Storefront Properties 'Used for Sorting in Product Listing', since it is disabled or grayed out.

enter image description here

What is the reason behind it ? How can I fix this ?

Updated

Script used below and version is Magento Commerce 2.4.0

          $attributeData  = [
            'attribute_code'                => $formattedCode,
            'is_global'                     => 1,
            'frontend_label'                => $replAttribute->getDescription() ?: $replAttribute->getCode(),
            'frontend_input'                => $frontendInput,
            'is_unique'                     => 0,
            'apply_to'                      => 0,
            'is_required'                   => 0,
            'is_configurable'               => 0,
            'is_searchable'                 => 1,
            'is_comparable'                 => 1,
            'is_user_defined'               => 1,
            'is_visible_in_advanced_search' => 0,
            'is_used_for_price_rules'       => 0,
            'is_wysiwyg_enabled'            => 0,
            'is_html_allowed_on_front'      => 1,
            'is_visible_on_front'           => 1,
            'used_in_product_listing'       => 0,
            'used_for_sort_by'              => 1,
            'backend_type'                  => 'varchar',
            'backend_model'                 => ArrayBackend::class,
            'is_filterable'                 => ($frontendInput === 'multiselect') ? 1 : 0,
            'is_filterable_in_search'       => ($frontendInput === 'multiselect') ? 1 : 0
        ];
Was it helpful?

Solution

It's disabled because it is a multiselect attribute. I don't believe core Magento allows this attribute type for sorting.

It's disabled by following js code line 115 - 122 in vendor/magento/module-swatches/view/adminhtml/web/js/product-attribute.js

                if (this.frontendInput.val() === 'multiselect' ||
                    this.frontendInput.val() === 'gallery' ||
                    this.frontendInput.val() === 'textarea'
                ) {
                    this._disable(this.usedForSortBy);
                } else {
                    this._enable(this.usedForSortBy);
                }

I assume the reason is because if a product has more than one option selected, how will Magento/Elasticsearch know where to place it in the results? If you need sorting for multiselect, you would need to create a custom module to handle it.

OTHER TIPS

Create using this below way and update value based on your requirement. It's working fine in M2.4.0

app/code/RH/Helloworld/Setup/Patch/Data/CreateCustomAttr.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
/**
 * Created By : Rohan Hapani
 */
declare (strict_types = 1);
namespace RH\Helloworld\Setup\Patch\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
/**
 * Class CreateCustomAttr for Create Custom Product Attribute using Data Patch.
 */
class CreateCustomAttr implements DataPatchInterface {
    /**
     * ModuleDataSetupInterface
     *
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * EavSetupFactory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory          $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * {@inheritdoc}
     */
    public function apply() {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute('catalog_product', 'product_temp_attribute', [
            'type' => 'text',
            'backend' => '',
            'frontend' => '',
            'label' => 'Product Temp Atrribute',
            'input' => 'text',
            'class' => '',
            'source' => '',
            'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to' => '',
        ]);
    }
    /**
     * {@inheritdoc}
     */
    public static function getDependencies() {
        return [];
    }
    /**
     * {@inheritdoc}
     */
    public function getAliases() {
        return [];
    }
}

Reference : Click here

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top