Oct
3
2009

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.


foreach ($cartCandidates as $candidate) {
            $item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());
            ...
            ...
            ...
            $item->setCustomPrice('custom value goes here');
            $item->setCustomImage('custom value goes here');
            ...
            ...
}

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.

echo $_item->getCustomImage();

The price on the cart page will replaced automatically by custom_price value if that has been set.

Related Posts

About the Author: Jignesh Patel

Jignesh Patel is a geek, an engineer & a web developer, who focus on developing web sites which look great, work great and can still work on older web browsers. Expertise in PHP, MySQL, JS, Magento & Wordpress.

  • Ivan

    very useful articles – keep them coming!

  • http://www.octalsoftware.com Rakesh

    Very useful article, I was trying to set custom price and for this written a long code in cart model. It helped a lot.

  • rijo

    Friend this helped me a lot. But still i am struggling with an issue. I need to put a custom field (select/text) in the product page (for all products) and the customer can choose the or enter the option. I need the value of this field displayed in cart page and also stored in quote items. Hope you will help me
    Thanks in advance.

  • John

    Hi Rakesh

    can you please discuss all the changes you made for assigning custom price for product.

    thanks
    John

  • chii

    How can i add custom options of bundle product’s item in cart ??

  • Chii

    this is not working in magento 1.4.1.1 :(

  • Chii

    Hello,

    Still waiting for your replay…. Above method is not working in Magento 1.4.1.1.

    Any solutions for that???

  • Anonymous

    I will need to check this on that version. Because I implemented this more than a year ago. And then never tried to implement on 1.4. I am planning to make it work with latest magento version in few days.

  • Mohit Malik

    Hi,
    Is their any update on this script so that it works for latest Magento versions like v1.4.x and v1.5.x? I have tried using the above script in both the version but it does not work.

    Thank you in advance!

  • Shylaja B

    Hello

    For magento version 1.5+ u need to add one more line after
    $item->setCustomPrice(‘custom value goes here’);
    $item->setOriginalCustomPrice(‘custom value goes here”);

  • Anonymous

    Thanks for sharing. Hope this will help to others.

  • Anonymous

    Hi..  Can you try the solution written by Shylaja B in this thread.Best

  • Gerrit

    Thank you very helpfull.

    $item->setCustomPrice(‘custom value goes here’);
    $item->setOriginalCustomPrice(‘custom value goes here”);

    as per Shylaja B works for me in 1.4.1

  • Ashar Khan

    Good post. right on target. thanks!!!

blog comments powered by Disqus