سؤال

$_product->setData($attributeCode, $attributeValue);

I can assign "normal" attributes to product like that and those attributes are visible on product page and admin panel. If I want to assign multiselect attribute it does not work.

I tried to save it with random ids and just values but nothing works.

 $_product->setData($multiselectAttributeCode, '1,2,3');
 $_product->setData($multiselectAttributeCode, ['option1', 'option2', 'option3']);

When I create multiselect attribute (code below) multiselect attribute name is visible in stores/attibutes/product tab and also is visible in panel products/catalog (it is empty without values/options). I can not see it directly on product page even if try to save it like "normal" attribute.

        $multiSelectData = ['option1', 'option2', 'option3', 'option4', 'option5'];

        $data = array(
        'attribute_set'           => 'Default',
        'group'                   => 'Autosettings',
        'type'                    => 'text',
        'backend'                 => '',
        'frontend'                => '',
        'label'                   => $attributeLabel,
        'input'                   => 'text',
        'class'                   => '',
        'source'                  => '',
        'global'                  => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
        'visible'                 => true,
        'required'                => false,
        'user_defined'            => true,
        'default'                 => '',
        'searchable'              => true,
        'filterable'              => true,
        'comparable'              => true,
        'visible_on_front'        => true,
        'used_in_product_listing' => true,
        'unique'                  => false,
        'apply_to'                => '',
        'is_used_in_grid'         => true,
        'is_visible_in_grid'      => true,
        'is_filterable_in_grid'   => true,
    );

    //if attribute should be multiselect change attribute data
    if (!$multiSelectData === false and is_array($multiSelectData)) {
        $data['input'] = 'multiselect';
        $data['option'] = array('values' => $multiSelectData);
    }
    $eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY, $attributeCode, $data);

Let say I want to assign multiselect attribute with values

option1, option2, option3 to Product1,

option3, option4 to Product2.

Only those values are visible on product page. Is there way to do it?

هل كانت مفيدة؟

المحلول

After a lot of digging I found a solution why my attribute values wasn't able to be assigned to product. In "if" add:

$data['backend'] = 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend';

Whole if:

if (!$multiSelectData === false && is_array($multiSelectData)) {
        $data['input'] = 'multiselect';
        $data['option'] = array('values' => $multiSelectData);
        $data['backend'] = 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend';
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top