Placing order tooo late in Magento 2 (Performance Issue)












0















We have different customer groups like Retail, Not logged, MD and DP. After placing the order we are changing the order number with the Prefix of customer groups. For changing the order number, we have followed below steps.



1) Get the Customer Group



2) Get the Random Order Number



3) Set the Prefix with Random Order Number (example: N-345433)



4) Check the Order Number is exist or Not. If exists again execute Step 3.



5) Set the Order Number to Order Factory



6) Saving the Order



the same we followed the below code with the help of sales_order_place_after observer.



<?php

namespace ABCSolutionsCustomOrderNumberObserver;

use MagentoFrameworkEventObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoSalesModelOrder;
use MagentoSalesModelOrderAddress;
use MagentoFrameworkAppResourceConnection;

class ChangeOrderId implements ObserverInterface
{

/**
* @var MagentoFrameworkAppConfigScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var MagentoFrameworkAppResourceConnection
*/
protected $resource;

/**
* @var PsrLogLoggerInterface
*/
protected $logger;

/**
* @var ABCSolutionsCustomOrderNumberModelOrderidFactory
*/
protected $OrderidobjFactory;

/**
* @var MagentoSalesModelResourceModelOrderCollectionFactory
*/
protected $_orderCollectionFactory;

/** @var MagentoSalesModelOrderStatusHistoryFactory $historyFactory */
protected $historyFactory;


/** @var MagentoSalesModelOrderFactory $orderFactory */
protected $orderFactory;


public function __construct(
PsrLogLoggerInterface $logger,
ABCSolutionsCustomOrderNumberModelOrderidFactory $Orderidobj,
ResourceConnection $resource,
MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoSalesModelOrderFactory $orderFactory,
MagentoSalesModelOrderStatusHistoryFactory $historyFactory
) {
$this->resource = $resource;
$this->logger = $logger;
$this->scopeConfig = $scopeConfig;
$this->OrderidobjFactory = $Orderidobj;
$this->_orderCollectionFactory = $orderCollectionFactory;
$this->orderFactory = $orderFactory;
$this->historyFactory = $historyFactory;
}

public function execute(Observer $observer)
{

try{
if($this->scopeConfig->getValue('custom_orders/custom_order/active', MagentoStoreModelScopeInterface::SCOPE_STORE))
{
/** @var MagentoSalesModelOrder */
$orderInstance = $observer->getOrder();

$prefix = "";

if($orderInstance->getCustomerGroupId() == 0){//Not Logged Customers
$prefix = $this->scopeConfig->getValue('custom_orders/not_logged_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
}elseif($orderInstance->getCustomerGroupId() == 1){ // Retails Customers
$prefix = $this->scopeConfig->getValue('custom_orders/retail_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
}elseif($orderInstance->getCustomerGroupId() == 2){ // MD Customers
$prefix = $this->scopeConfig->getValue('custom_orders/md_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
}elseif($orderInstance->getCustomerGroupId() == 3){ //DP Customers
$prefix = $this->scopeConfig->getValue('custom_orders/dp_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
}

$OrderNumber = $orderInstance->getIncrementId();

if (strpos($OrderNumber, $prefix) !== false) {
//return $this;
file_put_contents('ChangeOrderId.txt',print_r("n" . ' => ' . $OrderNumber,true),FILE_APPEND);
}else{

$randomNumber = mt_rand(10000000,99999999);
$incNo = $prefix . $randomNumber;
if($this->getSalesOrderCollection($incNo)){
$orderIdObj = $this->OrderidobjFactory->create()->load(1);

if(!$orderIdObj->getStartup()){
$orderIdObj = $this->OrderidobjFactory->create();
$orderIdObj->setData(array("startup"=>$randomNumber,"currentid"=>$randomNumber))->save();
}
else{
if($orderIdObj->getData("startup") != $randomNumber){
$orderIdObj->setStartup($randomNumber);
$orderIdObj->setCurrentid($randomNumber);
$orderIdObj->save();
}
else{
$randomNumber = (int)$orderIdObj->getData("currentid") + 1;
$orderIdObj->setCurrentid($randomNumber);
$orderIdObj->save();
}

}

$orderInstance = $observer->getOrder();
$orderInstance->setData("increment_id",$incNo)->save();

/* Order Comments and PO Number Starts */
$order_id = $orderInstance->getIncrementId();

/** @param MagentoSalesModelOrderFactory $order */
$order = $this->orderFactory->create()->load($orderInstance->getId());

$payment = $order->getPayment();
$method = $payment->getMethodInstance();
$methodTitle = $method->getTitle();

if($methodTitle == "PayPal"){

if(isset($_SESSION['po_number'])){
$po_number = $_SESSION['po_number'];
unset($_SESSION['po_number']);
}

if(isset($_SESSION['comments_note'])){
$comment = $_SESSION['comments_note'];
// remove any HTML tags
$comment = strip_tags($comment);
unset($_SESSION['comments_note']);
}

$order->setData('customer_note',$comment);
$order->setData('po_number',$po_number);
$order->save();

// make sure $order exists
if ($order->getData('entity_id')) {

/** @param string $status */
$status = $order->getData('status');

/** @param MagentoSalesModelOrderStatusHistoryFactory $history */
$history = $this->historyFactory->create();

// set comment history data
$history->setData('comment', $comment);
$history->setData('parent_id', $order->getId());
$history->setData('is_visible_on_front', 1);
$history->setData('is_customer_notified', 0);
$history->setData('entity_name', 'order');
$history->setData('status', $status);
$history->save();
}
}
}
}
}
} catch (Exception $e) {
file_put_contents('CustomOrderNumber.txt',print_r("n" . $e->getMessage(),true),FILE_APPEND);
}
}

public function getSalesOrderCollection($randomNumber){

$orderCollection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');
$orderCount = count($orderCollection) - 1;
$i = 0;
foreach($orderCollection as $order){
if($order->getIncrementId() == $randomNumber){
$this->getSalesOrderCollection($randomNumber);
}else{
if($orderCount == $i){
return true;
}
}
$i++;
}
}
}


Now we have 40k orders. When ever customer placing order we need to get the order collection and checking with order number weather exist or not?



It's taking too late to get the 40k orders and checking it's taking aroung 20 seconds each order.



If placing order more than one customer at a time it's very difficult to handle. If any of one facing same issue, please suggest to over come.



So is there any chance to over come above situation?



Please suggest us.









share



























    0















    We have different customer groups like Retail, Not logged, MD and DP. After placing the order we are changing the order number with the Prefix of customer groups. For changing the order number, we have followed below steps.



    1) Get the Customer Group



    2) Get the Random Order Number



    3) Set the Prefix with Random Order Number (example: N-345433)



    4) Check the Order Number is exist or Not. If exists again execute Step 3.



    5) Set the Order Number to Order Factory



    6) Saving the Order



    the same we followed the below code with the help of sales_order_place_after observer.



    <?php

    namespace ABCSolutionsCustomOrderNumberObserver;

    use MagentoFrameworkEventObserver;
    use MagentoFrameworkEventObserverInterface;
    use MagentoSalesModelOrder;
    use MagentoSalesModelOrderAddress;
    use MagentoFrameworkAppResourceConnection;

    class ChangeOrderId implements ObserverInterface
    {

    /**
    * @var MagentoFrameworkAppConfigScopeConfigInterface
    */
    protected $scopeConfig;

    /**
    * @var MagentoFrameworkAppResourceConnection
    */
    protected $resource;

    /**
    * @var PsrLogLoggerInterface
    */
    protected $logger;

    /**
    * @var ABCSolutionsCustomOrderNumberModelOrderidFactory
    */
    protected $OrderidobjFactory;

    /**
    * @var MagentoSalesModelResourceModelOrderCollectionFactory
    */
    protected $_orderCollectionFactory;

    /** @var MagentoSalesModelOrderStatusHistoryFactory $historyFactory */
    protected $historyFactory;


    /** @var MagentoSalesModelOrderFactory $orderFactory */
    protected $orderFactory;


    public function __construct(
    PsrLogLoggerInterface $logger,
    ABCSolutionsCustomOrderNumberModelOrderidFactory $Orderidobj,
    ResourceConnection $resource,
    MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
    MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
    MagentoSalesModelOrderFactory $orderFactory,
    MagentoSalesModelOrderStatusHistoryFactory $historyFactory
    ) {
    $this->resource = $resource;
    $this->logger = $logger;
    $this->scopeConfig = $scopeConfig;
    $this->OrderidobjFactory = $Orderidobj;
    $this->_orderCollectionFactory = $orderCollectionFactory;
    $this->orderFactory = $orderFactory;
    $this->historyFactory = $historyFactory;
    }

    public function execute(Observer $observer)
    {

    try{
    if($this->scopeConfig->getValue('custom_orders/custom_order/active', MagentoStoreModelScopeInterface::SCOPE_STORE))
    {
    /** @var MagentoSalesModelOrder */
    $orderInstance = $observer->getOrder();

    $prefix = "";

    if($orderInstance->getCustomerGroupId() == 0){//Not Logged Customers
    $prefix = $this->scopeConfig->getValue('custom_orders/not_logged_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
    }elseif($orderInstance->getCustomerGroupId() == 1){ // Retails Customers
    $prefix = $this->scopeConfig->getValue('custom_orders/retail_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
    }elseif($orderInstance->getCustomerGroupId() == 2){ // MD Customers
    $prefix = $this->scopeConfig->getValue('custom_orders/md_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
    }elseif($orderInstance->getCustomerGroupId() == 3){ //DP Customers
    $prefix = $this->scopeConfig->getValue('custom_orders/dp_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
    }

    $OrderNumber = $orderInstance->getIncrementId();

    if (strpos($OrderNumber, $prefix) !== false) {
    //return $this;
    file_put_contents('ChangeOrderId.txt',print_r("n" . ' => ' . $OrderNumber,true),FILE_APPEND);
    }else{

    $randomNumber = mt_rand(10000000,99999999);
    $incNo = $prefix . $randomNumber;
    if($this->getSalesOrderCollection($incNo)){
    $orderIdObj = $this->OrderidobjFactory->create()->load(1);

    if(!$orderIdObj->getStartup()){
    $orderIdObj = $this->OrderidobjFactory->create();
    $orderIdObj->setData(array("startup"=>$randomNumber,"currentid"=>$randomNumber))->save();
    }
    else{
    if($orderIdObj->getData("startup") != $randomNumber){
    $orderIdObj->setStartup($randomNumber);
    $orderIdObj->setCurrentid($randomNumber);
    $orderIdObj->save();
    }
    else{
    $randomNumber = (int)$orderIdObj->getData("currentid") + 1;
    $orderIdObj->setCurrentid($randomNumber);
    $orderIdObj->save();
    }

    }

    $orderInstance = $observer->getOrder();
    $orderInstance->setData("increment_id",$incNo)->save();

    /* Order Comments and PO Number Starts */
    $order_id = $orderInstance->getIncrementId();

    /** @param MagentoSalesModelOrderFactory $order */
    $order = $this->orderFactory->create()->load($orderInstance->getId());

    $payment = $order->getPayment();
    $method = $payment->getMethodInstance();
    $methodTitle = $method->getTitle();

    if($methodTitle == "PayPal"){

    if(isset($_SESSION['po_number'])){
    $po_number = $_SESSION['po_number'];
    unset($_SESSION['po_number']);
    }

    if(isset($_SESSION['comments_note'])){
    $comment = $_SESSION['comments_note'];
    // remove any HTML tags
    $comment = strip_tags($comment);
    unset($_SESSION['comments_note']);
    }

    $order->setData('customer_note',$comment);
    $order->setData('po_number',$po_number);
    $order->save();

    // make sure $order exists
    if ($order->getData('entity_id')) {

    /** @param string $status */
    $status = $order->getData('status');

    /** @param MagentoSalesModelOrderStatusHistoryFactory $history */
    $history = $this->historyFactory->create();

    // set comment history data
    $history->setData('comment', $comment);
    $history->setData('parent_id', $order->getId());
    $history->setData('is_visible_on_front', 1);
    $history->setData('is_customer_notified', 0);
    $history->setData('entity_name', 'order');
    $history->setData('status', $status);
    $history->save();
    }
    }
    }
    }
    }
    } catch (Exception $e) {
    file_put_contents('CustomOrderNumber.txt',print_r("n" . $e->getMessage(),true),FILE_APPEND);
    }
    }

    public function getSalesOrderCollection($randomNumber){

    $orderCollection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');
    $orderCount = count($orderCollection) - 1;
    $i = 0;
    foreach($orderCollection as $order){
    if($order->getIncrementId() == $randomNumber){
    $this->getSalesOrderCollection($randomNumber);
    }else{
    if($orderCount == $i){
    return true;
    }
    }
    $i++;
    }
    }
    }


    Now we have 40k orders. When ever customer placing order we need to get the order collection and checking with order number weather exist or not?



    It's taking too late to get the 40k orders and checking it's taking aroung 20 seconds each order.



    If placing order more than one customer at a time it's very difficult to handle. If any of one facing same issue, please suggest to over come.



    So is there any chance to over come above situation?



    Please suggest us.









    share

























      0












      0








      0








      We have different customer groups like Retail, Not logged, MD and DP. After placing the order we are changing the order number with the Prefix of customer groups. For changing the order number, we have followed below steps.



      1) Get the Customer Group



      2) Get the Random Order Number



      3) Set the Prefix with Random Order Number (example: N-345433)



      4) Check the Order Number is exist or Not. If exists again execute Step 3.



      5) Set the Order Number to Order Factory



      6) Saving the Order



      the same we followed the below code with the help of sales_order_place_after observer.



      <?php

      namespace ABCSolutionsCustomOrderNumberObserver;

      use MagentoFrameworkEventObserver;
      use MagentoFrameworkEventObserverInterface;
      use MagentoSalesModelOrder;
      use MagentoSalesModelOrderAddress;
      use MagentoFrameworkAppResourceConnection;

      class ChangeOrderId implements ObserverInterface
      {

      /**
      * @var MagentoFrameworkAppConfigScopeConfigInterface
      */
      protected $scopeConfig;

      /**
      * @var MagentoFrameworkAppResourceConnection
      */
      protected $resource;

      /**
      * @var PsrLogLoggerInterface
      */
      protected $logger;

      /**
      * @var ABCSolutionsCustomOrderNumberModelOrderidFactory
      */
      protected $OrderidobjFactory;

      /**
      * @var MagentoSalesModelResourceModelOrderCollectionFactory
      */
      protected $_orderCollectionFactory;

      /** @var MagentoSalesModelOrderStatusHistoryFactory $historyFactory */
      protected $historyFactory;


      /** @var MagentoSalesModelOrderFactory $orderFactory */
      protected $orderFactory;


      public function __construct(
      PsrLogLoggerInterface $logger,
      ABCSolutionsCustomOrderNumberModelOrderidFactory $Orderidobj,
      ResourceConnection $resource,
      MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      MagentoSalesModelOrderFactory $orderFactory,
      MagentoSalesModelOrderStatusHistoryFactory $historyFactory
      ) {
      $this->resource = $resource;
      $this->logger = $logger;
      $this->scopeConfig = $scopeConfig;
      $this->OrderidobjFactory = $Orderidobj;
      $this->_orderCollectionFactory = $orderCollectionFactory;
      $this->orderFactory = $orderFactory;
      $this->historyFactory = $historyFactory;
      }

      public function execute(Observer $observer)
      {

      try{
      if($this->scopeConfig->getValue('custom_orders/custom_order/active', MagentoStoreModelScopeInterface::SCOPE_STORE))
      {
      /** @var MagentoSalesModelOrder */
      $orderInstance = $observer->getOrder();

      $prefix = "";

      if($orderInstance->getCustomerGroupId() == 0){//Not Logged Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/not_logged_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }elseif($orderInstance->getCustomerGroupId() == 1){ // Retails Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/retail_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }elseif($orderInstance->getCustomerGroupId() == 2){ // MD Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/md_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }elseif($orderInstance->getCustomerGroupId() == 3){ //DP Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/dp_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }

      $OrderNumber = $orderInstance->getIncrementId();

      if (strpos($OrderNumber, $prefix) !== false) {
      //return $this;
      file_put_contents('ChangeOrderId.txt',print_r("n" . ' => ' . $OrderNumber,true),FILE_APPEND);
      }else{

      $randomNumber = mt_rand(10000000,99999999);
      $incNo = $prefix . $randomNumber;
      if($this->getSalesOrderCollection($incNo)){
      $orderIdObj = $this->OrderidobjFactory->create()->load(1);

      if(!$orderIdObj->getStartup()){
      $orderIdObj = $this->OrderidobjFactory->create();
      $orderIdObj->setData(array("startup"=>$randomNumber,"currentid"=>$randomNumber))->save();
      }
      else{
      if($orderIdObj->getData("startup") != $randomNumber){
      $orderIdObj->setStartup($randomNumber);
      $orderIdObj->setCurrentid($randomNumber);
      $orderIdObj->save();
      }
      else{
      $randomNumber = (int)$orderIdObj->getData("currentid") + 1;
      $orderIdObj->setCurrentid($randomNumber);
      $orderIdObj->save();
      }

      }

      $orderInstance = $observer->getOrder();
      $orderInstance->setData("increment_id",$incNo)->save();

      /* Order Comments and PO Number Starts */
      $order_id = $orderInstance->getIncrementId();

      /** @param MagentoSalesModelOrderFactory $order */
      $order = $this->orderFactory->create()->load($orderInstance->getId());

      $payment = $order->getPayment();
      $method = $payment->getMethodInstance();
      $methodTitle = $method->getTitle();

      if($methodTitle == "PayPal"){

      if(isset($_SESSION['po_number'])){
      $po_number = $_SESSION['po_number'];
      unset($_SESSION['po_number']);
      }

      if(isset($_SESSION['comments_note'])){
      $comment = $_SESSION['comments_note'];
      // remove any HTML tags
      $comment = strip_tags($comment);
      unset($_SESSION['comments_note']);
      }

      $order->setData('customer_note',$comment);
      $order->setData('po_number',$po_number);
      $order->save();

      // make sure $order exists
      if ($order->getData('entity_id')) {

      /** @param string $status */
      $status = $order->getData('status');

      /** @param MagentoSalesModelOrderStatusHistoryFactory $history */
      $history = $this->historyFactory->create();

      // set comment history data
      $history->setData('comment', $comment);
      $history->setData('parent_id', $order->getId());
      $history->setData('is_visible_on_front', 1);
      $history->setData('is_customer_notified', 0);
      $history->setData('entity_name', 'order');
      $history->setData('status', $status);
      $history->save();
      }
      }
      }
      }
      }
      } catch (Exception $e) {
      file_put_contents('CustomOrderNumber.txt',print_r("n" . $e->getMessage(),true),FILE_APPEND);
      }
      }

      public function getSalesOrderCollection($randomNumber){

      $orderCollection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');
      $orderCount = count($orderCollection) - 1;
      $i = 0;
      foreach($orderCollection as $order){
      if($order->getIncrementId() == $randomNumber){
      $this->getSalesOrderCollection($randomNumber);
      }else{
      if($orderCount == $i){
      return true;
      }
      }
      $i++;
      }
      }
      }


      Now we have 40k orders. When ever customer placing order we need to get the order collection and checking with order number weather exist or not?



      It's taking too late to get the 40k orders and checking it's taking aroung 20 seconds each order.



      If placing order more than one customer at a time it's very difficult to handle. If any of one facing same issue, please suggest to over come.



      So is there any chance to over come above situation?



      Please suggest us.









      share














      We have different customer groups like Retail, Not logged, MD and DP. After placing the order we are changing the order number with the Prefix of customer groups. For changing the order number, we have followed below steps.



      1) Get the Customer Group



      2) Get the Random Order Number



      3) Set the Prefix with Random Order Number (example: N-345433)



      4) Check the Order Number is exist or Not. If exists again execute Step 3.



      5) Set the Order Number to Order Factory



      6) Saving the Order



      the same we followed the below code with the help of sales_order_place_after observer.



      <?php

      namespace ABCSolutionsCustomOrderNumberObserver;

      use MagentoFrameworkEventObserver;
      use MagentoFrameworkEventObserverInterface;
      use MagentoSalesModelOrder;
      use MagentoSalesModelOrderAddress;
      use MagentoFrameworkAppResourceConnection;

      class ChangeOrderId implements ObserverInterface
      {

      /**
      * @var MagentoFrameworkAppConfigScopeConfigInterface
      */
      protected $scopeConfig;

      /**
      * @var MagentoFrameworkAppResourceConnection
      */
      protected $resource;

      /**
      * @var PsrLogLoggerInterface
      */
      protected $logger;

      /**
      * @var ABCSolutionsCustomOrderNumberModelOrderidFactory
      */
      protected $OrderidobjFactory;

      /**
      * @var MagentoSalesModelResourceModelOrderCollectionFactory
      */
      protected $_orderCollectionFactory;

      /** @var MagentoSalesModelOrderStatusHistoryFactory $historyFactory */
      protected $historyFactory;


      /** @var MagentoSalesModelOrderFactory $orderFactory */
      protected $orderFactory;


      public function __construct(
      PsrLogLoggerInterface $logger,
      ABCSolutionsCustomOrderNumberModelOrderidFactory $Orderidobj,
      ResourceConnection $resource,
      MagentoSalesModelResourceModelOrderCollectionFactory $orderCollectionFactory,
      MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
      MagentoSalesModelOrderFactory $orderFactory,
      MagentoSalesModelOrderStatusHistoryFactory $historyFactory
      ) {
      $this->resource = $resource;
      $this->logger = $logger;
      $this->scopeConfig = $scopeConfig;
      $this->OrderidobjFactory = $Orderidobj;
      $this->_orderCollectionFactory = $orderCollectionFactory;
      $this->orderFactory = $orderFactory;
      $this->historyFactory = $historyFactory;
      }

      public function execute(Observer $observer)
      {

      try{
      if($this->scopeConfig->getValue('custom_orders/custom_order/active', MagentoStoreModelScopeInterface::SCOPE_STORE))
      {
      /** @var MagentoSalesModelOrder */
      $orderInstance = $observer->getOrder();

      $prefix = "";

      if($orderInstance->getCustomerGroupId() == 0){//Not Logged Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/not_logged_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }elseif($orderInstance->getCustomerGroupId() == 1){ // Retails Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/retail_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }elseif($orderInstance->getCustomerGroupId() == 2){ // MD Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/md_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }elseif($orderInstance->getCustomerGroupId() == 3){ //DP Customers
      $prefix = $this->scopeConfig->getValue('custom_orders/dp_grp/prefix', MagentoStoreModelScopeInterface::SCOPE_STORE);
      }

      $OrderNumber = $orderInstance->getIncrementId();

      if (strpos($OrderNumber, $prefix) !== false) {
      //return $this;
      file_put_contents('ChangeOrderId.txt',print_r("n" . ' => ' . $OrderNumber,true),FILE_APPEND);
      }else{

      $randomNumber = mt_rand(10000000,99999999);
      $incNo = $prefix . $randomNumber;
      if($this->getSalesOrderCollection($incNo)){
      $orderIdObj = $this->OrderidobjFactory->create()->load(1);

      if(!$orderIdObj->getStartup()){
      $orderIdObj = $this->OrderidobjFactory->create();
      $orderIdObj->setData(array("startup"=>$randomNumber,"currentid"=>$randomNumber))->save();
      }
      else{
      if($orderIdObj->getData("startup") != $randomNumber){
      $orderIdObj->setStartup($randomNumber);
      $orderIdObj->setCurrentid($randomNumber);
      $orderIdObj->save();
      }
      else{
      $randomNumber = (int)$orderIdObj->getData("currentid") + 1;
      $orderIdObj->setCurrentid($randomNumber);
      $orderIdObj->save();
      }

      }

      $orderInstance = $observer->getOrder();
      $orderInstance->setData("increment_id",$incNo)->save();

      /* Order Comments and PO Number Starts */
      $order_id = $orderInstance->getIncrementId();

      /** @param MagentoSalesModelOrderFactory $order */
      $order = $this->orderFactory->create()->load($orderInstance->getId());

      $payment = $order->getPayment();
      $method = $payment->getMethodInstance();
      $methodTitle = $method->getTitle();

      if($methodTitle == "PayPal"){

      if(isset($_SESSION['po_number'])){
      $po_number = $_SESSION['po_number'];
      unset($_SESSION['po_number']);
      }

      if(isset($_SESSION['comments_note'])){
      $comment = $_SESSION['comments_note'];
      // remove any HTML tags
      $comment = strip_tags($comment);
      unset($_SESSION['comments_note']);
      }

      $order->setData('customer_note',$comment);
      $order->setData('po_number',$po_number);
      $order->save();

      // make sure $order exists
      if ($order->getData('entity_id')) {

      /** @param string $status */
      $status = $order->getData('status');

      /** @param MagentoSalesModelOrderStatusHistoryFactory $history */
      $history = $this->historyFactory->create();

      // set comment history data
      $history->setData('comment', $comment);
      $history->setData('parent_id', $order->getId());
      $history->setData('is_visible_on_front', 1);
      $history->setData('is_customer_notified', 0);
      $history->setData('entity_name', 'order');
      $history->setData('status', $status);
      $history->save();
      }
      }
      }
      }
      }
      } catch (Exception $e) {
      file_put_contents('CustomOrderNumber.txt',print_r("n" . $e->getMessage(),true),FILE_APPEND);
      }
      }

      public function getSalesOrderCollection($randomNumber){

      $orderCollection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');
      $orderCount = count($orderCollection) - 1;
      $i = 0;
      foreach($orderCollection as $order){
      if($order->getIncrementId() == $randomNumber){
      $this->getSalesOrderCollection($randomNumber);
      }else{
      if($orderCount == $i){
      return true;
      }
      }
      $i++;
      }
      }
      }


      Now we have 40k orders. When ever customer placing order we need to get the order collection and checking with order number weather exist or not?



      It's taking too late to get the 40k orders and checking it's taking aroung 20 seconds each order.



      If placing order more than one customer at a time it's very difficult to handle. If any of one facing same issue, please suggest to over come.



      So is there any chance to over come above situation?



      Please suggest us.







      magento2 orders payment performance





      share












      share










      share



      share










      asked 53 secs ago









      BojjaiahBojjaiah

      2,4082367




      2,4082367






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "479"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f259028%2fplacing-order-tooo-late-in-magento-2-performance-issue%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f259028%2fplacing-order-tooo-late-in-magento-2-performance-issue%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What other Star Trek series did the main TNG cast show up in?

          Berlina muro

          Berlina aerponto