Skip to content

Correct way to call an object based on quantity, for displaying a product-alert, when backorders are allowed

I am trying to modify the following file in Magento2

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

/**
 * Recurring payment view stock
 *
 * @api
 * @since 100.0.2
 */
class Stock extends MagentoProductAlertBlockProductView
{
    /**
     * Prepare stock info
     *
     * @param string $template
     * @return $this
     */
    public function setTemplate($template)
    {
        if (!$this->_helper->isStockAlertAllowed() || !$this->getProduct() || $this->getProduct()->isAvailable()) {
            $template = '';
        } else {
            $this->setSignupUrl($this->_helper->getSaveUrl('stock'));
        }
        return parent::setTemplate($template);
    }
}

I want to display a “send me a notification mail” link on the productpage, when the quantity of a product is zero or lower.
Because backorders are allowed, products maintain the status of “in stock” even when hitting the zero treshold, or lower.

As I understand right now, a possible solution could be to call for an object in: vendor/magento/module-catalog/Model/Product.php

Could this be a solution? If so, what would the code look like.