Change store view automatically for different customer groups in Magento 2 - magento2

I'm currently using Magento 2.3.2 and I would like to show certain customers a specific store view based on their customer group. (For example a customer in the "General" group would see the default store view, while a customer in the "Platinum" group would see the "Platinum" store view with a slightly different logo and design).
Is there an extension out there that can do this? I can only find ones which restrict the products in the catalog?
Edit 20/02/2020 -
Thanks to Invigorate Systems for the solution. I have now implemented the code as below in the app > code folders:
registration.php file inside GroupSite/SiteSwitch/
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'GroupSite_SiteSwitch',
__DIR__
);
module.xml file inside GroupSite/SiteSwitch/etc/
<?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="GroupSite_SiteSwitch" setup_version="2.1.1"></module>
</config>
events.xml inside GroupSite/SiteSwitch/etc/frontend/
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
<event name="layout_load_before">
<observer name="add_layout_handles" instance="GroupSite\SiteSwitch\Observer\AddHandles" />
</event>
</config>
AddHandles.php file inside GroupSite/SiteSwitch/Observer
<?php
namespace GroupSite\SiteSwitch\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;
class AddHandles implements ObserverInterface
{
protected $customerSession;
protected $_storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
CustomerSession $customerSession
) {
$this->customerSession = $customerSession;
$this->_storeManager = $storeManager;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$layout = $observer->getEvent()->getLayout();
if ($this->customerSession->isLoggedIn())
{
$customerGroup = $this->customerSession->getCustomer()->getGroupId();
if($customerGroup === '5'){
$this->_storeManager->setCurrentStore('13'); //Set your desired store ID that you wish to set.
}
else{
$this->_storeManager->setCurrentStore('1');
}
}
}
}

You can do this by using Observers, here's an example module for you. this module will change store ID after customer login to the system.
Create registration.php file inside Vendor/Module/
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Create module.xml file inside Vendor/Module/etc/
<?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="Vendor_Module" setup_version="2.1.1"></module>
</config>
Create events.xml inside Vendor/Module/etc/frontend/
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
<event name="layout_load_before">
<observer name="add_layout_handles" instance="Vendor\Module\Observer\AddHandles" />
</event>
</config>
Create Handler file for Observer AddHandles.php file inside Vendor/Module/Observer
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;
class AddHandles implements ObserverInterface
{
protected $customerSession;
protected $_storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
CustomerSession $customerSession
) {
$this->customerSession = $customerSession;
$this->_storeManager = $storeManager;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$layout = $observer->getEvent()->getLayout();
if ($this->customerSession->isLoggedIn())
{
/*
Here you fetch loggedIn Customer Group and add if condition such as
if(customerGroup == 'ID/Name of group you desire'){
$this->_storeManager->setCurrentStore('2'); //Set your desired store ID that you wish to set.
}
*/
$this->_storeManager->setCurrentStore('2');
}
}
}

Related

Creating a admin controller using adminhtml id

I'm trying to add a controller accessible on admin menu through url:
https://dev.m2t2.com/admin_k1tgag/admin/helloWorld/index/key/0195fab99cc865bb756a77e8fe5ceedb6f8eee97de91d569398d383cef4f0d81/
Generated by the XML code inserted below. But it keeps returning
Invalid security or form key. Please refresh the page.
Router.xml :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="adminhtml">
<module name="Study_Admin" before="Magento_Backend"/>
</route>
</router>
</config>
In menu i inserted:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Study_Admin::greetings" title="Greetings" translate="title" module="Study_Admin" parent="Magento_Backend::content" sortOrder="50" dependsOnModule="Study_Admin" resource="Study_Admin::greetings"/>
<add id="Study_Admin::greetings_helloworld" title="Hello World" translate="title" module="Study_Admin" parent="Study_Admin::greetings" sortOrder="10" dependsOnModule="Study_Admin" action="adminhtml/helloWorld" resource="Study_Admin::greetings"/>
</menu>
</config>
But when i access the controller through the menu i have no success. I started debugging and i checked that non-custom controllers extends \Magento\Backend\App\Action class to pass validations inside magento routing core flow. I did the same but i still have no success.
Below my controller class:
<?php
namespace Study\Controller\Adminhtml\HelloWorld;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\App\Action\Context;
use ‌Magento\Framework\App\ActionInterface;
class Index extends \Magento\Backend\App\Action
{
const MENU_Id = "Study_Admin::greetings_helloworld";
protected $resultPageFActory;
public function __construct(Context $context, PageFactory $resultPageFActory)
{
parent::__construct($context);
$this->resultPageFActory = $resultPageFActory;
}
public function execute()
{
$resultPage = $this->resultPageFActory->create();
$resultPage->setActiveMenu(static::MENU_Id);
$resultPage->getConfig()->getTitle()->prepend(__('Hello World'));
return $resultPage;
// TODO: Implement execute() method.
}
}
The file structure is :
Thanx in advance, and take care.
It works... was just my namespace in Controller's class that had an error
'namespace Study\Admin\Controller\Adminhtml\Helloworld;'

Magento 2 How to change "Grand Total to be Charged" text in order emails

In Magento 2 how to change "Grand Total to be Charged" text in emails?
Text is found in this file vendor/magento/module-sales/Block/Order/Totals.php
How to override this file to my theme or module?
Thanks
Santosh
To override this class, you need to create your own class and extend the original class.
You need to declare override class in the di.xml file. Follow the steps below:
app/code/MilanDev/ExtendsCore/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="MilanDev_ExtendsCore" setup_version="1.0.0">
<sequence>
<module name="Magento_Sales"/>
</sequence>
</module>
</config>
app/code/MilanDev/ExtendsCore/etc/di.xml
<?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\Sales\Block\Order\Totals" type="MilanDev\ExtendsCore\Rewrite\Magento\Sales\Block\Order\Totals"/>
</config>
app/code/MilanDev/ExtendsCore/Rewrite/Magento/Sales/Block/Order/Totals.php
<?php
namespace MilanDev\ExtendsCore\Rewrite\Magento\Sales\Block\Order;
class Totals extends \Magento\Sales\Block\Order\Totals
{
/**
* Initialize order totals array
*
* #return $this
*/
protected function _initTotals()
{
// ...........
if ($this->getOrder()->isCurrencyDifferent()) {
$this->_totals['base_grandtotal'] = new \Magento\Framework\DataObject(
[
'code' => 'base_grandtotal',
'value' => $this->getOrder()->formatBasePrice($source->getBaseGrandTotal()),
'label' => __('Your custom text'), // changed
'is_formated' => true,
]
);
}
// ........
}
}

How get configurable by Simple Product Magento 2 rest API

The question is simple, how can I get the Configurable Product through a Simple product through the REST API Magento 2?
I'm using the following call to get the simple product:
http://127.0.0.1/magento2/index.php/rest/V1/products/prdConfig-RED
Thank you
I have created a new module which accepts child product id as parameter and returns the parent product id and other attributes like name , thumbnail....
registeration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'WebAPI_GetParentProductThumbnail',
__DIR__
);
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="WebAPI_GetParentProductThumbnail" setup_version="1.0.0"/>
</config>
etc/di.xml
<?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="WebAPI\GetParentProductThumbnail\Api\ChildThumbnailManagementInterface" type="WebAPI\GetParentProductThumbnail\Model\ChildThumbnailManagement"/>
</config>
etc/webapi.xml
<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route method="GET" url="/V1/webapi-getparentproductthumbnail/childthumbnail">
<service class="WebAPI\GetParentProductThumbnail\Api\ChildThumbnailManagementInterface" method="getChildThumbnail"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
Api/ChildThumbnailManagementInterface.php
<?php
namespace WebAPI\GetParentProductThumbnail\Api;
interface ChildThumbnailManagementInterface
{
/**
* GET for ChildThumbnail api
* #param string $product_id
* #return string
*/
public function getChildThumbnail($product_id);
}
Model/ChildThumbnailManagement.php
<?php
namespace WebAPI\GetParentProductThumbnail\Model;
class ChildThumbnailManagement
{
/**
* {#inheritdoc}
*/
public function getChildThumbnail($product_id)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
if($product_id != ""){
//This method getParentIdsByChild($child_id) get the parent id of a configurable product.
$parent_product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product_id);
if(isset($parent_product[0]))
{
$parent_id = $parent_product[0];
//Parent object where you can get Thumbnail, name.... etc
//$parent_object = $objectManager->create('Magento\Catalog\Model\Product')->load($parent_product[0]);
echo parent_id;
}
}
return null;
}
}

Magento2 Soap service not working

I tried to create a soap service but somehow its wsdl url is not working. Below is the code:-
Api/CustomapitInterface.php
namespace W3solver\Customapi\Api;
interface CustomapiInterface
{
/**
* Returns greeting message to user
*
* #api
* #param string $name Users name.
* #return string Greeting message with users name.
*/
public function name($name);
}
model/Customapi.php
<?php
namespace W3solver\Customapi\Model;
use W3solver\Customapi\Api\CustomapiInterface;
class Customapi implements CustomapiInterface
{
/**
* Returns greeting message to user
*
* #api
* #param string $name Users name.
* #return string Greeting message with users name.
*/
public function name($name) {
return "Hello, " . $name;
}
}
etc/di.xml
<?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="W3solver\Customapi\Api\CustomapiInterface" type="W3solver\Customapi\Model\Customapi" />
</config>
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="W3solver_Customapi" setup_version="1.0.0" />
</config>
etc/webapi.xml
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/customapi/name/:name" method="GET">
<service class="W3solver\Customapi\Api\CustomapiInterface" method="name"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
I have created this by using a refrence from http://inchoo.net/magento/api-magento/magento-2-custom-api/. I did not where this get wrong.
Below is the url i am trying to use:-
http://magento2.local/index.php/soap/default?wsdl&services=w3solverCustomapiV1
Magento 2 has API access to anonymous APIs disabled by default, you will need to enable this from the backend administration panel.
To disable this feature, log in to the Admin panel and select
Stores > Configuration > Services > Magento Web API > Web API Security.
Then select Yes from the Allow Anonymous Guest Access menu.
You can find more information on the dev guidelines here.

Magento: error for custom module (Class not found in Layout.php)

I tried to create a new custom module (block) in Magento which will show other products from manufacturer on product detail page. When I load product detail page I get:
Fatal error: Class 'AimIT_ManufacturerBlock_Block_Manufacturerblock' not found in ..\app\code\core\Mage\Core\Model\Layout.php on line 491
I have created:
1)\app\etc\modules\AimIT_ManufacturerBlock.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<AimIT_ManufacturerBlock>
<!-- Whether our module is active: true or false -->
<active>true</active>
<!-- Which code pool to use: core, community or local -->
<codePool>local</codePool>
</AimIT_ManufacturerBlock>
</modules>
</config>
2) \app\code\local\AimIT\ManufacturerBlock\etc\config.xml
<?xml version="1.0"?>
<config>
<global>
<blocks>
<aimitmanufacturerblock>
<class>AimIT_ManufacturerBlock_Block</class>
</aimitmanufacturerblock>
</blocks>
</global>
</config>
3) \app\code\local\AimIT\ManufacturerBlock\Block\Manufacturerblock.php
<?php
class AimIT_ManufacturerBlock_Block_Manufacturerblock extends Mage_Core_Block_Template
{
public function getManufacturerProducts($manufacturer)
{
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('manufacturer',$manufacturer);
$collection->addAttributeToSelect('manufacturer');
return $collection;
}
}
?>
4)\app\design\frontend\default\respond\template\aimit\manufacturerblock\manufacturerblock.phtml
<?php $_products = $this->getManufacturerProducts('cukrarna-u-vanku') ?>
<?php print_r($_products); ?>
5) in catalog\product\view.phtml I have placed this code:
<?php echo $this->getLayout()->createBlock('aimitmanufacturerblock/manufacturerblock')->setTemplate('aimitmanufacturerblock/manufacturerblock.phtml')->toHtml(); ?>
What did I omit while creating the module?
When translating 'aimitmanufacturerblock/manufacturerblock' into a class name Magento generates AimIT_ManufacturerBlock_Block_Manufacturerblock and can't find a class under such name because your block's class name is actually 'AimIT_ManufacturerBlock_Block_ManufacturerBlock' - which is wrongly cased.
Rename your class into
class AimIT_ManufacturerBlock_Block_Manufacturerblock extends Mage_Core_Block_Template
{
Rename your class file ManufacturerBlock.php into Manufacturerblock.php