[英]Add product to cart outside of magento (external script)
I have to add a product through an external page into the cart under the same URL. 我必须通过外部页面将产品添加到同一URL下的购物车中。
The difference between these pages is that eg test.de
is running TYPO3 and test.de/Shop
is running Magento and test.de/productpage
is running an external script through TYPO3. 这些页面之间的区别在于,例如,
test.de
正在运行TYPO3,而test.de/Shop
正在运行Magento,而test.de/productpage
正在通过TYPO3运行外部脚本。
From the product page, I have to add a product to the Magento cart to proceed the checkout. 在产品页面上,我必须将产品添加到Magento购物车中才能进行结帐。
The key problem is to start the session outside of Magento. 关键问题是在Magento外部启动会话。 If I call
test.de/Shop/checkout/cart
before I go to test.de/productpage
and add a product to the cart, it works flawlessly. 如果在转到
test.de/productpage
并将产品添加到购物车之前先致电test.de/Shop/checkout/cart
,它会正常工作。 But if I go the normal way (site -> productpage -> cart) I cant get Magento to listen to the session on the product page. 但是,如果我按常规方式(站点->产品页面->购物车),则无法使Magento收听产品页面上的会话。
I have something like this to call 我有这样的电话
function addToBasket()
{
require_once('../app/Mage.php');
ob_start();
session_start();
umask(0);
session_write_close();
Mage::app()->setCurrentStore(33);
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$productId = !isset($_GET['activeProdId']) ? '' : $_GET['activeProdId'];
$qty = !isset($_GET['qty']) ? '1' : $_GET['qty'];
if(empty($productId)) {
return "no product-id found";
}
$request = Mage::app()->getRequest();
$product = Mage::getModel('catalog/product')->load($productId);
$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product, array('qty' => $qty));
$session->setLastAddedProductId($product->getId());
$session->setCartWasUpdated(true);
$cart->save();
return true;
}
The addToBasket is called through ajax. addToBasket通过ajax调用。
So the question is: How do I start the session outside of the Magento-scope and put products into the cart? 所以问题是:如何在Magento范围之外启动会话并将产品放入购物车?
once you include Mage.php you will need to initialize Magento with 一旦包含Mage.php,您将需要使用初始化Magento
Mage::app("default");
(first param is store code) (第一个参数是商店代码)
after that... 之后...
Mage::getSingleton('core/session', array('name'=>'frontend'));
...will work as it should. ...将如期运作。
Add first (like @samsonovits suggested) I had to add the store code in mage app and the store id (didn't test if the store-id is necessary after adding the store code). 首先添加(如建议的@samsonovits ),我必须在Mage应用程序中添加商店代码和商店ID(添加商店代码后,无需测试是否需要商店ID)。
Mage::app("default")->setCurrentStore(33);
Afterwards I called the frontend on Mage::app
之后,我在
Mage::app
上调用了前端
Mage::app()->loadArea('frontend');
I switched the Mage::helper('checkout/cart')
to the singleton Mage::getSingleton('checkout/cart')
. 我将
Mage::helper('checkout/cart')
切换为单例Mage::getSingleton('checkout/cart')
。
Since Magento 1.8
we have to add a form_key
to add products through an external script. 从
Magento 1.8
我们必须添加form_key
才能通过外部脚本添加产品。
This was accomplished by the following code: 这是通过以下代码完成的:
$param = array('product' => $productId,
'qty' => $qty,
'form_key' => Mage::getSingleton('core/session')->getFormKey());
$request = new Varien_Object();
$request->setData($param);
Now we could proceed with a $cart->addProduct($product, $request)
and $cart->save()
to save the cart. 现在我们可以继续使用
$cart->addProduct($product, $request)
和$cart->save()
保存购物车。
There is one more important part to change (since the external script is not in the same scope like magento) - the cookie url and path. 有一个更重要的部分需要更改(因为外部脚本与magento不在同一范围内)-cookie url和路径。
System -> Configuration -> Web -> Cookies
系统->配置->网站-> Cookies
There we have to customize two fields: 在这里,我们必须自定义两个字段:
Cookie-Path: /Shop
(where /Shop
is the destination of magento) Cookie-Path: /Shop
(其中/Shop
是magento的目的地)
Cookie-Domain: .test.de
(where .test.de
is the URL of the project) Cookie-Domain: .test.de
(其中.test.de
是项目的URL)
After this the /Shop/checkout/cart
was able to inherit the frontend-Cookie
of the external script ( productpage
) 此后,
/Shop/checkout/cart
能够继承外部脚本的frontend-Cookie
( productpage
)
NOTE: Without changing the Cookie-Path & Cookie-Domain Magento will create two frontend cookies with different content.
注意:在不更改Cookie路径和Cookie域的情况下,Magento将创建两个具有不同内容的前端cookie。
Full code: 完整代码:
function addToBasket()
{
require_once('path/to/Mage.php');
umask(0);
Mage::app("default")->setCurrentStore(33);
umask(0);
Mage::app()->loadArea('frontend');
$productId = !isset($_GET['activeProdId']) ? '' : $_GET['activeProdId'];
$qty = !isset($_GET['qty']) ? '1' : $_GET['qty'];
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
$cart = Mage::getSingleton('checkout/cart')->setStoreId(33);
$cart->init();
$paramater = array('product' => $productId,
'qty' => $qty,
'form_key' => Mage::getSingleton('core/session')->getFormKey()
);
$request = new Varien_Object();
$request->setData($paramater);
if(empty($productId)) {
return "no product-id found";
}
$product = Mage::getModel('catalog/product')->load($productId);
$cart->addProduct($product, $request);
$session->setLastAddedProductId($product->getId());
$session->setCartWasUpdated(true);
$cart->save();
return 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.