Whoever looked at the Magento source code or database in the context of orders, probably encountered the parameter $buyRequest
in many methods or the option info_buyRequest
in quote items and order items. Its purpose is not immediately obvious since it contains seemingly redundant information. Also it does not have its own model class, it’s just a Varien_Object
and a serialized array when stored to the database.
Example:
mysql> select code,value from sales_flat_quote_item_option where option_id=2359; +-----------------+------------------------------------------------------------------------------------------------------------+ | code | value | +-----------------+------------------------------------------------------------------------------------------------------------+ | info_buyRequest | a:4:{s:4:"uenc";s:140:"[...],,";s:7:"product";s:4:"5000";s:15:"related_product";s:0:"";s:3:"qty";s:1:"1";} | +-----------------+------------------------------------------------------------------------------------------------------------+
In short: The $buyRequest
object represents the customer request coming from a click on “Add to cart”, all related data can be derived from this object. It’s basically a generator for quote items. The minimal necessary data is thus the product id (product
) and quantity (qty
).
Where is the $buyRequest object needed?
For example it is created when you add a product to the wishlist, so that it can easily be transfered to the cart with the same configuration. When reconfiguring a product, i.e. editing it from the cart, the buyRequest is passed to the product view to preselect all options.
Also for recurring profiles and reordering the buyRequest objects get “reused” to generate a new order.
Use Cases
Some examples, when it is useful to deal with the buyRequest:
- Programmatically create orders. See
Mage_Sales_Model_Quote::addProduct()
- Automatically add complex products to the cart. See
Mage_Checkout_Model_Cart::addProduct().
- Implementation of own product types. The buyRequest gets processed in
Mage_Catalog_Model_Product_Type_Abstract::_prepareProduct()
. - Manipulation of quote or wishlist items. See
Mage_Sales_Model_Quote::updateItem()
andMage_Wishlist_Model_Wishlist::updateItem()
. In my extension SSE_EditCustomOptions you can see how even custom options in orders can be changed retroactively, by manipulating the buyRequest objects. In the extension C4B_Freeproduct the buyRequest object is used to flag automatically added gifts so that they won’t get added to the cart again when reordering. That’s because there is no fieldset conversion from order items to quote Items when you reorder an order. That makes sense, since the price or other options might have changed in the meantime, so the quote shold be generated again based on request and product data. - Flexible prices through configuration on the product page. Custom request parameters can be included in the price calculation. Andreas von Studnitz writes more about that topic in th article Umsetzung von flexiblen Preisen (German).