Programatically adding products to a user's remote magento cart

时间:2015-09-01 21:52:40

标签: ajax magento cors e-commerce commerce

I have a webpage where users can add products from 3rd party commerce sites (amazon, shopify, magento, etc) from my page. users select multiple products then click checkout, then the page redirects them to the checkout page in the 3rd party commerce site.

this works fine with Amazon as they have a server-side API that we proxy. Input: a bunch of products, output: a checkout URL. pretty simple and it works pretty well.

we're stuck with magento, however.

  • There is no public API for adding a product to a user's remote cart
  • The undocumented API only supports adding a single product at a time, so supporting multiple products requires multiple API calls (which is slow)
  • Most magento sites do not support HTTPS and/or redirect to HTTP all the time, and our site uses HTTPS. Thus, it doesn't really work.
  • We run into CORS issues when we try AJAX requests - we're currently hacking it with iframes.
  • The first request gives us a "no cookie" error page instead of actually adding it to the cart.

Is there a way to solve this? Does magento support CORS requests? What is magento's support with HTTPS?

I see http://community.magento.com/t5/Programming-Questions/API-Redirecting-user-to-magento-instance-to-view-their-cart/m-p/9113#M2029 which requires our client to install an extension, but that may not be acceptable

thanks

2 个答案:

答案 0 :(得分:1)

虽然Magento中没有支持开箱即用的API功能,但可以通过编写一个小型API模块来扩展Magento,以便安装在Magento中。通过创建API扩展,您可以制定规则,接受 - 例如 - 将多个产品添加到购物篮中。

Magento的文档一直很差,通常像我这样的开发人员必须调试并逐步完成代码,以了解特定功能的实际工作方式。一旦你变得足够熟练,你就会开始看到“更大的图片”,它允许你“预测”Magento的行为,并且经常允许你跳过逐步的调试调查。

我建议您从专业开发人员那里获得帮助,建立这个(我会说至少2 - 3年的后端开发经验),并获得认证的Magento开发人员来帮助您:业余爱好者可能会做更少的工作解决方案,但您需要长期付出代价,更不用说Magento版本和版本的模块可移植性,以及与安全相关的问题(XSS,SQL注入等)。

Magento支持HTTPS(实际上,这取决于网站的系统管理员,正确配置Web服务器),并且有一些CORS支持(默认情况下,“同源”策略已实现)。更改CORS也是一项开发任务,即使我没有真正看到这与REST或SOAP API有何关联。

另外,请注意,Magento严重依赖cookie来支持会话(并且篮子存储在数据库中以防止客户会话)。

这就是我所能说的,关于你提出的实际问题的信息很少。

如果您需要更多信息,我很乐意提供帮助,但我需要更多详细信息,以便找出最佳解决方案。

答案 1 :(得分:1)

也许你可以用这样的查询字符串来尝试吗?

<?php

$formKey = Mage::getSingleton('core/session')->getFormKey();?>

<form action="/checkout/cart/add/product/<?php echo $productid; ?>" method="post">
    <input type="hidden" name="form_key" value="<?php echo $formKey; ?>" />

    <input type="text" name="qty"> QTY

    <input type="submit" value="Add to basket" />
</form>