Magento2 – Update Customer’s Custom Attribute After Order Placement

attributescustomer-attributemagento2

I created a customer attribute called "new_customer" with a value of 1 YES.

Once an order is successfully placed by the customer, I want update the customer's custom_attribute "new_customer" to 0(NO)

{
"id": 28,
"group_id": 1,
"created_at": "2021-07-21 18:39:00",
"updated_at": "2021-07-21 19:16:44",
"created_in": "Default Store View",
"email": "[email protected]",
"firstname": "NewMitchell2",
"lastname": "NewThompson22",
"gender": 0,
"store_id": 1,
"website_id": 1,
"addresses": [],
"disable_auto_group_change": 0,
"extension_attributes": {
    "company_attributes": {
        "customer_id": 28,
        "company_id": 0
    },
    "is_subscribed": false
},
"custom_attributes": [
    {
        "attribute_code": "company_reg",
        "value": "Jane Cffsdfo222"
    },
    {
        "attribute_code": "has_purchased",
        "value": "0"
    },
    {
        "attribute_code": "new_customer",
        "value": "1"
    },
    {
        "attribute_code": "override_importer_of_record",
        "value": "default"
    }
]

}

enter image description here

enter image description here

Best Answer

You can use checkout_onepage_controller_success_action observer.

In your module create following file

app/code/Vendor/Mymodule/etc/frontend/events.xml

with following content

<?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_onepage_controller_success_action">
        <observer name="vendor_mymodule_observer_frontend_checkout_onepagecontrollersuccessaction_checkout_onepage_controller_success_action" instance="Vendor\Mymodule\Observer\Frontend\Checkout\OnepageControllerSuccessAction"/>
    </event>
</config>

Then create the following file

Observer/Frontend/Checkout/OnepageControllerSuccessAction.php

    <?php
    
    declare(strict_types=1);
    
    namespace Vendor\Mymodule\Observer\Frontend\Checkout;
    use Magento\Sales\Api\OrderRepositoryInterface;
    use Magento\Customer\Api\CustomerRepositoryInterface;

    class OnepageControllerSuccessAction implements \Magento\Framework\Event\ObserverInterface
    {

    protected $orderRepositoryInterface;

    protected $customerRepositoryInterface;


    public function __construct(
        CustomerRepositoryInterface $customerRepositoryInterface,
        OrderRepositoryInterface $orderRepositoryInterface
    ) {
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        $this->orderRepositoryInterface = $orderRepositoryInterface;
    }
        /**
         * Execute observer
         *
         * @param \Magento\Framework\Event\Observer $observer
         * @return void
         */
        public function execute(
            \Magento\Framework\Event\Observer $observer
        ) {
            $order = $this->orderRepositoryInterface->get((int) $observer->getEvent()->getOrderIds()[0]);

            $customer = $this->customerRepositoryInterface->getById($order->getCustomerId());
            if ($customer->getData('new_customer') == '1') {
                $customer->setData('new_customer') == '0';
            }
        }
    }
Related Topic