#StackBounty: #magento2 #magento2.3 Duplicate orders created when proforma disabled programmatically via module Magento 2.3

Bounty: 250

In my custom module I’m using a custom customer attribute called disable_proforma_email, and using a plugin to stop the proforma email being sent if it is set to yes. This is working (or at least i thought it was) until i noticed that it is creating duplicate orders. If someone was to order 10 products it creates 1 orders every couple of seconds until it hits the amount of items they ordered originally (so until it creates a 10th order)

I’m not sure why this is happening, can anyone help?

etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="MagentoSalesModelOrderEmailContainerOrderIdentity">
        <plugin name="change_is_enable_method" type="XXBlockProformaPluginSalesOrderEmailContainerOrderIdentityPlugin"/>
    </type>
</config>

Plugin/Sales/Order/Email/Container/OrderIdentityPlugin.php

<?php

namespace XXBlockProformaPluginSalesOrderEmailContainer;
use MagentoCustomerApiCustomerRepositoryInterface;

class OrderIdentityPlugin
{
    /**
     * @var MagentoCheckoutModelSession $checkoutSession
     */
    protected $checkoutSession;

    protected $resourceConnection;

    protected $_customer;

    protected $customerRepository;

    /**
     * @param MagentoCheckoutModelSession $checkoutSession
     *
     * @codeCoverageIgnore
     */
    public function __construct(
        MagentoCheckoutModelSession $checkoutSession,
        MagentoFrameworkAppResourceConnection $resourceConnection,
        CustomerRepositoryInterface $customerRepository, 
        MagentoCustomerModelCustomer $customer
    )
    {
        $this->checkoutSession = $checkoutSession;
        $this->resourceConnection = $resourceConnection;
        $this->customerRepository = $customerRepository;
        $this->_customer = $customer;
    }

    /**
     * @param MagentoSalesModelOrderEmailContainerOrderIdentity $subject
     * @param callable $proceed
     * @return bool
     */
    public function aroundIsEnabled(MagentoSalesModelOrderEmailContainerOrderIdentity $subject, callable $proceed)
    {
        $returnValue = $proceed();

        $order = $this->checkoutSession->getLastRealOrder();
        $orderId=$order->getEntityId();  // order id
        
        $connection = $this->resourceConnection->getConnection();
        $sql = "select customer_id FROM sales_order where entity_id = " . $orderId;
        $result = $connection->fetchAll($sql);
        $customerId = null;
        foreach($result as $cid){
            $customerId = $cid["customer_id"];
        }

        $customerGetValue = $this->customerRepository->getById($customerId);
        $dpe = $customerGetValue->getCustomAttribute('disable_proforma_email')->getValue();

        if($dpe == 1){
            return false;
        }
        else{
            return true;
        }

    }
}

The functionality of the proforma not sending or sending depending on the attribute is working but of course I need it to stop duplicating orders.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.