0
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
        ];
2
  • Can you please upload your script and magento version? Apr 20, 2021 at 8:07
  • @RohanHapani updated. Apr 20, 2021 at 9:05

2 Answers 2

1

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.

1
0

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

1
  • I appreciate your response however this is not I’m looking for or asked. I need to understand on what conditions it is disabled. Apr 20, 2021 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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