Sergei Gorjunov
Sergei Gorjunov

Reputation: 1799

Get quote by reserved order id

I want to create a new payment method. On checkout process payment method redirects to 3-rd party service. I pass reservedOrderId to 3-rd party service and it returns this id back on payment success. How can I get quote by reservedOrderId? Or need I convert quote to order before redirecting to 3-rd party service?

Upvotes: 4

Views: 6480

Answers (4)

edi9999
edi9999

Reputation: 20574

This works fine:

Note that $orderId is order increment_id column value, not id for the row in the database

Mage::getSingleton('sales/quote')
     ->load($orderId, 'reserved_order_id')

Thanks to @Sergei

Upvotes: 2

Tschallacka
Tschallacka

Reputation: 28742

In Magento2 you can load a quote by reserved order id in the following way:

namespace Tschallacka\TestPlugin\Quote;

use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;

class Test 
{
    const RESERVED_ORDER_FIELD = 'reserved_order_id';

    protected $quoteFactory;
    protected $quoteResource;

    public function __construct(
        QuoteFactory $quoteFactory, 
        QuoteResource $quoteResource
    ) {
        $this->quoteFactory = $quoteFactory;
        $this->quoteResource = $quoteResource;
    }
 
    /** @return \Magento\Quote\Model\Quote **/
    public function loadQuote() 
    {
       $quote = $this->quoteFactory->create();
       $this->quoteResource->load($quote, 'test_quote', self::RESERVED_ORDER_FIELD);
       return $quote;
    }
}

Upvotes: 1

Marius
Marius

Reputation: 15216

Since the reserved order should be unique, you can try this:

$quote = Mage::getModel('sales/quote')->getCollection()
    ->addFieldToFilter('reserved_order_id', 'Your Order id')
    ->getFirstItem();

if ($quote->getId()) {
   //quote was found - $quote
}
else {
   //the quote does not exist.
}

Upvotes: 3

Christophe Ferreboeuf
Christophe Ferreboeuf

Reputation: 1058

You'd better convert quote to order before you send to third part. it will permit you to have the correct status for the order (cancelled, paid, fraud....).

If you really need to create the order after, you can override mage_sales_model_quote and mage_sales_model_resource_quote to create the according function loadByReservedOrderId() the resource one with the call to database and the model to permit you to call it on a controller.

for resource, something like that would do.

public function loadByReservedOrderId($id){
    $select = $this->_getReadAdapter()->select()->from(array('main_table'=>$this->getMainTable()), 'entity_id')
        ->where('main_table.reserved_order_id = ?', array($id));

    return $this->_getReadAdapter()->fetchOne($select);
}

for the model :

public function loadByReservedOrderId($id)
{
    if ($id == null){
        return  false;
    }
    $order_id = $this->_getResource()->loadByReservedOrderId($id);
    $this->load($order_id);
    return $this;
}

Upvotes: 0

Related Questions