Được rồi, sau một thời gian tôi đã tìm thấy một giải pháp trong trường hợp người khác cần nó .. Magento sử dụng một cách tiếp cận khác để khởi tạo các đối tượng, cách truyền thống để khởi tạo các đối tượng trong Magento 1.x là sử dụng "Mage :: getModel (..)", điều này đã thay đổi trong Magento 2. Bây giờ Magento sử dụng trình quản lý đối tượng để khởi tạo các phản đối, tôi sẽ không nhập chi tiết về cách thức hoạt động của nó .. vì vậy, mã tương đương để tạo khách hàng trong Magento 2 sẽ như thế này:
<?php
namespace ModuleNamespace\Module_Name\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Customer\Model\CustomerFactory
*/
protected $customerFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Customer\Model\CustomerFactory $customerFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Customer\Model\CustomerFactory $customerFactory
) {
$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
parent::__construct($context);
}
public function execute()
{
// Get Website ID
$websiteId = $this->storeManager->getWebsite()->getWebsiteId();
// Instantiate object (this is the most important part)
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
// Preparing data for new customer
$customer->setEmail("email@domain.com");
$customer->setFirstname("First Name");
$customer->setLastname("Last name");
$customer->setPassword("password");
// Save data
$customer->save();
$customer->sendNewAccountEmail();
}
}
Hy vọng đoạn mã này giúp người khác ..