I want to display stock only to logged in customers. can you guide me how can I do? 
Asked
Active
Viewed 426 times
3 Answers
1
Something like:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
$_product->getExtensionAttributes()->getStockItem()->getQty();
}
BartZalas
- 1,757
- 12
- 23
-
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 https://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-directly – 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 04: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();
}
Mukesh Prajapati
- 1,881
- 13
- 22
-
-
-
@kiran like this
like this $stockQty = $product->getExtensionAttributes()->getStockItem()->getQty();
– Mukesh Prajapati Sep 24 '18 at 05:31echo $stockQty; -
-
-
-
I attached one more screenshot, can we display stock each product only for logged customer. – kiran Sep 24 '18 at 11:18
-
package/theme/Magento_Catalog/view/frontend/templates/product/list.phtml
after this code
$block->getProductPrice($_product) ?>
– Mukesh Prajapati Sep 24 '18 at 11:21 -
app\design\frontend\Solwin\freego\Magento_Catalog\templates\product\list.phtml path is correct – kiran Sep 24 '18 at 11:26
-
-
-
1Don't use object Manager directly, it is not recommended Magento practice. – Anshu Mishra Sep 25 '18 at 04:53
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.
Anshu Mishra
- 8,950
- 7
- 40
- 88
-
-
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 04:52
-
-
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 05:29
