4

I have a Magento v1.9.2.3 webshop and I'm also adding my products to my Facebook shopping page.

I thought I could use the form action url from my webshop as the payment url in FB shopping: my-webshop/checkout/cart/add/uenc/aHR0cHM6Ly93d3cuc2NyYXB3ZWJzaG9wLm5sL3N0ZXJyZW5iZWVsZC1rbmlwdmVsbGVuLmh0bWw_X19fU0lEPVU,/product/5130/form_key/bO1glkQ2GyvpRd7m/

But after reading I (and trying) I understand the form_key is changed after every cache clear/refresh (which we do very regularly). So I need a more flexible solution.

How can I solve this? I can't imagine I'm the first to try this. Should I create a new php page that will do this or ... ?

Thanks for any advice.

EDIT

I created a new php page in the root of my webshop and added these lines:

 $product = Mage::getModel('catalog/product')->load(5130);
 echo Mage::helper('checkout/cart')->getAddUrl($product, array('qty'=>1, 'form_key'=>Mage::getSingleton('core/session')->getFormKey()));

The resulting url is my-webshop/checkout/cart/add/uenc/aHR0cHM6Ly93d3cuc2NyYXB3ZWJzaG9wLm5sL0FkZFRvQ2FydC5waHA,/product/5130/form_key/lPCvzei32zLtxqxp/qty/1/ but according to the form url it should be my-webshop/checkout/cart/add/uenc/aHR0cHM6Ly93d3cuc2NyYXB3ZWJzaG9wLm5sL3NhbGUvc3RlcnJlbmJlZWxkLWtuaXB2ZWxsZW4uaHRtbD9fX19TSUQ9VQ,,/product/5130/form_key/kqe8DtKacPrJzjgN/ Notice the difference of the form_key ?!?!?

The resulting URL is not working, but the form url is. So this is not a working solution.

Found something else and added these lines of code:

try {     
    $cart = Mage::getSingleton('checkout/cart');
    $cart->init();
    $product = Mage::getModel('catalog/product')->load(5130);

    $parameter = array('product' => '5130',
                'qty' => '1',
                'form_key' => Mage::getSingleton('core/session')->getFormKey()
        );       

    $request = new Varien_Object();
    $request->setData($parameter);
    $cart->addProduct($product, $request);
    $cart->save();     
    Mage::getSingleton('customer/session')->setCartWasUpdated(true); 
    echo 'product added<br>';
} catch (Exception $e) {
  print_r($e->getMessage());
}

product added was printed but when I go to my cart it is still empty. So this is not working either.

I would really much appreciate if someone would help me out.

  • Sadly no answer. I did some more looking and found dozen other similar questions with no working solution either. I'll update my post with some code I tried so far (without any luck) – Paul Meems Nov 19 '18 at 20:47
  • Not sure if my answer from 4 years ago is still valid for 1.9, but you can give it a try: magento.stackexchange.com/a/37781/146 – Marius Nov 20 '18 at 17:53
  • Create a controller action on which you can receive the product sku or id and add the product to cart programatically. And create a url like abc.com/frontname/actionname/id/product_id and put this url in your facebook page. If you want me to create a module then I can write an answer. Let me know if this logic helps you. – Sukumar Gorai Nov 21 '18 at 9:21
0

I did see the controller suggestion before, but I find it a bit complicated for my skills ;)

I found this answer: https://stackoverflow.com/questions/47281786/add-product-to-cart-outside-of-magento-external-script Which I merged with what I already had. And the trick seems to be to call $session = Mage::getSingleton("customer/session"); then you get the correct form-key.

Here's my complete script, which seems to work:

/* Configuration */
define('WEBSTORE_NAME','default');

$productId = !isset($_GET['id']) ? '' : $_GET['id'];
if(empty($productId)) {
    // "no product-id found";
    redirect("my-webshop", 301);
}    

// https://stackoverflow.com/questions/47281786/add-product-to-cart-outside-of-magento-external-script
require_once 'app/Mage.php';
umask(0);
Mage::app(WEBSTORE_NAME);     
umask(0);
Mage::app()->loadArea('frontend');

Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");

$product = Mage::getModel('catalog/product')->load($productId);   
$url = Mage::helper('checkout/cart')->getAddUrl($product, array('qty'=>1, 'form_key'=>Mage::getSingleton('core/session')->getFormKey()));     

redirect($url);

// https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php
function redirect($url, $statusCode = 303)
{
   header('Location: ' . $url, true, $statusCode);
   die();
}

And I call it as this: my-webshop/AddToCart.php?id=5130

| improve this answer | |
0

I know this might be a little late, but you should be able to do this.

yourdomain.com/checkout/cart/add?product=sku&qty=1

| improve this answer | |
  • Thanks @Andreas I tried that but that doesn't work (anymore). Apparently after Magento v1.7 the form_key is needed which changes very regularly. – Paul Meems Nov 25 '18 at 9:53
  • We are in magento 1.9 and im pretty sure it works fine. :) – Andreas Therkildsen Nov 26 '18 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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