I want to display stock only to logged in customers. can you guide me how can I do? enter image description here

enter image description here


Try using below code, it will hide stock sku information from the product details page for not logged in user.


<?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">
            <module name="Magento_Catalog" />
            <module name="Magento_Customer" />



use Magento\Framework\Component\ComponentRegistrar;



<?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" />



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())


<?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">
        <referenceBlock name="product.info.stock.sku" remove="true" />

This will give you some idea and you can modify it according to your requirement.

  • This code is not working – kiran Sep 24 '18 at 16:51
  • 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. – Anshu Mishra Sep 25 '18 at 4:52
  • code is working now. i want to disable only stock then how can I Do? – kiran Sep 26 '18 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. – Anshu Mishra Sep 27 '18 at 5:29

Something like:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    $customerSession = $objectManager->get('Magento\Customer\Model\Session');

    if($customerSession->isLoggedIn()) {


  • 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? – kiran Sep 24 '18 at 11:21
  • Please, use Mukesh answer , here why magento.stackexchange.com/questions/117098/… – BartZalas Sep 24 '18 at 11:35
  • Don't use object Manager directly, it is not recommended Magento practice. – Anshu Mishra Sep 25 '18 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()) {
  • Can you explain me please where I can use this code? – kiran Sep 23 '18 at 1:46
  • @kiran in your phtml file – Mukesh Prajapati Sep 24 '18 at 5:22
  • @kiran like this like this $stockQty = $product->getExtensionAttributes()->getStockItem()->getQty(); echo $stockQty; – Mukesh Prajapati Sep 24 '18 at 5:31
  • In which file, I use this code? – kiran Sep 24 '18 at 11:03
  • 1
    Don't use object Manager directly, it is not recommended Magento practice. – Anshu Mishra Sep 25 '18 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.