3

I'm trying to get the shopping cart ID given the customer ID, to expose it by SOAP API.

Calling the web service with an ID 1 returns nothing.

What am I missing here?

Here's the code for the web service

public function getShoppingCartId($customerId)
{
    Mage::log(__METHOD__);

    $customer = Mage::getModel('customer/customer');
    $customer = $customer->load($customerId);

    Mage::log('Getting shopping cart of '. $customer->getName() .' ('. $customer->getEmail() .'), ID: ' . $customer->getId() );

    $quote = Mage::getModel('sales/quote')->loadByCustomer($customer);

    Mage::log("-------------------------------------------");
    return $quote->getId();
}

This is the log output

DEBUG (7): Phoenix_Moneybookers_Model_Core_Api::getShoppingCartId
DEBUG (7): Getting shopping cart of John Doe ([email protected]), ID: 1
DEBUG (7): -------------------------------------------

2 Answers 2

6

What you can do is not use the loadByCustomer function that is found in the class Mage_Sales_Model_Quote but simply build your own collection with the correct filters.

/** @var Mage_Sales_Model_Resource_Quote_Collection $quoteCollection */
$quoteCollection = Mage::getModel('sales/quote')->getCollection();
$quoteCollection->addFieldToFilter('customer_id', 4);

This will get you all quotes ever for this customer. You can then filter by updated_at date to get the the most recent first and then select only one.

$quoteCollection->addOrder('updated_at');
$quote = $quoteCollection->getFirstItem();

This will give you the most recent quote object for this customer. This way you do not need to rewrite anything.

1
  • @David Manners can i add also $quoteCollection->addFieldToFilter('store_id', 1); Nov 14, 2016 at 7:46
1

Your Quote is currently at inactive stage.As per magento when we try to load a quote using customer(i.e loadByCustomer()) it only given the one active quote object.

See: at Class Mage_Sales_Model_Quote

 public function loadByCustomer($customer)
    {
        if ($customer instanceof Mage_Customer_Model_Customer) {
            $customerId = $customer->getId();
        }
        else {
            $customerId = (int) $customer;
        }
        $this->_getResource()->loadByCustomerId($this, $customerId);
        $this->_afterLoad();
        return $this;
    }

and Class Mage_Sales_Model_Resource_Quote

 public function loadByCustomerId($quote, $customerId)
    {
        $adapter = $this->_getReadAdapter();
        $select  = $this->_getLoadSelect('customer_id', $customerId, $quote)
            ->where('is_active = ?', 1)
            ->order('updated_at ' . Varien_Db_Select::SQL_DESC)
            ->limit(1);

        $data    = $adapter->fetchRow($select);

        if ($data) {
            $quote->setData($data);
        }

        $this->_afterLoad($quote);

        return $this;
    }

Solution:

rewrite class Mage_Sales_Model_Resource_Quote and remove is_active clause function loadByCustomerId.

 public function loadByCustomerId($quote, $customerId)
    {
        $adapter = $this->_getReadAdapter();
        $select  = $this->_getLoadSelect('customer_id', $customerId, $quote)
          //  ->where('is_active = ?', 1)
            ->order('updated_at ' . Varien_Db_Select::SQL_DESC)
            ->limit(1);

        $data    = $adapter->fetchRow($select);

        if ($data) {
            $quote->setData($data);
        }

        $this->_afterLoad($quote);

        return $this;
    }

Note: It is not a good idea.

1
  • Thanks for your input. I agree with you that is not a good idea to mess with magento's core code.
    – Ramses
    May 15, 2015 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.