I think it's a session problem but i'm not sure on where to look to fix it. The user does get logged in if i check it in a different tab.
The error log doesn't show any errors.
<?php
namespace BB\Checkout\Model\Checkout;
class ShippingInformationManagementPlugin
{
protected $quoteRepository;
/**
* @var \Magento\Newsletter\Model\SubscriberFactory
*/
protected $subscriberFactory;
/**
* @var \Magento\Customer\Api\AccountManagementInterface
*/
protected $accountManagement;
/**
* @var \Magento\Customer\Helper\Address
*/
protected $addressHelper;
/**
* @var \Magento\Customer\Model\Metadata\FormFactory
*/
protected $formFactory;
/**
* @var \Magento\Customer\Api\Data\RegionInterfaceFactory
*/
protected $regionDataFactory;
/**
* @var \Magento\Customer\Api\Data\AddressInterfaceFactory
*/
protected $addressDataFactory;
/**
* @var \Magento\Customer\Model\Registration
*/
protected $registration;
/**
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory
*/
protected $customerDataFactory;
/**
* @var \Magento\Customer\Model\Url
*/
protected $customerUrl;
/**
* @var \Magento\Framework\Escaper
*/
protected $escaper;
/**
* @var \Magento\Customer\Model\CustomerExtractor
*/
protected $customerExtractor;
/**
* @var \Magento\Framework\UrlInterface
*/
protected $urlModel;
/**
* @var \Magento\Framework\Api\DataObjectHelper
*/
protected $dataObjectHelper;
/**
* @var Session
*/
protected $session;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Customer\Model\CustomerFactory
*/
protected $customerFactory;
/**
* @var \Magento\Customer\Model\AddressFactory
*/
protected $addressFactory;
protected $customerModel;
protected $customerSession;
protected $abstractAccount;
public function __construct(
\Magento\Quote\Model\QuoteRepository $quoteRepository,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory,
\Magento\Framework\Event\Observer $observer,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory,
\Magento\Customer\Model\Customer $customerModel,
\Magento\Customer\Model\Session $customerSession,
\Magento\Customer\Controller\AbstractAccount $abstractAccount
)
{
$this->quoteRepository = $quoteRepository;
$this->subscriberFactory = $subscriberFactory;
$this->extensionAttributesFactory = $extensionAttributesFactory;
$this->observer = $observer;
$this->storeManager = $storeManager;
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
$this->customerModel = $customerModel;
$this->customerSession = $customerSession;
$this->abstractAccount = $abstractAccount;
}
/**
* @param \Magento\Checkout\Model\ShippingInformationManagement $subject
* @param $cartId
* @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
*/
public function beforeSaveAddressInformation(
\Magento\Checkout\Model\ShippingInformationManagement $subject,
$cartId,
\Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
)
{
$extAttributes = $addressInformation->getExtensionAttributes();
$email = $extAttributes->getEmail();
$subscribe = $extAttributes->getSubscribe();
// Subscribe user to newsletter
if ($subscribe === true) {
$subscriber = $this->subscriberFactory->create()->loadByEmail($email);
if ($subscriber->getId() && $subscriber->getSubscriberStatus() == \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED) {
} else {
$status = $this->subscriberFactory->create()->subscribe($email);
}
}
// Create account for user
if ($extAttributes->getPassword() !== '') {
// Create account
$websiteId = $this->storeManager->getWebsite()->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId);
$shippingAddress = $addressInformation->getShippingAddress();
$billingAddress = $addressInformation->getBillingAddress();
// Preparing data for new customer
$customer->setEmail($extAttributes->getEmail());
$customer->setPassword($extAttributes->getPassword());
$customer->setFirstname($shippingAddress->getFirstname());
$customer->setLastname($shippingAddress->getLastname());
try {
$customer->save();
// Save shipping address
$customerAddress = $this->addressFactory->create();
$customerAddress->setCustomerId($customer->getId());
$customerAddress->setFirstname($shippingAddress->getFirstname());
$customerAddress->setLastname($shippingAddress->getLastname());
$customerAddress->setCountryId($shippingAddress->getCountryId());
$customerAddress->setPostcode($**bleep**());
$customerAddress->setCity($shippingAddress->getCity());
$customerAddress->setTelephone($shippingAddress->getTelephone());
$customerAddress->setStreet($shippingAddress->getStreet());
$customerAddress->setCompany($shippingAddress->getCompany());
$customerAddress->setVatId($shippingAddress->getVatId());
if ($shippingAddress->getStreet() === $billingAddress->getStreet()) {
$customerAddress->setIsDefaultBilling('1');
}
$customerAddress->setIsDefaultShipping('1');
$customerAddress->setSaveInAddressBook('1');
$customerAddress->save();
// Create billing address
if ($shippingAddress->getStreet() !== $billingAddress->getStreet()) {
$customerAddress = $this->addressFactory->create();
$customerAddress->setCustomerId($customer->getId());
$customerAddress->setFirstname($billingAddress->getFirstname());
$customerAddress->setLastname($billingAddress->getLastname());
$customerAddress->setCountryId($billingAddress->getCountryId());
$customerAddress->setPostcode($billingAddress->getPostcode());
$customerAddress->setCity($billingAddress->getCity());
$customerAddress->setTelephone($billingAddress->getTelephone());
$customerAddress->setStreet($billingAddress->getStreet());
$customerAddress->setCompany($billingAddress->getCompany());
$customerAddress->setVatId($billingAddress->getVatId());
$customerAddress->setIsDefaultBilling('1');
$customerAddress->setSaveInAddressBook('1');
$customerAddress->save();
}
$this->customerSession->setCustomerAsLoggedIn($customer);
if($this->customerSession->isLoggedIn()) {
echo "Customer Logged in";
}else{
echo "customer is Not Logged in";
}
} catch (\Exception $e) {
// E-mail address is already added, this will occur if the customer goes back and forward in steps, the account is already created by then.
}
}
}
}