0

How to create the plugin for customer registration for create post get the inserted last id in magento2.

I tried with an example but it is working.

can you please help me?

di.xml

<type name="Magento\Customer\Controller\Account\CreatePost">
    <plugin name="createpost"
            type="Vendor\Module\Plugin\Customer\CreatePost"/>
</type>

controller/action/createpost

public function aroundExecute(
    \Magento\Customer\Controller\Account\CreatePost $subject,
    \Closure $proceed
)
{
    // i need the last inserted id 
}
3
  • do you mean you want to customer Primary Id which generated for them? Dec 29, 2021 at 9:28
  • yes i need primaryid created
    – mks
    Dec 29, 2021 at 9:30
  • If answer help you, upvote and accepted as solutions. so it will help other in community. Dec 29, 2021 at 10:38

2 Answers 2

0

In your plugin need to try this code.

$customer = $this->accountManagement ->createAccount($customer, $password, $redirectUrl);

After above line try this one.

$customerId = $customer->getId();

0

Try Below Code

app/code/VendoerName/ModuleName/etc/frontend

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\Customer\Controller\Account\CreatePost">
        <plugin name="after_create_account_get_id" type="VendoerName\ModuleName\Plugin\CreatePostPlugin" sortOrder="1" />
    </type>
</config>

app/code/VendoerName/ModuleName/Plugin

CreatePostPlugin.php

<?php

namespace VendoerName\ModuleName\Plugin;

class CreatePostPlugin
{
    protected $_customerSession;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->_customerSession = $customerSession;
    }

    public function afterExecute(
        \Magento\Customer\Controller\Account\CreatePost $subject,
        $result
    ) {
        echo $this->_customerSession->getCustomer()->getId();
        exit();
        return $result;
    }
}

Note: Your last customer id is your current customer ID. Because in last you have created this customer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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