3

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

0

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.

  • 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
1

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? – 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
0

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();
}
  • 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.