0

I create creditmemo, but when I want to return qty to stock,I don't know how to used; vendor/magento/module-sales/Model/RefundOrder.php vendor/magento/module-sales/Model/RefundInvoice.php

2 Answers 2

0

To return the refunded quantity to the stock, you can use the following code in your custom module or script

use Magento\Sales\Api\CreditmemoRepositoryInterface;
use Magento\CatalogInventory\Api\StockManagementInterface;

class StockRefund
{
    protected $creditmemoRepository;
    protected $stockManagement;

    public function __construct(
        CreditmemoRepositoryInterface $creditmemoRepository,
        StockManagementInterface $stockManagement
    ) {
        $this->creditmemoRepository = $creditmemoRepository;
        $this->stockManagement = $stockManagement;
    }

    public function returnToStock($creditmemoId)
    {
        $creditmemo = $this->creditmemoRepository->get($creditmemoId);
        $items = $creditmemo->getAllItems();
        $itemsToRefund = [];

        foreach ($items as $item) {
            if (!$item->getBackToStock()) {
                continue;
            }
            $itemsToRefund[$item->getProductId()] = $item->getQty();
        }

        if (!empty($itemsToRefund)) {
            $this->stockManagement->backItemQty($itemsToRefund);
        }
    }
}

In this code, we are injecting CreditmemoRepositoryInterface to load the credit memo and StockManagementInterface to manage the stock.

We get all the items from the credit memo and check if the "Back to Stock" option is selected for each item. If yes, we add the product ID and the quantity to an array $itemsToRefund.

Finally, we pass the $itemsToRefund array to the backItemQty method of the StockManagementInterface to return the refunded quantity to the stock.

You can call this function by passing the credit memo ID to the returnToStock method.

2
  • Hi,I can't find where used $item->getBackToStock(). I can create creditmemo, but I don't add creditmemo_created to inventory_reservation table.
    – Allen
    Commented Mar 21, 2023 at 9:27
  • You have to create it
    – Harun
    Commented Mar 21, 2023 at 9:38
0

To return the refunded quantity to stock after creating a credit memo in Magento 2, you can use the refund() method of the Magento\Sales\Model\Order\Creditmemo class. This method will call the Magento\Sales\Model\Order\Creditmemo\Item::backToStock() method which will add the refunded quantity back to the stock.

Here's an example of how to use it:

    use Magento\Sales\Model\Order\CreditmemoFactory;
    use Magento\Sales\Api\CreditmemoRepositoryInterface;

    class YourClass
    {
        protected $creditmemoFactory;
        protected $creditmemoRepository;

        public function __construct(
            CreditmemoFactory $creditmemoFactory,
            CreditmemoRepositoryInterface $creditmemoRepository
        ) {
            $this->creditmemoFactory = $creditmemoFactory;
            $this->creditmemoRepository = $creditmemoRepository;
        }

        public function refundCreditmemo($creditmemoId)
        {
            $creditmemo = $this->creditmemoFactory->create()->load($creditmemoId);
            $creditmemo->refund();
            $this->creditmemoRepository->save($creditmemo);
        }
    }

In this example, $creditmemoId is the ID of the credit memo that you want to refund. You can get this ID from the sales_creditmemo table in your database.

Once you have loaded the credit memo object, you can call the refund() method to return the refunded quantity to the stock. Finally, you need to save the credit memo object using the CreditmemoRepositoryInterface to persist the changes to the database.

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.