Question

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

Était-ce utile?

La solution

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.

Autres conseils

Something like:

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

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

    if($customerSession->isLoggedIn()) {

       $_product->getExtensionAttributes()->getStockItem()->getQty();

    }

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();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top