0

I created a new product attribut then i want to assign to it option dunamically before saving my product but i always found this error cannot save attribute 145 here is my code :

class InstallData implements InstallDataInterface { /** * EAV setup factory * * @var EavSetupFactory */ private $eavSetupFactory; /** * Init * * @param EavSetupFactory $eavSetupFactory */ public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory;

}

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{

    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

    $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'choix_prix_product');
    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'choix_prix_product',/* Custom Attribute Code */
        [
            'type' => 'text',/* Data type in which formate your value save in database*/
            'backend' => '',
            'frontend' => '',
            'label' => 'Choix-Prix de vente', /* lablel of your attribute*/
            'input' => 'select',
            'class' => '',
            'source' => '', /* Source of your select type custom attribute options*/
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => true,
            'visible_in_advanced_search' => true,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => true,
            'used_in_product_listing' => false,
            'unique' => false,
            'is_visible_in_grid' => false


        ]

    );
}

and here is my Class observer Editoptions.php

class Editoptions implements ObserverInterface

{ protected $_logger;

protected $_attributeRepository;

protected $_attributeOptionManagement;

protected $_option;

protected $_attributeOptionLabel;


public function __construct( \Psr\Log\LoggerInterface $logger,
                             \Magento\Eav\Model\AttributeRepository $attributeRepository,
                             \Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionManagement,
                             \Magento\Eav\Api\Data\AttributeOptionLabelInterface $attributeOptionLabel,
                             \Magento\Eav\Model\Entity\Attribute\Option $option

) {
    $this->_logger = $logger;
    $this->_attributeRepository = $attributeRepository;
    $this->_attributeOptionManagement = $attributeOptionManagement;
    $this->_option = $option;
    $this->_attributeOptionLabel = $attributeOptionLabel;
}


public function execute(\Magento\Framework\Event\Observer $observer)
{
    $_product = $observer->getProduct();  // you will get product object

   // $att=$this->createOrGetId('choix_prix_product', $label); for adding new option to our attribute

    $_sku = $_product->getSku(); // for sku

    $var1 = substr($_sku, 0, 1);


    $var2 = substr($_sku, 1, 2);

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
    $connection = $resource->getConnection();
    $tableName = $resource->getTableName('table_prix');


    $fields = array('prix_unitaire','prix_id');


    $sql = $connection->select()
        ->from($tableName, $fields)
        ->where('code_famille' . '=?', $var1)
        ->where('code_nom_commercial' . '=?', $var2)
        ->join('table_choix',
            'table_choix.choix_id = table_prix.code_choix',
            [
                'designation_choix'
            ]);

    $result = $connection->fetchAll($sql);

    $attribute_id = $this->_attributeRepository->get('catalog_product', 'choix_prix_product')->getAttributeId();


    /* new attribute option */



    if ($result) {
        $i = 0;
        foreach ($result as $elt) {

            $this->_option->setValue(" Choix" . $elt['designation_choix'] . "-" . $elt['prix_unitaire'] . "£");

/* $this->_attributeOptionLabel->setStoreId(0);*/ $this->_attributeOptionLabel->setLabel(" Choix" . $elt['designation_choix'] . "-" . $elt['prix_unitaire'] . "£"); $this->_option->setLabel($this->_attributeOptionLabel); $this->_option->setStoreLabels([$this->_attributeOptionLabel]); /var_dump($this->_option->getLabel()); var_dump($this->_option->getValue()); die('test');/

            $this->_attributeOptionManagement->add('catalog_product', $attribute_id, $this->_option);


            $i++;

        }




    } /*else {

        $this->_options = [['label' => __(' '), 'value' => '0']];
        $_product->setData('choix_prix_product', $this->_options);

    }*/


}

} any help please

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.