0

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?

1 Answer 1

1

Open the following class:

vendor/magento/module-catalog/Block/Product/AbstractProduct.php

public function displayProductStockStatus()
{
    $statusInfo = new \Magento\Framework\DataObject(['display_status' => true]);
    $this->_eventManager->dispatch('catalog_block_product_status_display', ['status' => $statusInfo]);
    return (bool) $statusInfo->getDisplayStatus();
}

Now you can check where Magento set display_status by the event. So open the following class:

vendor/magento/module-catalog-inventory/Observer/DisplayProductStatusInfoObserver.php

public function execute(EventObserver $observer)
{
    $info = $observer->getEvent()->getStatus();
    $info->setDisplayStatus($this->stockConfiguration->isDisplayProductStockStatus());
}

Additionally, you can check how to implement:

vendor/magento/module-catalog-inventory/Model/Configuration.php

public function isDisplayProductStockStatus($store = null)
{
    return $this->scopeConfig->isSetFlag(
        self::XML_PATH_DISPLAY_PRODUCT_STOCK_STATUS,
        ScopeInterface::SCOPE_STORE,
        $store
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.