Question:

Put qty back to stock on creditmemo magneto 2 programmatically

Jaxson: 2 weeks ago

I am creating the credit memo from the front-end for the order. Saleable qty are returning back to stock, But Qty Per source is not returning back to stock. I have added this code to my controller. Please help. Thanks in advance.

use Magento\Sales\Model\RefundOrder;
use Magento\Sales\Model\Order\Creditmemo\ItemCreationFactory;
use Magento\Sales\Api\OrderRepositoryInterface;

class Cancel extends Action implements OrderInterface
{

    public function __construct(
        Context $context,
        RefundOrder $refundOrder,
        ItemCreationFactory $itemCreationFactory,
        OrderRepositoryInterface $orderRepository
    )
    {
        $this->refundOrder = $refundOrder;
        $this->itemCreationFactory = $itemCreationFactory;
        $this->orderRepository = $orderRepository;
        parent::__construct($context);
    }

    public function execute()
    {
        $orderId = $this->getRequest()->getParam('order_id');
        $order = $this->orderRepository->get($orderId);
        $creditMemo = $this->creditmemoFactory->createByOrder($order);

        $creditMemoItems = $creditMemo->getAllItems();

        foreach ($creditMemoItems as $item) {
            $creditmemoItem = $this->itemCreationFactory->create();
            $creditmemoItem->setQty($item->getQty())
                    ->setOrderItemId($item->getOrderItemId());
            $itemIdsToRefund[] = $creditmemoItem;
        }

        $this->refundOrder->execute(
                $order->getId(),
                $itemIdsToRefund
        );
    }

}

Answer:
Carson: 2 weeks ago

Change your code as below and import new class:

use Magento\Sales\Model\RefundOrder;
use Magento\Sales\Model\Order\Creditmemo\ItemCreationFactory;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\SalesInventory\Model\Order\ReturnProcessor;
use Magento\Sales\Model\Service\CreditmemoServiceFactory;

class Cancel extends Action implements OrderInterface
{

    public function __construct(
        Context $context,
        RefundOrder $refundOrder,
        ItemCreationFactory $itemCreationFactory,
        OrderRepositoryInterface $orderRepository,
        ReturnProcessor $returnProcessor,
        CreditmemoServiceFactory $creditmemoServiceFactory
    )
    {
        $this->refundOrder = $refundOrder;
        $this->itemCreationFactory = $itemCreationFactory;
        $this->orderRepository = $orderRepository;
        $this->_creditmemoServiceFactory = $creditmemoServiceFactory;
        $this->_returnProcessor = $returnProcessor;
        parent::__construct($context);
    }

    public function execute()
    {
        $orderId = $this->getRequest()->getParam('order_id');
        $order = $this->orderRepository->get($orderId);
        $creditMemo = $this->creditmemoFactory->createByOrder($order);
        $this->_returnProcessor->execute($creditmemo, $order, [''], true);

        $this->_creditmemoServiceFactory->create()->refund($creditmemo);        }

}

After you can change the order status as you want. Try this code.