Question

I want to get RMA returns details by current registry or by order id.

How can i load RMA by registry or order id?

Any help or hint will be appreciated.

Was it helpful?

Solution

This is a demo class code about rma, I hope to help you.

<?php

namespace YourVendorName\YourModuleName\Demo;

class RmaDemo
{
    protected $orderFactory;

    protected $rmaFactory;

    protected $rmaItemFactory;

    public function __construct(
        \Magento\Sales\Model\OrderFactory $orderFactory,
        \Magento\Rma\Model\ResourceModel\Rma\CollectionFactory $rmaFactory,
        \Magento\Rma\Model\ResourceModel\Item\CollectionFactory $rmaItemFactory
    )
    {
        $this->orderFactory = $orderFactory;
        $this->rmaFactory = $rmaFactory;
        $this->rmaItemFactory = $rmaItemFactory;
    }

    // eg: $orderId = xxxxx
    public function execute($orderId)
    {
        // 1.get rma collection by orderId
        // return array and rma_entity_id $RmaData['entity_id']
        $RmaData = $this->getRmaCollection($orderId);

        // 2.get rma item collection by orderId
        $order = $this->orderFactory->create()->load($orderId);
        foreach ($order->getItems() as $item) {
            $RmaItemData = $this->getRmaItemCollection($item->getItemId());
        }
    }

    /**
     * @param $orderId
     * @return array
     */
    public function getRmaCollection($orderId)
    {
        return $this->rmaFactory->create()->addFieldToSelect(
            '*'
        )->addFieldToFilter(
            'order_id',
            $orderId
        )->getFirstItem();
    }

    /**
     * @param $orderItemId
     * @return array
     */
    public function getRmaItemCollection($orderItemId)
    {
        return $this->rmaItemFactory->create()->addFieldToSelect(
            '*'
        )->addFieldToFilter(
            'order_item_id',
            $orderItemId
        )->getFirstItem();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top