title: Magento - Add product programmatically to cart through observer
body: <p>Here is my questions: What is the right event to hook in for adding a product to quote programmatically (entry sales_flat_quote_item table). Also i have to catch the items/products added to cart from an user/customer, because their data informations will determine the product which will be added programmatically. So the scenario would be:</p>
  
  <ol>
  <li>user/customer adds a product to cart</li>
  <li>find the right event for hook in</li>
  <li>fetch informations about the products added to cart</li>
  <li>add additional product to cart based on a product id and modified its data</li>
  </ol>
  
  <p>In my opinion its better to work with the quote, before the products are written to the database.</p>
  
  <p>I figure out how to add it through <strong>Mage_Sales_Model_Quote::_addCatalogProduct()</strong>. But i would like to do it through an event observer instead of overwriting core classes. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After more research and suggestions here in the post, i was able to get it working. The key thing for me was the understanding of the objects in the observer, their classes and class methods with </p>
  
  <pre><code>var_dump(get_class($quote)); // $item // $product
  var_dump(get_class_methods($quote)); // $item // $product
  </code></pre>
  
  <p>Now knowing the methods are available its easier to figure it out:</p>
  
  <p>Event:</p>
  
  <pre><code>        &lt;events&gt;
          &lt;checkout_cart_product_add_after&gt;
              &lt;observers&gt;
                  &lt;unifiedarts_configurablebundleset&gt;
                      &lt;type&gt;singleton&lt;/type&gt;
                      &lt;class&gt;Namespace_ConfigurableBundleSet_Model_Observer&lt;/class&gt;
                      &lt;method&gt;salesQuoteEditItems&lt;/method&gt;
                  &lt;/unifiedarts_configurablebundleset&gt;
              &lt;/observers&gt;
          &lt;/checkout_cart_product_add_after&gt;
      &lt;/events&gt;
  </code></pre>
  
  <p>And observer data:</p>
  
  <pre><code>public function salesQuoteEditItems($observer)
  {
      $event = $observer-&gt;getEvent();
      $item = $event-&gt;getQuoteItem();
      $product = $item-&gt;getProduct();
      $quote = $item-&gt;getQuote();
  
      $parent = Mage::getModel('catalog/product')-&gt;load($product-&gt;getParentProductId());
  
      if($parent-&gt;getTypeId() == 'bundle')
      {
  
          if($product-&gt;getTypeId() == 'configurable')
          {
  
              if ($simpleItem = $item-&gt;getOptionByCode('simple_product'))
              {
                  // check if the simple product is in the table
                  if(!$quote-&gt;hasProductId($simpleItem-&gt;getProduct()-&gt;getId()))
                  {
                      echo '&lt;br /&gt;&lt;br /&gt; no simple, add it';
                      echo ' simple id '.$simpleItem-&gt;getProduct()-&gt;getId();
  
                      $simple = Mage::getModel('catalog/product')-&gt;load($simpleItem-&gt;getProduct()-&gt;getId());
                      $quoteItem = Mage::getModel('sales/quote_item')-&gt;setProduct($simple);
                      $parentItem = ( $item-&gt;getParentItem() ? $item-&gt;getParentItem() : $item );
  
                      echo 'simple id '.$simpleItem-&gt;getProduct()-&gt;getId();
  
                      $item-&gt;setRowWeight($product-&gt;getWeight());
  
                      $quoteItem-&gt;setQuote($quote);
                      $quoteItem-&gt;setQty('1');
                      $quoteItem-&gt;setParentItem($parentItem);
                      $quoteItem-&gt;setStoreId(Mage::app()-&gt;getStore()-&gt;getId());
                      $quoteItem-&gt;setOriginalCustomPrice(0);
  
                      $quote-&gt;addItem($quoteItem);
                      $quote-&gt;save();
  
                  }
                  else
                  {
                      echo 'simple found change someting'; die;
                  }
              }
          }
      }
  
  }
  </code></pre>
  
  <p>The hole thing is that my extension has to add an configured Item with the bundle. The product view is working. The hard task is to get it in the cart. Now, everything is working fine till going back and add the product again with different (configured) options. </p>
  
  <p>You see above i try to check for the simple product (which i add manually). The code generates two new rows in the <strong>sales_flat_quote_item</strong> table. </p>
  
  <p>How can i delete the old two entries, or is there a different better approach to set the new data? thanks :)</p>
  
  <p><img src="https://i.stack.imgur.com/N8IS7.png" alt="enter image description here"></p>