3

I want to save some custom data to quote and order item, when a product is added to cart. I have a custom controller with an addtocartAction where i do the following:

...
$cart->addProduct($product, array(
    'qty' => $params['qty'] ? $params['qty'] : 1
));
...

The action is called via ajax with some data i have to save to the quote item. Where can i do that? Adding the custom attribute to the quote item is not the problem, but changing the value of the attribute. The Product can be more than one time in cart, but with different attribute values in the quote item.

3 Answers 3

8

Instead of using a custom controller it's easier (and perhaps cleaner) to do this from a controller. So, we'll need to build a little module for that

config.xml

<?xml version="1.0"?>
<config>
     [...]
  <global>
     <fieldsets>
         <sales_convert_quote_item>
             <custom_attribute_column>
                 <to_order_item>*</to_order_item>
             </custom_attribute_column>
         </sales_convert_quote_item>
     </fieldsets>
     <resources>
         <[namespace]_[module]_setup>
              <setup>
                  <module>[Namespace]_[Module]</module>
                  <class>Mage_Sales_Model_Mysql4_Setup</class>
              </setup>
         </[namespace]_[module]_setup>
     </resources>
     <models>
          <[namespace]_[module]>
               <class>[namespace]_[module]_Model</class>
          </[namespace]_[module]>
     </models>
     <events>
          <checkout_cart_product_add_after>
               <observers>
                    <[namespace]_[module]_checkout_cart_product_add_after>
                         <type>model</type>
                         <class>[Namespace]_[Module]_Model_Observer</class>
                         <method>checkoutCartProductAddAfter</method>
                    </[namespace]_[module]_checkout_cart_product_add_after>
               </observers>
          </checkout_cart_product_add_after>
     </events>
  </global>
     [...]
</config> 

your setup script

$installer = $this;
$installer->getConnection()->addColumn($this->getTable('sales_flat_quote_item'), 'custom_attribute_column', 'varchar(255) NOT NULL');
$installer->getConnection()->addColumn($this->getTable('sales_flat_order_item'), 'custom_attribute_column', 'varchar(255) NOT NULL');
$installer->endSetup();

And the observer [Namespace]/[Module]/Model/Observer.php

class [Namespace]_[Module]_Model_Observer
{
    public function checkoutCartProductAddAfter($observer)
    {
        $item = $observer->getEvent()->getQuoteItem();
        $item->setData('custom_attribute_column', 'the value');
        $item->save();

        return $this;
    }
}
4
  • 1
    this is good one Commented May 21, 2015 at 8:38
  • Useful. I have one check point here. In observer, how can I get list of custom options associated for the products I am adding to cart in checkout_cart_product_add_after event?
    – Anto S
    Commented Aug 24, 2016 at 19:03
  • @invissible I'm guessing it differs per product, printing it in the observer would be an option Commented Aug 25, 2016 at 6:31
  • @SanderMangel yes I am trying to print it in observer only. Here you are using $item->setData('custom_attribute_column', 'the value'); but custom options will not get variable name right? We are getting it as option['<ID_NUMBER>']
    – Anto S
    Commented Aug 25, 2016 at 7:03
0
$item->setQty($qty);
$item->setCustomPrice($new_price);
$item->setOriginalCustomPrice($new_price);
$item->getProduct()->setIsSuperMode(true);
$item->save();
 $quote->save();
 $quote->setTotalsCollectedFlag(false)->collectTotals(); 
0

In my case first variant, with observer, work only without line $item->save(). Otherwise i get exception when add product to cart: exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

Your Answer

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

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