0

I am using Magento 2.4.2 version. I like to know if there is any possibility to override the stock status on the product detail page without overriding the template. Please help and Thanks in advance. enter image description here

2
  • Hello. What are you trying to achieve?
    – Joao71
    Sep 15 at 9:17
  • Do you only want to change stock status from Instock to Outofstock? OR if you want to change the verbiage then you have to make change in template file OR another work around is you can use translation but that will not be conditional. Let me know if you need any other help. Sep 15 at 14:36
0

You can achieve this by doing the following:

On your etc\di.xml

<type name="Magento\Catalog\Model\Product">
        <plugin name="hidecartbutton" type="vendor\module\Plugin\HideAddButtonOutOfStock" sortOrder="1"/>
    </type>

Create a Plugins folder an add the file HideAddButtonOutOfStock.php

<?php

namespace vendor\module\Plugin;
use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Session;

class HideAddButtonOutOfStock
{

    protected $_session;
    protected $productRepository;
    public function __construct(
        Session                                     $session,
        \Magento\Catalog\Model\ProductRepository    $productRepository     
    ) 
    {
        $this->_session           = $session;
        $this->productRepository  = $productRepository;
    }
    
   public function afterIsAvailable( Product $product)
   {
            $hasStock = //place your condition here

       
            if ($hasStock)
            {
                return true;
            }
            else
            {
                return false;
            }
       
    }
}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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