in catalog_product_view.xml
of my theme I have a block like this
`<block class="Magento\Catalog\Block\Product\View" name="extra.product.info" template="product/view/extra-product-info.phtml" after="product.price.final"/>`
Then in "extra-product-info.phtml" file I see these lines of code:
[...]
$_product = $block->getProduct();
if ($block->displayProductStockStatus()){
[...]
displayProductStockStatus() method is used by Magento to decide whether to display or not, the product's stock status in the product detail page; The stock status visibility can be set from the Admin -> Configuration -> Catalog -> Inventory -> Stock Options -> "Display products availability in stock on Storefront" backend config option.
Now, how can I get displayProductStockStatus() method from a custom block?
For example, I created a custom module and I added the block in catalog_product_view.xml
, then I created the block file
app/code/{CustomVendor}/{CustomModule}/Block/MyBlock.php
with this code
`
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;
class MyBlock extends \Magento\Framework\View\Element\Template
{
/**
* @var Registry
*/
protected $registry;
/**
* @var \Magento\Catalog\Model\Product
*/
protected $product;
/**
* ProductView constructor.
* @param Template\Context $context
* @param array $data
* @param Registry $registry
*/
public function __construct(
Template\Context $context,
array $data = [],
Registry $registry
)
{
$this->registry = $registry;
parent::__construct($context, $data);
}
/**
* @return \Magento\Catalog\Model\Product
*/
public function getProduct()
{
if (is_null($this->product)) {
$this->product = $this->registry->registry('product');
}
return $this->product;
}
}
`
Besides, from my template file /app/code/{CustomVendor}/{CustomModule}/view/frontend/templates/myblock.phtml
I can access the product like this:
`<?php $_product = $block->getProduct(); ?>`
But I can't get $block->displayProductStockStatus()
because the block class extends \Magento\Framework\View\Element\Template
and not \Magento\Catalog\Block\Product\View
any idea how to implement this method in the class?