Saturday, August 11, 2012

Add to Cart with custom attributes & values


I want to display the custom price on the Shopping Cart page. By default if you add to cart the product it will take the price & will show that price on the cart page.
The data on the cart page is coming from the table ‘sales_flat_quote_item’. This table is having lot many fields. Some of them are product_id, name, description, qty, price. Those are the fields which basically reflects on the cart page.
My task was to display the custom image, custom price, custom description in the row of a product on the cart page.
So first of all we need to insert those values in the DB, while adding the product to the cart.
The phase of adding the product to cart is bit complicated to understand.
These are list of some functions involved in Add to cart phase.
app/code/core/Mage/Checkout/controllers/CartController.php: addAction()
app/code/core/Mage/Checkout/Model/Cart.php: addProduct($product, $info)
app/code/core/Mage/Sales/Model/Quote.php: addProduct(Mage_Catalog_Model_Product $product, $request=null)
In the above function, you can set the custom values for the product.
Find the for loop:
foreach ($cartCandidates as $candidate) { … }
Inside that custom fields can be set.
01foreach ($cartCandidates as $candidate) {
02            $item $this->_addCatalogProduct($candidate$candidate->getCartQty());
03            ...
04            ...
05            ...
06            $item->setCustomPrice('custom value goes here');
07            $item->setCustomImage('custom value goes here');
08            ...
09            ...
10}
I want the custom image as well. So I have added the one more field in the table ‘sales_flat_quote_item’.
Getting those custom values on the cart page is very easy. Go to template/checkout/cart/item/default.phtml.
The below lines can get those values.
1echo $_item->getCustomImage();
The price on the cart page will replaced automatically by custom_price value if that has been set.

No comments:

Post a Comment