0

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.

  • What is the value on row 38? – PY Yick May 4 '18 at 9:36
  • row 38 is configurable product row. – zed Blackbeard May 4 '18 at 9:39
  • Seems look like you put all configurable options in one row. You should refer to the sample to create configurable product via import. – PY Yick May 4 '18 at 9:46
  • configurable option means -> additional attributes ? – zed Blackbeard May 4 '18 at 9:51
  • No, of course not. It just mean the individual product have additional attributes. – PY Yick May 4 '18 at 11:26
0

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

Your Answer

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

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