0

Is there a way to update the Order ID prefix based on the user's customer group? E.G. wholesale customer starts with 2 while retail starts with 1.

4 Answers 4

0

For that, you need to use sales_order_place_after event observer and in observer file using customer session you can fatch or set group id as per you requirement on order prefix.

0

Yes, it is possible to update the order ID prefix based on the user's customer group in Magento 2 with code, by using the sales_order_place_after. Here's an example of how you could accomplish this:

  1. Create a new module and register it in Magento.
  2. Create a new observer that listens for the sales_order_place_after event
  3. In the observer's execute method, retrieve the customer group of the current order
  4. Based on the customer group, set a new prefix for the order ID using the setIncrementId method on the order model.
  5. Save the order using the save method on the order model

Here's an example of what the observer's execute method might look like:

public function execute(Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    $customerGroupId = $order->getCustomerGroupId();
    $prefix = '';
    switch ($customerGroupId) {
        case 1:
            $prefix = 'A';
            break;
        case 2:
            $prefix = 'B';
            break;
        // Add additional cases as needed
    }

    $incrementId = $order->getIncrementId();
    $newIncrementId = $prefix . $incrementId;
    $order->setIncrementId($newIncrementId);
    $order->save();
}

You will need to make sure that the observer is properly configured in your module's events.xml file.

It's important to note that, the sales_order_place_after event is triggered after the order is placed. Because of that, you will not be able to cancel the order if the prefix is incorrect.

Let me know if any query.

THANKS.

0

There are two methods to add prefix based on the user's customer group on orderId,

Methode 1: Event Observer

Step 1: Create the file in app/code/Vendor/Module/etc/event.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="add_prefix_to_order_id"
                  instance="Vendor\Module\Observer\PrefixOrderNumber" />
    </event>
</config>

Step 2: Create the file in app/code/Vendor/Module/Observer/PrefixOrderNumber.php


<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;

class PrefixOrderNumber implements ObserverInterface
{
    /**
     * Set Prefix to OrderId
     *
     * {@inheritdoc}
     */
    public function execute(Observer $observer)
    {
        $orderInstance = $observer->getEvent()->getOrder();
        /** @var Order $orderInstance */
        $incrementId = $orderInstance->getIncrementId(); // Order Increment Id
        $groupId  = $orderInstance->getCustomerGroupId();
        $prefix = $groupId ; // Add Your Custom Prefix Here

        $newIncrementId = $prefix . '-' . $incrementId;

        $orderInstance->setIncrementId($newIncrementId);
        $orderInstance->save();
    }
}

Methode 2: Plugin

Step 1: Create the file in app/code/Vendor/Module/etc/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\OrderManagementInterface">
        <plugin name="MyOrderPlaceAfterPlugin"
                type="Vendor\Module\Plugin\Order\PlaceAfterPlugin" sortOrder="99" />
    </type>
</config>

Step 2: Create the file in app/code/Vendor/Module/PlaceAfterPlugin.php


<?php

namespace Vendor\Module\Plugin\Order;

use Exception;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderManagementInterface;

class PlaceAfterPlugin
{
    /**
     * After Order Place
     *
     * @param OrderManagementInterface $subject
     * @param OrderInterface $result
     * @return OrderInterface
     * @throws Exception
     */
    public function afterPlace(OrderManagementInterface $subject, OrderInterface $result)
    {
        $orderId = $result->getIncrementId(); // Order Increment Id
        $groupId = $result->getCustomerGroupId();
        $prefix = $groupId; // Add Your Custom Prefix Here

        $newOrderId = $prefix . '-' . $orderId;
        if ($orderId) {

            $result->setIncrementId($newOrderId);
            $result->save();
        }
        return $result;
    }
}

Let me know if any query.

Thanks!!

0

Step 1: Create etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

 <type name="Magento\Sales\Api\OrderManagementInterface">
        <plugin name="before_place_order_operation" type="Sunarc\Demo\Plugin\OrderManagement"/>
</type>

</config>

Step 2: Create Plugin/OrderManagement.php

<?php

namespace Sunarc\Demo\Plugin;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderManagementInterface;

/**
 * Class OrderManagement
 */
class OrderManagement
{
    protected $_customerGroupCollection;

    public function __construct(
        \Magento\Customer\Model\Group $customerGroupCollection,

    ) {
        $this->_customerGroupCollection = $customerGroupCollection;
    }
    /**
     * @param OrderManagementInterface $subject
     * @param OrderInterface           $order
     *
     * @return OrderInterface[]
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function beforePlace(OrderManagementInterface $subject, OrderInterface $order)
    {
        $quoteId = $order->getQuoteId();

        $collection = $this->_customerGroupCollection->load($order->getData('customer_group_id'));
        $collection->getCustomerGroupCode();
        if ($collection->getCustomerGroupCode() == "General") {
            $order->setData("increment_id","G".$order->getData("increment_id"));
            
        }
        else{
            $order->setData("increment_id","W".$order->getData("increment_id"));
        }
 
        return [$order];
    }
}

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.