Domanda

I have added a product multiselect attribute with option group values programatically as follows

$eavSetup->addAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'print_and_locations',
    [
        'group' => 'Custom Configurations',
        'type' => 'varchar',
        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
        'frontend' => '',
        'label' => 'Print options',
        'input' => 'multiselect',
        'note' => 'Print options',
        'class' => '',
        'source' => 'Custom\Attribute\Model\Config\Source\Options',
        'global' => 0,
        'visible' => true,
        'required' => false,
        'user_defined' => true,
        'default' => null,
        'searchable' => false,
        'filterable' => false,
        'comparable' => false,
        'visible_on_front' => true,
        'used_in_product_listing' => false,
        'apply_to' => 'configurable',
        'unique' => false,
        'option' => [
            'values' => [],
        ]
    ]
);

This attribute options values contains the option values of two simple dropdown product attribute (ie. print and location ) values.

I am using following code to get the options

<?php
/**
 * Copyright © Blackbeard, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Custom\Attribute\Model\Config\Source;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;

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

  /**
   * instance of eav attribute config.
   * @var \Magento\Eav\Model\Config
   */
    public $eavConfig;

    public function __construct(
        \Magento\Eav\Model\Config $eavConfig
    ) {
        $this->eavConfig = $eavConfig;
    }

    /**
     * Get all options based on print method an locations product attributes
     *
     * @return array
     */
    public function getAllOptions()
    {

        $finalOption = [];
        // get print attribute values
        $print_attribute = $this->eavConfig->getAttribute('catalog_product', 'print');
        $printOptions = $print_attribute->getSource()->getAllOptions(false);
        // get logo location options values
        $location_attribute = $this->eavConfig->getAttribute('catalog_product', 'location');
        $locationOptions = $location_attribute->getSource()->getAllOptions(false);

        if (!empty($printOptions)) {
            foreach ($printOptions as $key => $print) {
                $locationvalues=[];
                if (!empty($locationOptions)) {
                    foreach ($locationOptions as $key => $location) {
                        $locationvalues[] = [
                        'label'=>$location['label'],
                        'value'=>$print['value'].'|'.$location['value']
                        ];
                    }
                }

                $finalOption[] = [
                'label'=>$print['label'],
                'value'=>$locationvalues
                ];
            }
        }
        return $finalOption;
    }
}

This is working fine.

enter image description here

I am working on prouct import. I am unable to import this attribute ie. print_and_locations attribute. I am adding attribute value in following format

print_and_locations=Group 1/Left Chest|Group 1/Left Sleeve|Group 1/Right Chest|Group 1/Right Sleeve|Group 2/Left Chest|Group 2/Left Sleeve|Group 2/Right Chest|Group 2/Right Sleeve|Group 3/Left Chest|Group 3/Left Sleeve|Group 3/Right Chest|Group 3/Right Sleeve

but during import it shows error like mention below.

enter image description here

PS : row no 38 is configurable product row.

Any help would be appreciated.

Thanks.

È stato utile?

Soluzione

I have solved this issue by changing in following file.

app\code\Custom\Attribute\Model\Config\Source\Options.php

<?php
/**
 * Copyright © Blackbeard, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Custom\Attribute\Model\Config\Source;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\OptionFactory;
use Magento\Framework\DB\Ddl\Table;

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

  /**
   * instance of eav attribute config.
   * @var \Magento\Eav\Model\Config
   */
    public $eavConfig;

    public function __construct(
        \Magento\Eav\Model\Config $eavConfig
    ) {
        $this->eavConfig = $eavConfig;
    }

    /**
     * Get all options based on print method an locations product attributes
     *
     * @return array
     */
    public function getAllOptions($flag=true)
    {

        $finalOption = [];
        // get print attribute values
        $print_attribute = $this->eavConfig->getAttribute('catalog_product', 'print');
        $printOptions = $print_attribute->getSource()->getAllOptions(false);
        // get logo location options values
        $location_attribute = $this->eavConfig->getAttribute('catalog_product', 'location');
        $locationOptions = $location_attribute->getSource()->getAllOptions(false);

        if (!empty($printOptions)) {
            foreach ($printOptions as $key => $print) {
                $locationvalues=[];
                if (!empty($locationOptions)) {
                    foreach ($locationOptions as $key => $location) {
                    // updated code start

                    if ($flag) {
                        $locationvalues[] = [
                        'label'=>$location['label'],
                        'value'=>$print['value'].'|'.$location['value']
                        ];
                      } else {
                        $locationvalues[] = [
                        'label'=>$print['label'].'/'.$location['label'],
                        'value'=>$print['value'].'|'.$location['value']
                        ];
                      }

                      // updated code end
                    }
                }

                $finalOption[] = [
                'label'=>$print['label'],
                'value'=>$locationvalues
                ];
            }
        }
        return $finalOption;
    }
}

Logic behind adding updated code is getting options values with label during different format from source model config options. During logo import validation it checks only the locations labels which are same in all the print options So I have passed the false argument and setup unique format options for that.

I am importing this attribute value in following format

print_and_locations=Group 1/Left Chest|Group 1/Left Sleeve|Group 1/Right Chest|Group 1/Right Sleeve|Group 2/Left Chest|Group 2/Left Sleeve|Group 2/Right Chest|Group 2/Right Sleeve|Group 3/Left Chest|Group 3/Left Sleeve|Group 3/Right Chest|Group 3/Right Sleeve

because magento render select option group values in html as follows

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top