0

I have created a Multi-Select Product attribute programmatically but my options are not being saved. Vendor/CustomProductAttributes/Setup/Patch/Data/AddAllowedCountriesAttribute.php

<?php

namespace Vendor\CustomProductAttributes\Setup\Patch\Data;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Vendor\CustomProductAttributes\Model\Config\Product\Options;

class AddAllowedCountriesAttribute implements DataPatchInterface
{
    /** @var ModuleDataSetupInterface */
    private $moduleDataSetup;

    /** @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', 'sd_allowed_shipping_countries', [
            'type' => 'text',
            'label' => 'Allowed Shipping Countries',
            'input' => 'multiselect',
            'source' => Options::class, 
            'user_defined' => 1,
            'is_global' => 1,
            'is_visible_in_grid' => 1,
            'is_filterable_in_grid' => 1,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'position' => 2,
            'visible_on_front' => 1,

        ]);
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

My Vendor/CustomProductAttributes/Model/Config/Product/Options.php looks like that.

<?php

namespace Vendor\CustomProductAttributes\Model\Config\Product;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class Options extends AbstractSource
{
    protected $optionFactory;
    public function getAllOptions()
    {
        $this->_options = [];
        $this->_options[] = ['label' => 'No restrictions', 'value' => 'No restrictions'];
        $this->_options[] = ['label' => 'Czech Republic', 'value' => 'Czech Republic'];
        $this->_options[] = ['label' => 'Denmark', 'value' => 'Denmark'];
        $this->_options[] = ['label' => 'Finland', 'value' => 'Finland'];
        $this->_options[] = ['label' => 'France', 'value' => 'France'];
        $this->_options[] = ['label' => 'Germany', 'value' => 'Germany'];
    }
}

Can anyone tell me what I can do so the Options will be saved?
Thanks!

1
  • Did you try printing it in your save controller and see if it's passed there or not? – Abdul Samad Abbasi Dec 16 '20 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.