1

How I can add a value to new column created by my module in the table 'sales_order_item'? I have a problem, could you help me ? I explain : In the Magento table, 'eav attribute set', the 'attribute set name' column exists. And it contains a value like this: Gear [with attribute_set_id 11] ; Bag [it's id 15]; Top ; etc.

The table, 'catalog_product_entity', continent also the column 'attribute_set_id', like the value 11 for 'Bag'.

But we have not any information on the 'attribute set name' in the 'sales_order_item' table.

My module created the new column, named, 'attribute_set_name' in the 'sales_order_item'. It works, it is funny.

But I don't know how to add this information on the product purchased in the table 'sales_order_item' when the order saves it in this table.

I tried

events.xml:

<?xml version="1.0"?>
<!--
/**
 *  apprentice.magento
 *  My observer events code : sales_order_place_after
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_after">
        <observer name="vendor_sales_order_place_after" instance="ApprenticeLearnerOrderMagento\AddNewColumn\Observer\OrderObserver" />
    </event>
</config>

OrderObserver.php :

    <?php
/**
 *  apprentice.magento
 *  My observer php code
 */
namespace ApprenticeLearnerOrderMagento\AddNewColumn\Observer;

use Magento\Framework\Event\ObserverInterface; 


class OrderObserver implements ObserverInterface
{
  protected $attributeSet;

  public function __construct(\Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSet) {
    $this->attributeSet = $attributeSet;
  }

  public function execute(\Magento\Framework\Event\Observer $observer){       

    $order = $observer->getEvent();
    $products = $order->getProduct();

    $attributeSetRepository = $this->attributeSet->get($products->getAttributeSetId());
    $attributeSetName = $attributeSetRepository->getAttributeSetName(); 

    //see if the variable [$attributeSetName] contains a value
    file_put_contents('C:/xampp/htdocs/magentoSiteRoot/var/logMyModuleAddNewColumn/nameAttribut.log', $attributeSetName, FILE_APPEND | LOCK_EX);
    file_put_contents('C:/xampp/htdocs/magentoSiteRoot/var/logMyModuleAddNewColumn/nameAttributprint.log', var_dump($attributeSetName), FILE_APPEND | LOCK_EX);
  }
}

but this does not work:


Error: Call to a member function getAttributeSetId() on null


When I click on 'Place Order' I have a this message on the web site :

Something went wrong with your request. Please try again later.

And when I check the error.log of apache [C:\xampp\apache\logs] :

[php7:error] [pid 6476:tid 1504] [client 127.0.0.1:51141] PHP Fatal error: Uncaught Error: Call to a member function getAttributeSetId() on null in C:\xampp\htdocs\magentoSiteRoot\app\code\ApprenticeLearnerOrderMagento\AddNewColumn\Observer\OrderObserver.php:24 \nStack trace:\n#0 C:\xampp\htdocs\magentoSiteRoot\vendor\magento\framework\Event\Invoker\InvokerDefault.php(88): TestOrder\AddAttributeSet\Observer\OrderObserver->execute(Object(Magento\Framework\Event\Observer))\n#1

C:\xampp\htdocs\magentoSiteRoot\vendor\magento\framework\Event\Invoker\InvokerDefault.php(74): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(TestOrder\AddAttributeSet\Observer\OrderObserver), Object(Magento\Framework\Event\Observer))\n#2 C:\xampp\htdocs\magentoSiteRoot\vendor\magento\framework\Event\Manager.php(66): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))\n#3 C:\xampp\htdocs\magentoSiteRoot\generated\code\Magento\Framework\Event\Manager\Proxy.php(95): Magento\Framework\Event\Manager->dispatch('sales_order_pla...', Array)\n in C:\xampp\htdocs\magentoSiteRoot\app\code\ApprenticeLearnerOrderMagento\AddNewColumn\Observer\OrderObserver.php on line 24, referer: http://magentoSiteRoot:81/checkout/

I nedd your help and Thanks

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.