Magento – Magento Edit Product from Cart and get selected options

custom-optionsmultiselect-attributetemplatetheme

Problem: in the cart is a product set together from selected options.
Now I want to edit one of those products.

Question: how do I get the same option values to the configure page? The page that has this path is checkout/cart/configure/id/1/

Where does Magento has to take the needed informations from?

PS. I use a Custom Theme!

Best Answer

The information is pulled from the quote object that is persisted in the database. The integer on the end of the checkout/cart/configure/id/123 is a reference to the quote item ID from the sales_flat_quote_item table.

In short, when you hit the configure page, it fetches the item and looks up the configuration option selections in order to pre-populate the form with them.

You could access this information via...

/* @var $cart Mage_Checkout_Model_Cart */
$cart = Mage::helper( 'checkout/cart' )->getCart();

/* @var $item Mage_Sales_Model_Quote_Item */
$item = $cart->getQuote()->getItemById( $id );

There's a method available to Mage_Sales_Model_Quote_Item called getBuyRequest that returns the configured options.

/**
 * Returns formatted buy request - object, holding request received from
 * product view page with keys and options for configured product
 *
 * @return Varien_Object
 */
public function getBuyRequest()
{
    $option = $this->getOptionByCode('info_buyRequest');
    $buyRequest = new Varien_Object($option ? unserialize($option->getValue()) : null);

    // Overwrite standard buy request qty, because item qty could have changed since adding to quote
    $buyRequest->setOriginalQty($buyRequest->getQty())
        ->setQty($this->getQty() * 1);

    return $buyRequest;
}

This is a good starting spot.