Hi,
Actually, on the MySQL table's "sales_order_item", there is an information about the product item, (like sku, name, weight, qty_ordered, product_type, is_virtual), but nothing on 'attribute_set_name' because the column / field of 'attribute_set_name' do not exist on this MySQL's table.
Do you know if you have a solution through the administration panel or I have to create a module?
For now, I'm trying to create a new module for this:
c:\xampp\htdocs\magentoLocal\app\code\TestOrder\AddAttributeSet
I need 2 step :
1step : to add the column 'attribute_set_name' into the "sales_order_item"
my registration file :
registration.php (...\TestOrder\AddAttributeSet\)
<?php /** * CREATE REGISTRATION FILE */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'TestOrder_AddAttributeSet', __DIR__ );
My mdoule file :
module.xml (...\TestOrder\AddAttributeSet\etc\)
<?xml version="1.0"?> <!-- /** * CREATE A MODULE FILE. */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="TestOrder_AddAttributeSet" setup_version="0.0.1"> </module> </config>
To add the column 'attribute_set_name' into the "sales_order_item" I put the schema file :
db_schema.xml (...\TestOrder\AddAttributeSet\etc\)
<?php /** * TO ADD AN PRODUCT ATTRIBUTE (A NEW COLUMN) [attribute_set_name] INTO THE MYSQL TABLE SALES_ORDER_ITEM) : * VIA AN UPGRADE SCRIPT. * Add the Column dans la table 'sales_order_item' * Using $setup->getConnection()->addColumn() */ namespace TestOrder\AddAttributeSet\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; class UpgradeSchema implements UpgradeSchemaInterface { /** * Upgrades DB schema for a module * * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $orderTable = 'sales_order_item'; $setup->getConnection() ->addColumn( $setup->getTable($orderTable), 'attribute_set_name', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'length' => 255, 'comment' =>'attribute set namer' ] ); $setup->endSetup(); } }
My table Upgrade File
UpgradeSchema.php (...\TestOrder\AddAttributeSet\Setup\)
<?php /** * TO ADD AN PRODUCT ATTRIBUTE (A NEW COLUMN) [attribute_set_name] INTO THE MYSQL TABLE SALES_ORDER_ITEM) : * VIA AN UPGRADE SCRIPT. * Add the Column dans la table 'sales_order_item' * Using $setup->getConnection()->addColumn() */ namespace TestOrder\AddAttributeSet\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; class UpgradeSchema implements UpgradeSchemaInterface { /** * Upgrades DB schema for a module * * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $orderTable = 'sales_order_item'; $setup->getConnection() ->addColumn( $setup->getTable($orderTable), 'attribute_set_name', [ 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 'length' => 255, 'comment' =>'attribute set namer' ] ); $setup->endSetup(); } }
2cn step : to put the right information : this field must have the correct information when there is an order recorded in this table.
I have no idea how I can do this ? You can help me, please.
Hi @tontonodildcbe
If column has been created successfully with table. Please create observer 'sales_order_place_after' which can help you to set data for your custom field by using following observer code:
Create event.xml "app\code\Vendor\Extension\etc\events.xml":
<?xml version="1.0"?> <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="Vendor\Extensoin\Observer\OrderObserver" /> </event> </config>
Create observer file - app\code\Vendor\Extension\Observer\OrderObserver.php
<?php namespace Vendor\Extension\Observer; use Magento\Framework\Event\ObserverInterface; class OrderObserver implements ObserverInterface { public function execute(\Magento\Framework\Event\Observer $observer) { /* Add your functionality */ } }
If issue resolve, please click on 'Kudos' & Accept as Solution!
Hi Bhanu Periwal,
Thanks for help.
I don't know which step I must use for add the right information into the field of 'attribute_set_name' on the MySQL table's "sales_order_item" via OrderObserver.php script (this column has been created successfull on the "sales_order_item").
I thinks
I tried several test on 1st step but I didn't get the "attribute set_name"'s information.
here is my tests :
public function execute(\Magento\Framework\Event\Observer $observer) { /* Add your functionality */ if (!is_dir('C:/xampp/htdocs/magento232/var/logDataOdilon/')) { mkdir('C:/xampp/htdocs/magento232/var/logDataOdilon/' ); } /* access the product being saved like this*/ //did not work : something went wrong with your request. Please try again later. $order = $observer->getEvent(); $products = $order->getProduct(); $attributeSetName = $products->getDataByKey('attribute_set_name'); file_put_contents('C:/xampp/htdocs/magento232/var/logDataOdilon/attributeName___text.log', $attributeSetName, FILE_APPEND | LOCK_EX); file_put_contents('C:/xampp/htdocs/magento232/var/logDataOdilon/attributeName__arr_text.log', var_dump($attributeSetName), FILE_APPEND | LOCK_EX); }
I have an error : PHP Fatal error: Uncaught Error: Call to a member function getDataByKey() on null
or
public function execute(\Magento\Framework\Event\Observer $observer) { /* Add your functionality */ if (!is_dir('C:/xampp/htdocs/magento232/var/logDataOdilon/')) { mkdir('C:/xampp/htdocs/magento232/var/logDataOdilon/' ); } /* access the product being saved like this*/ //did not work : something went wrong with your request. Please try again later. // $order = $observer->getEvent(); // $products = $order->getProduct(); // $attributeSetName = $products->getDataByKey('attribute_set_name'); // file_put_contents('C:/xampp/htdocs/magento232/var/logDataOdilon/attributeName___text.log', $attributeSetName, FILE_APPEND | LOCK_EX); // file_put_contents('C:/xampp/htdocs/magento232/var/logDataOdilon/attributeName__arr_text.log', var_dump($attributeSetName), FILE_APPEND | LOCK_EX); $order = $observer->getData('order'); $attributeSetName = $order('attribute_set_name'); //did not work file_put_contents('C:/xampp/htdocs/magento232/var\logDataOdilon\attributeSetName2arr_text.log', var_dump($attributeSetName), FILE_APPEND | LOCK_EX); }
I have an error : Uncaught Error: Function name must be a string
another test :
public function execute(\Magento\Framework\Event\Observer $observer) { /* Add your functionality */ if (!is_dir('C:/xampp/htdocs/magento232/var/logDataOdilon/')) { mkdir('C:/xampp/htdocs/magento232/var/logDataOdilon/' ); } /* access the product being saved like this*/ //did not work : something went wrong with your request. Please try again later. // $order = $observer->getEvent(); // $products = $order->getProduct(); // $attributeSetName = $products->getDataByKey('attribute_set_name'); // file_put_contents('C:/xampp/htdocs/magento232/var/logDataOdilon/attributeName___text.log', $attributeSetName, FILE_APPEND | LOCK_EX); // file_put_contents('C:/xampp/htdocs/magento232/var/logDataOdilon/attributeName__arr_text.log', var_dump($attributeSetName), FILE_APPEND | LOCK_EX); // $order = $observer->getData('order'); // $attributeSetName = $order('attribute_set_name'); // //did not work // file_put_contents('C:/xampp/htdocs/magento232/var\logDataOdilon\attributeSetName2arr_text.log', var_dump($attributeSetName), FILE_APPEND | LOCK_EX); $order = $observer->getData('order'); $orderItems = $order->getItems(); foreach ($orderItems as $orderItem) { file_put_contents('C:/xampp/htdocs/magento232/var\logdataodilon\orderitem1arr_text.log', var_dump($orderitem), file_append | lock_ex); file_put_contents('C:/xampp/htdocs/magento232/var\logdataodilon\orderitem1_text.log', $orderitem, file_append | lock_ex); } }
What I must do it ?
Thnaks