I want to display stock only to logged in customers. can you guide me how can I do?
3 Answers
Try using below code, it will hide stock sku information from the product details page for not logged in user.
app/code/Anshu/Custom/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="Anshu_Custom" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog" />
<module name="Magento_Customer" />
</sequence>
</module>
</config>
app/code/Anshu/Custom/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Anshu_Custom',
__DIR__
);
app/code/Anshu/Custom/etc/frontend/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="layout_load_before">
<observer name="add_layout_handles" instance="Anshu\Custom\Observer\AddHandles" />
</event>
</config>
app/code/Anshu/Custom/Observer/AddHandles.php
<?php
namespace Anshu\Custom\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;
class AddHandles implements ObserverInterface
{
protected $_customerSession;
public function __construct(CustomerSession $_customerSession)
{
$this->_customerSession = $_customerSession;
}
public function execute(Observer $observer)
{
$layout = $observer->getEvent()->getLayout();
if (!$this->_customerSession->isLoggedIn())
{
$layout->getUpdate()->addHandle('customer_logged_out');
}
}
}
app/code/Anshu/Custom/view/frontend/layout/customer_logged_out.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.stock.sku" remove="true" />
</body>
</page>
This will give you some idea and you can modify it according to your requirement.
-
-
I have checked in 2.2.6 on the product page and it works for me. Check in your vanilla Magento. May be compatibility issue with the theme. Commented Sep 25, 2018 at 4:52
-
code is working now. i want to disable only stock then how can I Do?– kiranCommented Sep 26, 2018 at 19:33
-
If I am correct then stock and sku are coming from the same template (Magento_Catalog::product/view/attribute.phtml), you need to create a separate template from one of them. Commented Sep 27, 2018 at 5:29
Something like:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
$_product->getExtensionAttributes()->getStockItem()->getQty();
}
-
Hi! Thank you for the response, I am using custom theme. Can you guide me where I can use this code and in which file?– kiranCommented Sep 24, 2018 at 11:21
-
Please, use Mukesh answer , here why magento.stackexchange.com/questions/117098/… Commented Sep 24, 2018 at 11:35
-
Don't use object Manager directly, it is not recommended Magento practice. Commented Sep 25, 2018 at 4:53
Please do not use $objectManager directly. Use below code
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerFactory = $objectManager->get('\Magento\Customer\Model\SessionFactory');
if($customerFactory->create()->isLoggedIn()) {
$product->getExtensionAttributes()->getStockItem()->getQty();
}
-
-
-
@kiran like this like this $stockQty = $product->getExtensionAttributes()->getStockItem()->getQty(); echo $stockQty; Commented Sep 24, 2018 at 5:31
-
-
1Don't use object Manager directly, it is not recommended Magento practice. Commented Sep 25, 2018 at 4:53