0

I need to module that is able to attachment a serial number to each product sold in Magento 2

3
  • Can you provide a little more info? Are you saving the serial number to each product and then needing that info to pass to the order for each product?
    – AJK
    Feb 1, 2019 at 14:39
  • Yes I want to add serial number when we place order
    – Gagan
    Feb 2, 2019 at 7:34
  • added how to get a product attribute from the order, not sure if you meant that you need to set the product attribute value, but that should get you going.
    – AJK
    Feb 2, 2019 at 15:16

1 Answer 1

0

This will show you how to get product info from the order using a Module / Observer:

First create your module in app/code/Vendor/Module

Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"></module>
</config>

Vendor/Module/registration.php

<?php    
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Next create an event to fire after every order:

Vendor/Module/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="checkout_submit_all_after">
        <observer name="vendor_module_get_product_info" instance="Vendor\Module\Observer\GetProductInfo"/>
    </event>
</config>

And lastly, create your observer that will grab your product info after every order:

Vendor/Module/Observer/GetProductInfo.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class GetProductInfo implements ObserverInterface
{      
  public function execute(\Magento\Framework\Event\Observer $observer)
  {
    $order = $observer->getEvent()->getOrder();

    try {
      foreach ($order->getAllItems() as $item) {
        // your product logic
        $item->getProductAttribute();
      }         
    } catch (\Exception $e) {
      // Handle your errors
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.