How to display stock quantity only for simple products?
-
06-02-2021 - |
Question
I want to display stock quantity only for simple products. For that, I added some code and it's working fine for simple products. But the thing is it's also showing quantity for other types of products like configurable.
here is the code I added app/code/Smartwave/Porto/Helper/Data.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Smartwave\Porto\Helper;
use Magento\Framework\Registry;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_objectManager;
private $_registry;
protected $_filterProvider;
private $_checkedPurchaseCode;
private $_messageManager;
protected $_configFactory;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
Registry $registry
) {
$this->stockRegistry = $stockRegistry;
parent::__construct($context);
}
public function getStockQty($product)
{
return $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId())->getQty();
}
}
app/design/frontend/Smartwave/porto/Magento_Catalog/templates/product/view/type/default.phtml
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
?>
<?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" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span class="label"><?php /* @escapeNotVerified */ echo __('Availability:') ?></span>
<span><?php echo $this->helper('Smartwave\Porto\Helper\Data')->getStockQty($_product);?></span>
</div>
<?php else: ?>
<div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span class="label"><?php /* @escapeNotVerified */ echo __('Availability:') ?></span>
<span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
</div>
<?php endif; ?>
<?php endif; ?>
Solution
Replace this below code in your if else condition :
<?php if ($_product->isAvailable()): ?>
<?php if($_product->getTypeId() == 'simple') : ?>
<div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span class="label"><?php /* @escapeNotVerified */ echo __('Availability:') ?></span>
<span><?php echo $this->helper('Smartwave\Porto\Helper\Data')->getStockQty($_product);?></span>
</div>
<?php endif; ?>
<?php else: ?>
<div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
<span class="label"><?php /* @escapeNotVerified */ echo __('Availability:') ?></span>
<span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
</div>
<?php endif; ?>
OTHER TIPS
You can try below condition to display stock quantity if its simple product only
if($_product->getTypeId() == 'simple'){
//your code
}
It will return configurable or simple
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange