0

create the observer sucessfully the add the create store code but there is an error "Something went wrong while saving the customer." enter image description here

the code is given below-

namespace Namespace\Modulename\Observer;
 use Magento\Framework\Event\Observer as EventObserver;
 use Magento\Framework\Event\ObserverInterface;

use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Store\Model\GroupFactory;
use Magento\Store\Model\ResourceModel\Group;
use Magento\Store\Model\ResourceModel\Store;
use Magento\Store\Model\ResourceModel\Website;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\WebsiteFactory;


 class CustomerSaveAfterEvent implements ObserverInterface
{
    protected $_request;
    protected $_layout;
    protected $_objectManager = null;
    protected $_customerGroup;
    private $logger;
    protected $_customerRepositoryInterface;


    private $websiteFactory;
    private $websiteResourceModel;
    private $storeFactory;
    private $groupFactory;
    private $groupResourceModel;
    private $storeResourceModel;
    private $eventManager;

    private $_customerFactory;


public function __construct(
    \Magento\Framework\View\Element\Context $context,
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    \Magento\Customer\Model\CustomerFactory $customerFactory,

    WebsiteFactory $websiteFactory,
    Website $websiteResourceModel,
    Store $storeResourceModel,
    Group $groupResourceModel,
    StoreFactory $storeFactory,
    GroupFactory $groupFactory,
    ManagerInterface $eventManager
){
    $this->_layout = $context->getLayout();
    $this->_request = $context->getRequest();
    $this->_objectManager = $objectManager;
    $this->logger = $logger;
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
    $this->_customerFactory = $customerFactory;

    $this->websiteFactory = $websiteFactory;
    $this->websiteResourceModel = $websiteResourceModel;
    $this->storeFactory = $storeFactory;
    $this->groupFactory = $groupFactory;
    $this->groupResourceModel = $groupResourceModel;
    $this->storeResourceModel = $storeResourceModel;
    $this->eventManager = $eventManager;
}

public function execute(EventObserver $observer)
{   
    $event = $observer->getEvent();
    $customer = $observer->getCustomerDataObject();
    $customerId = $customer->getId();
    $customer->getFirstName();  
    $customerData = $this->_customerRepositoryInterface->getById($customerId);
    $is_admin_approved = $customerData->getCustomAttribute('is_admin_approved')->getValue();

    if($is_admin_approved == 1){

        $customer_data = $this->_customerFactory->create()->getCollection()
                        ->addFieldToFilter('entity_id', array('eq' => $customerId))
                        ->getData();    
        $customer_group_id = $customer_data[0]['group_id'];

        if($customer_group_id == 4){

            $seller_store_code = 'seller_store_'.$customerId;
            $seller_store_name = 'Seller Store '.$customerId;

            $website = $this->websiteFactory->create();
            $website->load('base');

            if($website->getId()){
                $group = $this->groupFactory->create();
                $group->setWebsiteId($website->getWebsiteId());
                $group->setName($seller_store_name);
                $group->setRootCategoryId(2);
                $group->setDefaultStoreId(3);
                $this->groupResourceModel->save($group);
            }

            $store = $this->storeFactory->create();

            $store->load('my_custom_store_code');
            if(!$store->getId()){
                $group = $this->groupFactory->create();
                $group->load('Seller', 'name');
                $store->setCode($seller_store_code);
                $store->setName($seller_store_name);
                $store->setWebsite($website);
                $store->setGroupId($group->getId());
                $store->setData('is_active','1');
                $this->storeResourceModel->save($store);
                $this->eventManager->dispatch('store_add', ['store' => $store]);
            } 
        }
    }

   }
 }
  • can you share which event you are using? – Qaisar Satti Jan 9 at 11:32
  • customer_save_after_data_object – sdeveloper Jan 9 at 14:17
1

change event from

customer_save_after_data_object

In this event data not save yet. you can't update data in this event.

to

customer_save_commit_after

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.