首页>Magento>source

成功创建观察者添加创建商店代码,但出现错误"保存客户时出错了"。

代码如下 -

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]);
            } 
        }
    }
   }
 }
最新回答
  • 2019-12-5
    1 #

    更改活动
    customer_save_after_data_object
    

    在这种情况下,数据尚未保存.您无法在此活动中更新数据。

    customer_save_commit_after
    

  • magento2:Magento 2:如何添加BARDCODE
  • magento 1.9:注册成功页面