以编程方式为购物车添加物品并分配给访客用户
-
11-12-2019 - |
题
我收到了用于创建报价对象和报价项目对象的代码,我能够通过检查数据库来成功创建报价,我也创建了名为前端的cookie。我现在需要的只是将创建的报价分配给Guest用户,以便我可以让用户结账作为访客,但无法将购物车分配给访客用户。
在购物车中的物品请求将通过JSON请求来自第三方站点,我已经解码和循环。
这是代码
session_name("frontend");
session_start();
$cartInfo = $_GET['items'];
require_once '../app/Mage.php';
Mage::init('default');
$session = Mage::getSingleton('customer/session');
Mage::getSingleton('checkout/cart')->getQuote();
foreach ($cartArray as $key=> $value) {
$productId = $key;
$productQuantity = $value;
$productModel = Mage::getSingleton('catalog/product');
$productObj = $productModel->load($productId);
if (!$quoteObj) {
$quoteObj = new Mage_Sales_Model_Quote();
}
$store_id = Mage::app()->getStore()->getId();
$storeObj = $quoteObj->getStore()->load($store_id);
$quoteObj->setStore($storeObj);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty($productQuantity);
$quoteItem->setStoreId($store_id);
$quoteObj->addItem($quoteItem);
$quoteObj->setStoreId($store_id);
$quoteObj->collectTotals();
$quoteObj->setCustomerId(null);
$quoteObj->save();
$quoteId = $quoteObj->entity_id;
echo $quoteObj->entity_id.'**<br />';
echo $quoteItem->item_id.'---<br />';
}
. 解决方案
终于弄清楚了这个问题,希望它有助于某人,这是答案。
上面的给定代码在问题部分中完美工作,您所要做的就是将自定义文件放在Magento安装的根文件夹中,如果您创建文件夹并放置此代码,则不会工作。代码文件必须在root中。
其他提示
因为这是猜测结账,我可以想到的唯一方法是让客户在他们的浏览器中重新加载报价。因此,您需要通过电子邮件将它们电子邮件发送与报价ID的链接,或者让它们在网站上输入Quote ID。
在控制器中重新加载报价
$id = $this->getRequest()->getParam('quote_id');
$store_id = $this->getRequest()->getParam('store_id');
....
$this->_getSession()->clear();
$this->_getQuote()
->setQuoteId($id)
->setStoreId($store_id)
->setIsActive(true)
->load($id);
.