0

I am trying to override Model file in my custom module like below.

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

<preference for="Magento\InventorySales\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition" type="Vendor\Module\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition" />

</config>

Then Vendor\Module\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition

<?php 

  declare(strict_types=1);

 namespace Vendor\Module\Model\IsProductSalableCondition;

 use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalabilityErrorInterfaceFactory;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterface;
use Magento\InventorySalesApi\Api\Data\ProductSalableResultInterfaceFactory;
use Magento\InventorySalesApi\Api\IsProductSalableForRequestedQtyInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;

/**
 * @inheritdoc
 */

class BackOrderNotifyCustomerCondition implements 
 IsProductSalableForRequestedQtyInterface
{
/**
 * @var GetStockItemConfigurationInterface
 */
private $getStockItemConfiguration;

/**
 * @var GetStockItemDataInterface
 */
private $getStockItemData;

/**
 * @var ProductSalableResultInterfaceFactory
 */
private $productSalableResultFactory;

/**
 * @var ProductSalabilityErrorInterfaceFactory
 */
private $productSalabilityErrorFactory;

/**
 * @param GetStockItemConfigurationInterface $getStockItemConfiguration
 * @param GetStockItemDataInterface $getStockItemData
 * @param ProductSalableResultInterfaceFactory $productSalableResultFactory
 * @param ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory
 */
public function __construct(
    GetStockItemConfigurationInterface $getStockItemConfiguration,
    GetStockItemDataInterface $getStockItemData,
    ProductSalableResultInterfaceFactory $productSalableResultFactory,
    ProductSalabilityErrorInterfaceFactory $productSalabilityErrorFactory
) {
    $this->getStockItemConfiguration = $getStockItemConfiguration;
    $this->getStockItemData = $getStockItemData;
    $this->productSalableResultFactory = $productSalableResultFactory;
    $this->productSalabilityErrorFactory = $productSalabilityErrorFactory;
}

/**
 * @inheritdoc
 */
public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
{
    $stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);

    if ($stockItemConfiguration->getBackorders() === StockItemConfigurationInterface::BACKORDERS_YES_NOTIFY) {
        $stockItemData = $this->getStockItemData->execute($sku, $stockId);
        if (null === $stockItemData) {
            return $this->productSalableResultFactory->create(['errors' => []]);
        }

        $backOrderQty = $requestedQty - $stockItemData[GetStockItemDataInterface::QUANTITY];
        if ($backOrderQty > 0) {
            $errors = [
                $this->productSalabilityErrorFactory->create([
                        'code' => 'back_order-not-enough',
                        'message' => __(
                            'We don\'t have as many quantity as you requested, '
                            . 'but we\'ll back order the remaining %1.',
                            $backOrderQty * 1
                        )])
            ];
            return $this->productSalableResultFactory->create(['errors' => $errors]);
        }
    }

    return $this->productSalableResultFactory->create(['errors' => []]);
  }
}

Which is not overriding to my custom module, can anyone tell where exactly the issue is? Suggest me if any other possibilities using plugin or something.

Thanks

  • Which function do you want to override in your custom module? – Sumit Sep 24 at 7:41
  • i want to override checkQuoteItemQty function. – jafar pinjar Sep 24 at 7:43
  • @Sumit, updated my question, was trying different class, pls check now – jafar pinjar Sep 24 at 7:52
  • Magento Version?? – Rk Rathod Sep 24 at 7:58
  • 2.3.2 is magento version – jafar pinjar Sep 24 at 8:08
0

Based on the code you provided you have two namespace declared.

enter image description here

Remove one of them and test

  • no its a mistake while raising question, only once it is there in my file – jafar pinjar Sep 24 at 8:52
0

What i have observed You can not override this file because this functions contains private variables and you can not override private variables and methods.

So basically now you have a option to override with plugin. Click here for how to override method through plugin in Magento2.

Please use after plugin. so that you can override this method.

Code Spinnet:

Path: app/code/Vendor/Module/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Product">
        <plugin name="Magento\InventorySales\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition" type="Vendor\Module\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition" sortOrder="1" />
    </type>    
</config>

Path: Vendor\Module\Model\IsProductSalableCondition\BackOrderNotifyCustomerCondition.php

<?php 

namespace Vendor\Module\Model\IsProductSalableCondition;

public function afterBackOrderNotifyCustomerCondition(\Magento\Catalog\Model\Product $subject, $result){

    //return $result;   #This Variable will return at end.
}
  • 1
    hi, if you can pls update me the plugin code for my file which i'm trying to override. Thanks – jafar pinjar Sep 24 at 8:53
  • Updated code, Please check!! – Oscprofessionals Sep 24 at 9:03
  • is it working? not in my case – jafar pinjar Sep 24 at 10:09
  • This is an Sample, now you need to manuplate your values or put your desired code. between afterBackOrderNotifyCustomerCondition Method – Oscprofessionals Sep 24 at 10:13
  • You need to Change Module Name, Vendor Name, Also Put Logic in Method. – Oscprofessionals Sep 24 at 10:26

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.