简体   繁体   中英

Magento: add product to cart by ajax request from external site

I need to add product to magento cart from another site. I make ajax request to magento function with code

    $product_id = $this->getRequest()->getParam('id');
    $cart = Mage::getSingleton('checkout/cart');
    $cart->init();
    $product = Mage::getModel('catalog/product')->load($product_id);    
    $cart->addProduct($product, array('qty' => 1));
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

New row appears in table sales_flat_quote, but when I open magento site - cart is empty. Why is that? What should I change in code to make it work?

I figured it out. Thanks to this Cross domain jQuery ajax call with credentials question. All I need is add

  xhrFields: {
     withCredentials: true
  }

for my ajax request in js script and

header("Access-Control-Allow-Credentials:true");

in php code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM