Skip to content

Replace error message using a custom plugin

I’d like to replace the error message in this magento core function.

vendor/magento/module-inventory-sales/Model/IsProductSalableCondition/BackOrderNotifyCustomerCondition.php

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

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

            $salableQty = $this->getProductSalableQty->execute($sku, $stockId);
            $backOrderQty = $requestedQty - $salableQty;
            $displayQty = $this->getDisplayQty($backOrderQty, $salableQty, $requestedQty);

            if ($displayQty > 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.',
                                $displayQty * 1
                            )])*/
                            'message' => __(
                                'We don't have as many quantity as you requested, '
                                . 'but we'll back order the remaining %1.',
                                $displayQty * 1
                            )])
                ];
                return $this->productSalableResultFactory->create(['errors' => $errors]);
            }
        }

So I created a custom plugin, but I don’t understand how around() method would replace error message, since the original method will be executed in $proceed(). Can you help me?

namespace MymoduleBackorderCustomPluginMagentoInventorySalesModelIsProductSalableCondition;

class BackOrderNotifyCustomerCondition
{

    public function aroundExecute(
        MagentoInventorySalesModelIsProductSalableConditionBackOrderNotifyCustomerCondition $subject,
        Closure $proceed
    ) {
        // plugin code
        $result = $proceed();
        return $result;
    }
}