1

I want to display stock only to logged customers in product page. can you guide me how can I do and in which file?

gill
  • 329
  • 1
  • 4
  • 19

2 Answers2

0

First of all you have to add condition to check if customer is logged in or not. There are multiple ways either do with object manager in template or create a module / block which will return if customer is available or not . you can check this post How to check if customer is logged in or not in magento 2? to know how to implement that check.

Method 1 :

now create a template file customer-qty.phtml in /app/design/frontend/package-name/theme/Magento_Catalog/templates/product/view/customer-qty.phtml

and add code blow :

/**
* Product view template
*
* @see \Magento\Catalog\Block\Product\View\Customer-qty
*/
?>
<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
?>   

<div id="get-customer-qty">        
    <?php echo $product->getQty(); ?>
</div>
<?php } ?>

How to check if customer is logged in or not in magento 2?

Andd then add this template block in your catalog_product_view.xml layout file :

<block class="Magento\Catalog\Block\Product\View" name="customer.qty" as="customerQty" template="Magento_Catalog::product/view/customer-qty.phtml" group="detailed_info"/>

Method 2 is :

copy /vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml

and paste in you theme folder ( like that ): /frontend/package-name/theme/Magento_Catalog/templates/product/view/type/default.phtml

And update file with below code :

    <?php
    /**
    * Copyright © Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */

    // @codingStandardsIgnoreFile

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSession = $objectManager->get('Magento\Customer\Model\Session');
   if($customerSession->isLoggedIn()) {

   ?>
   <?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView  */?>
   <?php $_product = $block->getProduct() ?>



    <?php if ($block->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable()): ?>
    <div class="stock available" style="display: none" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
        <span><?= /* @escapeNotVerified */ __('In stock') ?></span>
    </div>
    <?php else: ?>
    <div class="stock unavailable" style="width: 100%; margin-bottom: 10px;" title="<?= /* @escapeNotVerified */ __('Availability') ?>">
        <span style="background: red; color: #fff; padding: 2px 10px; font-weight: bold; line-height: 24px; padding : 5px 15px"><?= /* @escapeNotVerified */ __('Sold Out') ?></span>
    </div>
    <?php endif; ?>
    <div><?php echo $_product->getQty(); ?></div>
<?php endif; ?>
<?php }?>

NOTE : for configurable products same you have to do with Configurable product template.

Again, i have used object manager for quick implementation and understanding however you can create a module and block for this which is more systematic way to do that.

Naveed Asim
  • 3,637
  • 2
  • 11
  • 22
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