- In case of the Guest customer, magento didn't map email id to
shipping or billing address before place order.
- In my case, I need guest email which is provided at the time of
shipping page. To validate Guest email with Coupon code using this
event ( salesrule_validator_process ) .
To Do that :
Step 1: Create di.xml to override core code.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Model\AccountManagement" type="Custom\Couponfriend\Model\AccountManagement" />
</config>
Step 2: Override model in custom module, and set email to
Quote's->shipping object. and get where you need it.
<?php
namespace Custom\Couponfriend\Model;
class AccountManagement extends \Magento\Customer\Model\AccountManagement {
public function isEmailAvailable($customerEmail, $websiteId = null) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$shippingAddress = $cart->getQuote()->getShippingAddress();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/guestFriendsFamily.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('------------');
try {
if ($shippingAddress) {
$shippingAddress->setData('email', $customerEmail);
$shippingAddress->save();
}
} catch (NoSuchEntityException $e) {
$logger->info($e->getMessage());
}
try {
if ($websiteId === null) {
$websiteId = $this->storeManager->getStore()->getWebsiteId();
}
$this->customerRepository->get($customerEmail, $websiteId);
return false;
} catch (NoSuchEntityException $e) {
return true;
}
}
}
?>
You can override constructor to create cart object using dependency injection