3. Now Create Function To Send Email
Now create controller function to send email-
/app/code/VendorName/ModuleName/Controller/Index/SendCustomMail.php
Magento 2 Mail Function Example:
_request = $request;
$this->_transportBuilder = $transportBuilder;
$this->_storeManager = $storeManager;
parent::__constr
parent::__construct($context);
}
public function execute()
{
$store = $this->_storeManager->getStore()->getId();
$transport = $this->_transportBuilder->setTemplateIdentifier('modulename_mytemplate')
->setTemplateOptions(['area' => 'frontend', 'store' => $store])
->setTemplateVars(
[
'store' => $this->_storeManager->getStore(),
]
)
->setFrom('general')
// you can config general email address in Store -> Configuration -> General -> Store Email Addresses
->addTo('john@example.com', 'John Dee')
->getTransport();
$transport->sendMessage();
return $this;
}
}
|
So you set your preferences in email template such as – set email template identifier, customer email, store, set from etc.
Magento 2 Logout Customer Programmatically
Magento 2 Logout Customer Programmatically– Sometimes we need to logout customer programmatically based upon some condition or validation. Here in this article we are going to explain how you can logout customer from block, controller, model, observer or anywhere.
Magento 2 Logout Customer Programmatically Example
You can use object manager to logout customer session simply as below –
Magento 2 Logout Customer Programmatically Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
$customerSession->_logout();
echo "Logged Out Successfully!";
}
|
The above example will logout the current customer session.
Magento 2 Get Set Session Variable
Magento 2 Get Set Session Variable– We can use object manager to create sessions in magento2. Here in this tutorial, we are going to explain how you can set, Unset session variable on phtml, controller block or model.
Magento 2 Get Set Session Variable | Unset Example
We can manage session variables in Magento using object manager simply as below-
Set Session Variable – Set Catalog | Customer | Checkout Session
You can set Catalog,Customer and Checkout session simply as below-
Magento 2 Get Set Session Variable Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$catalogSession = $objectManager->get('\Magento\Catalog\Model\Session');
$customerSession = $objectManager->get('\Magento\Customer\Model\Session');
$checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
// set session variable
$catalogSession->setMyTestVariable1('ABC');
$checkoutSession->setMyTestVariable2('DEF');
$customerSession->setMyTestVariable3('GHI');
|
Get Session Variable – Get Catalog | Customer | Checkout Session
You can get Catalog,Customer and Checkout session simply as below-
Magento 2 Get Set Session Variable Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$catalogSession = $objectManager->get('\Magento\Catalog\Model\Session');
$customerSession = $objectManager->get('\Magento\Customer\Model\Session');
$checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
// sget session variable
$catalogSession->getMyTestVariable1();
$checkoutSession->setMyTestVariable2();
$customerSession->setMyTestVariable3();
|
Unset Session Variable
You can destroy session variable in magento2 simply as below-
Destroy Session Variable Example:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$catalogSession = $objectManager->get('\Magento\Catalog\Model\Session');
$customerSession = $objectManager->get('\Magento\Customer\Model\Session');
$checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
// sget session variable
$catalogSession->unsMyTestVariable1();
$checkoutSession->unsMyTestVariable2();
$customerSession->unsMyTestVariable3();
|
Magento 2 show cart items count in header
Magento 2 show cart items count in header We can use Magento\Checkout\Helper\Cart to get the items count in header.
Magento 2 show cart items count in header Example
You can get the total number of items and display count in header simply as below-
Magento 2 show cart items count in header Example:
$counter = $this->helper('\Magento\Checkout\Helper\Cart');
echo $counter->getItemsCount();
|
Magento 2 Format Price
Magento 2 Format Price – We can use number_format function to format price in Magento2 upto decimal 2,3 digits. Here in this tutorial we are going to explain how to format price in Magento 2.
Magento 2 Format Price Example
You can use number_format function to format the price in Magento 2. Here is an example of Number formatting in Magento 2.
Magento 2 Format Price to 2, 3 decimal places Example:
//upto two decimal places
echo number_format($product->getPrice(), '2', '.', ',');
//upto three decimal places
echo number_format($product->getPrice(), '3', '.', ',')
|
Magneto 2 Bulk Update
Magneto 2 Bulk Update Products– We need bulk update for inventory such as products, category etc.
Magneto 2 Bulk Update Products | Inventry Example
You can use magento 2 admin import functionality to update the inventory but it updates the inventory one by one and it is slower so you can use Magmi – How to use magmi steps
Magento 2 Update Customer Programmatically
Magento 2 Update Customer Programmatically Sometimes we need to update customer information programmatically, It is very simple to update customer information in Magento2. Here in this tutorial we are going to explain how you can Update Customer Data programmaticaly.
Magento 2 Update Customer Programmatically | Attribute Value Update Example

You can use the dependency injection in controller & update customer information(attribute) programmatically-
Magento 2 Update Customer Attribute & Data Programmatically Example:
protected $_customerRepoInterface;
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepoInterface
) {
$this->_customerRepoInterface = $customerRepoInterface;
}
/************Update Part ********/
public function execute()
{
$websiteId = 2;
$customer = $this->_customerRepoInterface->get('jhon@example.com', $websiteId);
// UpdateCustomer Info
$customer->setFirstname('Jhon');
$customer->setLastname('Dee');
$customer->setEmail('jhon2@example.com');
$customer->setMyCustomAttribute('abc');
$this->_customerRepoInterface->save($customer);
}
|
So using the above method you can update customer data such as firstname, lastname, telephone, email or any custom attribute.
Magento 2 Load another Template in Template
Magento 2 Load another Template in Template Sometimes we need to load a template phtml file another phtml file. Here in this tutorial, we are going to explain how you can load a phtml file in another phtml template.
Magento 2 Load another Template in Templatem | Magento 2 Example
You can load another template phtml in any phtml simply as below –
Magento 2 Load another Template in Template Example:
getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Magento_Theme::html/MyTemplate.phtml")->toHtml();?>
|
You can use the above code to load any template file in any template.Q
|