Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save joshcarlson/7de96a5f51bc3c487394 to your computer and use it in GitHub Desktop.

Select an option

Save joshcarlson/7de96a5f51bc3c487394 to your computer and use it in GitHub Desktop.
Magento add to cart from url, useful for email promos or add to cart from offsite links
<?php
/**
* add to cart from url, useful for email promos
* based on http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/adding_a_product_to_the_cart_via_querystring
* which no longer works because of session validation of form_key
* usage: link to add.php?product=[id]&qty=[qty]
*/
require 'app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', array('name' => 'frontend'));
// init params
$productId = isset($_GET['product']) ? (int) $_GET['product'] : null;
$qty = isset($_GET['qty']) ? (int) $_GET['qty'] : 1;
// if invalid params, send to homepage
if ( !$productId || !$qty ) {
header('Location: ' . Mage::getUrl(''));
exit;
}
// load product and get url
$_product = Mage::getModel('catalog/product')->load($productId);
$url = Mage::helper('checkout/cart')->getAddUrl($_product, ['qty'=>$qty]);
header('Location: ' . $url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment