Send mail from custom module Magento 2

To send custom email in Magento 2 using the module, We need to create a custom module to send email to a specific recipient.

Out of the box, Magento Provides multiple mail template, like Sales Order, Customer mails, Contacts and so on.

If you want to send your custom mail functionality using Magento 2, You need to create a simple module and create a custom template file using Module for mail body.

Continue reading “Send mail from custom module Magento 2”

How to create custom tabs in product page magento 2?

Let’s create a product tab in Magento 2 product page using the simple module. We want to display short content for a product using product tabs.

Example, We want to display short description info in product tabs.
Start with the Simple module in Magento 2, create registration.php file to register our module.

Path, app/code/Rbj/ProductTabs/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Rbj_ProductTabs',
    __DIR__
);

File path, app/code/Rbj/ProductTabs/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Rbj_ProductTabs" setup_version="2.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

We have passed dependency of Catalog module in sequence tag of our module.
Now We want to create catalog_product_view.xml file for display our custom tabs of the short description.
Create a layout file,
Path, app/code/Rbj/ProductTabs/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product.info.details">
        	<!-- For short description tab -->
            <block class="Rbj\ProductTabs\Block\ExtraInfo" name="product.short.description" template="Rbj_ProductTabs::short-descripiton.phtml" group="detailed_info">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Extra Info</argument>
                </arguments>
            </block>
   		</referenceBlock>
   	</body>
</page>

We have keep our tab name is Extra Info if you want to change tabs title you can replace Extra info with your title in argument tag.  In the above layout file, We must need to pass group=”detailed_info”  in the block class.

Create template file for call short description content in tabs using template, Path, app/code/Rbj/ProductTabs/view/frontend/templates/short-descripiton.phtml

<?php $extraInfo = $block->getTabsContent(); ?>

<div class="short">
    <?php echo $extraInfo; ?>
</div>

We need to create ExtraInfo.php file to check if a product has short description we need to display tabs otherwise we don’t need to display empty tabs for the short description.

Path, app/code/Rbj/ProductTabs/Block/ExtraInfo.php

<?php

namespace Rbj\ProductTabs\Block;

class ExtraInfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
         array $data = []
    ) {
        $this->_coreRegistry = $context->getRegistry();
        parent::__construct($context, $data);
    }

    /**
     * Retrieve current product object
     *
     * @return \Magento\Catalog\Model\Product
     */
    public function getProduct()
    {
        if (!$this->hasData('product')) {
            $this->setData('product', $this->_coreRegistry->registry('product'));
        }
        return $this->getData('product');
    }

    /**
     * @return string
     */
    public function getTabsContent()
    {
        return $this->getProduct()->getShortDescription();
    }

    /**
     * @return return HTML
     */
    protected function _toHtml()
    {
        if(!empty($this->getTabsContent())) {
            return parent::_toHtml();
        }

        return false;
    }
}

Now run command to activate our module,

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

You can see new tabs on the product page in the tabs section below the image section. Your new tab will be displayed if your product has the short description otherwise tabs will not display in product page.
You can add new attribute details using the above way.

 

Magento 2 – How to get multiselect option in system configuration using system.xml.

Using below demo we can get multiselect option using system.xml.
For Example, We will get all Customer group of a system and display all the customer group by system.xml in System -> Configuration Section of admin.
Create system.xml file,
Path,   app/code/<Vendorname>/<Modulename>/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="catalog">
            <group id="customer_group" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="list" translate="label" type="multiselect" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Customer Groups</label>
                    <source_model>Vendorname\Modulename\Model\Adminhtml\System\Config\Source\Customer\Group</source_model>
                    <can_be_empty>1</can_be_empty>
                </field>
            </group>
        </section>
    </system>
</config>

You need to get the collection of all customer group using a Model file. Create Model file in your module. You can define your model php file using source_model tag in system.xml file.

Create Group.php file,
Path, app/code/<Vendorname>/<Modulename>/Model/Adminhtml/System/Config/Source/Customer/Group.php

<?php
namespace Vendorname\Modulename\Model\Adminhtml\System\Config\Source\Customer;

class Group implements \Magento\Framework\Option\ArrayInterface
{
    public function __construct(\Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory)
    {
        $this->_groupCollectionFactory = $groupCollectionFactory;
    }

    /**
     * @return array
     */
    public function toOptionArray()
    {
        if (!$this->_options) {
            $this->_options = $this->_groupCollectionFactory->create()->loadData()->toOptionArray();
        }
        return $this->_options;
    }
}

Clear Cache using php bin/magento cache:flush

Now You can get the list of customer group in your section with multi-select option.
If you want to get the list of all selected option in PHP file,

<?php
namespace Vendorname\Modulename\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context
    ) {
        $this->scopeConfig = $context->getScopeConfig();
        parent::__construct($context);
    }
    /**
     * Get Customer group selected list
     */
    public function getCustomerGroupList() {
        $list = $this->scopeConfig->getValue("catalog/customer_group/list",
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        return $list !== null ? explode(',', $list) : [];
    }

Using the above way you can list of all customer group selected in a Configuration section. The result will be a value of all selected customer group with comma separated.