Solved! Go to Solution.
Merci pour votre solution,
J'ai résolu mon problème en modifiant mon observer.
Maintenant j'ai bien tous mes éléments.
Merci,
FranckSub
Here is the solution:
Create events.xml at app\code\Vendor\Extension\etc\
<?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="checkout_cart_product_add_after"> <observer name="extension_checkout_cart_product_add_after" instance="Vendor\Extension\Observer\CheckoutCartAddObserver" /> </event> <event name="sales_model_service_quote_submit_before"> <observer name="extesnionadd" instance="Vendor\Extension\Observer\AddOptionToOrderObserver" /> </event> </config>Create CheckoutCartAddObserver.php at app\code\Vendor\Extension\Observer\
namespace Vendor\Extension\Observer; use Magento\Framework\Event\Observer as EventObserver; use Magento\Framework\Event\ObserverInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\Framework\View\LayoutInterface; use Magento\Framework\App\RequestInterface; use Magento\Framework\Serialize\SerializerInterface; class CheckoutCartAddObserver implements ObserverInterface { protected $layout; protected $storeManager; protected $request; private $serializer; public function __construct( StoreManagerInterface $storeManager, LayoutInterface $layout, RequestInterface $request, SerializerInterface $serializer ) { $this->layout = $layout; $this->storeManager = $storeManager; $this->request = $request; $this->serializer = $serializer; } public function execute(EventObserver $observer) { $item = $observer->getQuoteItem(); $post = $this->request->getPost(); $additionalOptions = array(); if ($additionalOption = $item->getOptionByCode('additional_options')) { $additionalOptions = $this->serializer->unserialize($additionalOption->getValue()); } $additionalOptions[] = [ 'label' => 'Size', 'value' => 'XL' ]; if (count($additionalOptions) > 0) { $item->addOption(array( 'product_id' => $item->getProductId(), 'code' => 'additional_options', 'value' => $this->serializer->serialize($additionalOptions) )); } } }Create AddOptionToOrderObserver.php at app\code\Vendor\Extension\Observer\
<?php namespace Vendor\Extension\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Serialize\SerializerInterface; class AddOptionToOrderObserver implements ObserverInterface { private $serializer; public function __construct( SerializerInterface $serializer ) { $this->serializer = $serializer; } public function execute(\Magento\Framework\Event\Observer $observer) { if ($this->helperData->isModuleEnabled()) { $quote = $observer->getQuote(); $order = $observer->getOrder(); foreach ($quote->getAllVisibleItems() as $quoteItem) { $quoteItems[$quoteItem->getId()] = $quoteItem; } foreach ($order->getAllVisibleItems() as $orderItem) { $quoteItemId = $orderItem->getQuoteItemId(); $quoteItem = $quoteItems[$quoteItemId]; $additionalOptions = $quoteItem->getOptionByCode('additional_options'); if (count($additionalOptions) > 0) { $options = $orderItem->getProductOptions(); $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue()); $orderItem->setProductOptions($options); } } return $this; } } }
Merci pour votre solution,
J'ai résolu mon problème en modifiant mon observer.
Maintenant j'ai bien tous mes éléments.
Merci,
FranckSub
Hello @francksub ,
please refer to the following link in order to solve this issue
https://www.siphor.com/adding-custom-attributes-to-magento-2-quotes-and-orders/
Hope this helps you!
Problem Solved! Click Kudos & Accept as Solution!
I need same solution in english
Hello,
Offering a highly informative checkout page and process to your online customers is essential. Magento 2 Order Attribute is exactly that. This new and special solution allows you to add custom attributes to your online store.
Read the full blog:- Order Attribute for Magento 2 To add Custom Field To Order
Hi @sohal2906
If you want information about Magento 2 Order attributes in English then you can discuss Webkul expert.