1

I want to create a configurable product according to my custom attribute but i can't find it when i pass create configurations here is my code

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,
            'is_configurable'=>1,
            '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 observer to stock options dynamically according to custom table data also i faced problem here instead of finding the just values of this product i found all the value of existing product how to manage this here is my code

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();






    if ($result) {
        $i = 0;
        foreach ($result as $elt) {
            /* new attribute option */

            $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]);

            $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.