title: Magento add products programmatically and redirect to checkout
body: <p>I have a external site where you can add products directly from.</p>
  
  <p>So I tried to create a php file which can receive some HTTP POST - but its not working.</p>
  
  <p>First I tried some very basic</p>
  
  <pre><code>&lt;?php
  namespace Addtocart;
  
  use Mage;
  
  require __DIR__ . '/app/Mage.php';
  
  /*
  if (! isset($_POST, $_POST['data'])) {
      exit;
  }
  */
  
  $data = array(
      //array('id' =&gt; 2532, 'qty' =&gt; 1, 'attribute' =&gt; 92, 'value' =&gt; 11), // 002
      //array('id' =&gt; 1854, 'qty' =&gt; 1),
      array('id' =&gt; '5711914018542', 'qty' =&gt; 1)
  );
  
  Mage::app();
  
  $product_model = Mage::getModel('catalog/product');
  
  /** @var \Mage_Checkout_Model_Cart $cart */
  $cart = Mage::getSingleton('checkout/cart');
  $cart-&gt;init();
  
  $errors = array();
  foreach($data as $product) {
      try {
          $productid = (int)$product_model-&gt;getIdBySku($product['id']);
  
          $params = array(
              'product' =&gt; $productid,
              'qty' =&gt; $product['qty']
          );
  
          if (array_key_exists('attribute', $product)) {
              $params['super_attribute'] = array($product['attribute'] =&gt; $product['value']);
          }
  
          $p = $product_model-&gt;load($productid);
          $cart-&gt;addProduct($p, $params);
      } catch (\Exception $e) {
          $errors[$product['id']] = $e-&gt;getMessage();
          continue;
      }
  }
  
  $cart-&gt;save();
  $cart-&gt;getItems()-&gt;setQuote($cart-&gt;getQuote());
  $cart-&gt;save();
  Mage::getSingleton('checkout/session')-&gt;setCartWasUpdated(true);
  
  if ($errors) {
      var_dump($errors);
      exit;
  }
  
  header('Location: /checkout/cart/');
  </code></pre>
  
  <p>But the cart is still empty.</p>
  
  <p>Then I tried looking into the cart <code>var_dump($cart)</code> and found out that all products is actually added, so my guess is the session is changed between <code>Mage::app()</code> and <code>Mage::run()</code> is there any thing I can do to persist the session?</p>
  
  <ul>
  <li>Magento: 1.7.0.2</li>
  </ul>