0

Hope there is one that can help me out with this problem. I made it years back for M1 but can remember how and now I need it for my M2 website.

On the productpage I want to show backorder insteed of add to cart for all products with the quantity 0 or below. All products with quantity of 1 or more it must show add to cart.

I have allow backorder and notify customers. But to make it more clear I want the cart bottum to display backorder (Bestillingsvare) for the products that are not in stock.

It M2 2.4.3-p1

1
  • Anyone who can help with this? Commented Jun 16, 2022 at 8:25

1 Answer 1

0

Create a view model with the below code and use it in the phtml. You can get the product's SKU from the registry.

<?php


namespace Vendor\Extension\ViewModel;

use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySalesApi\Api\Data\SalesChannelInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;

class BackorderHelper implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
    /**
     * @var StoreManagerInterface
     */
    private StoreManagerInterface $storeManager;
    /**
     * @var StockResolverInterface
     */
    private StockResolverInterface $stockResolver;
    /**
     * @var GetStockItemConfigurationInterface
     */
    private GetStockItemConfigurationInterface $getStockItemConfiguration;
    /**
     * @var GetProductSalableQtyInterface
     */
    private GetProductSalableQtyInterface $getProductSalableQty;

    public function __construct(
        StoreManagerInterface $storeManager,
        StockResolverInterface $stockResolver,
        GetStockItemConfigurationInterface $getStockItemConfiguration,
        GetProductSalableQtyInterface $getProductSalableQty
    ) {
        $this->storeManager = $storeManager;
        $this->stockResolver = $stockResolver;
        $this->getStockItemConfiguration = $getStockItemConfiguration;
        $this->getProductSalableQty = $getProductSalableQty;
    }
    /**
     * @param string $sku
     * @return bool
     */
    public function isBackOrder(string $sku)
    {
        try {
            $websiteCode = $this->storeManager->getWebsite()->getCode();
            $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode);
            $stockId = $stock->getStockId();

            $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
            $salableQty = $this->getProductSalableQty->execute($sku, $stockId);
            $threshold = $stockItemConfiguration->getMinQty();
            if ($threshold < 0) {
                $salableQty += $stockItemConfiguration->getMinQty();
            }

            if ($stockItemConfiguration->isManageStock() && $stockItemConfiguration->getBackorders() && !($salableQty > 1)) {
                return true;
            }
        } catch (\Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException $e) {
            return false;
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            return false;
        }
        return false;
    }
}

Your Answer

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

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