Question

I am stuck with the following error while creating custom dropdown in magento 2.3.5.

Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Catalog\Model\ResourceModel\Product\Collection::addIsSaleableAttributeToFilter() must be of the type array or null, int given, called in /opt/lampp/htdocs/M_3/vendor/magento/module-catalog/Model/ResourceModel/Product/Collection.php on line 1582 and defined in /opt/lampp/htdocs/M_3/vendor/magento/module-catalog/Model/ResourceModel/Product/Collection.php:2477 Stack trace: #0 /opt/lampp/htdocs/M_3/vendor/magento/module-catalog/Model/ResourceModel/Product/Collection.php(1582): Magento\Catalog\Model\ResourceModel\Product\Collection->addIsSaleableAttributeToFilter(1) #1 /opt/lampp/htdocs/M_3/generated/code/Magento/Catalog/Model/ResourceModel/Product/Collection/Interceptor.php(518): Magento\Catalog\Model\ResourceModel\Product\Collection->addAttributeToFilter('is_saleable', 1, 'left') #2 /opt/lampp/htdocs/M_3/app/code/Smartwave/Filterproducts/Block/FeaturedList.php(53): Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor->addAttributeToFilter('is_saleabl in /opt/lampp/htdocs/M_3/vendor/magento/module-catalog/Model/ResourceModel/Product/Collection.php on line 2477

Please help me to solve this error.

Was it helpful?

Solution

InstallData.php

<?php

namespace VendorName\ModuleName\Setup;

use Magento\Eav\Setup\EavSetup;
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;

/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
    $this->eavSetupFactory = $eavSetupFactory;
}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
            /** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            /**
            * Add attributes to the eav/attribute
            */

            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'custom_attribute',
                [
                    'group' => 'General',
                    'type' => 'int',
                    'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                    'frontend' => '',
                    'label' => 'Custom Select',
                    'input' => 'select',
                    'class' => 'custom_attribute_name',
                    'source' => \VendoreName\ModuleName\Model\AttributeList::class,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false,
                    'apply_to' => 'simple,configurable,virtual,bundle,downloadable'
                ]
            );
    }
}

create AttributeList.php file at VendoreName\ModuleName\Model

<?php

namespace VendoreName\ModuleName\Model;

class AttributeList extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

    public function getOptionArray()
    {
        $options = [];
        $options['0'] = __('New');
        $options['1'] = __('Process');
        $options['2'] = __('Reject');
        $options['3'] = __('Complete');
        return $options;
    }
    public function getAllOptions()
    {
        $res = $this->getOptions();
        array_unshift($res, ['value' => '', 'label' => '']);
        return $res;
    }
    public function getOptions()
    {
        $res = [];
        foreach ($this->getOptionArray() as $index => $value) {
            $res[] = ['value' => $index, 'label' => $value];
        }
        return $res;
    }
    public function toOptionArray()
    {
        return $this->getOptions();
    }
}

I Hope This Helps You

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