我在Magento之外运行了测试php脚本,该脚本会将商品添加到购物车中。 效果很好。 这段代码是在这里找到的,很多人在很多帖子中使用了这段代码。 当我将此代码放入现有模块时,不会从新会话添加该项。 如果我去商店添加常规产品(购物车中有商品),模块将正确添加商品。 我知道添加该项目的代码有效。 我遇到的问题是创建购物车的第一个实例。
如果我的购物车中已经有一个普通的 magento 产品,则此代码工作正常:
// this section of code only works if the customer already has a cart item added (cart established)
// for some reason this script fails when no cart exists
$session = Mage::getSingleton('customer/session');
// Get cart instance
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$productId=971;
$productInstance = Mage::getModel('catalog/product')->load($productId);
$param = array(
'product' => $productId,
'qty' => $rfqdata['option_quantity'],
'options' => array(
27 => $rfqdata['option_layers'], // Layers
26 => $rfqdata['option_thickness'], // Thickness
25 => $rfqdata['option_length'], // Length
24 => $rfqdata['option_width'], // Width
23 => $rfqdata['option_color'], // Color
22 => $rfqdata['option_finish'], // Finish
29 => $rfqdata['option_rush'], // Rush
30 => 'tbd' // RFQ Number
)
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $param);
$session->setCartWasUpdated(true);
$cart->save();
这以某种方式导致错误,并且模块添加到购物车操作未完成。 我找不到任何有用的错误消息。我已经在堆栈,谷歌上搜索了100多个页面,但我找不到解决这个问题的方法。 显然,如果我的模块在页面加载 1 中不起作用,它就毫无价值。
我之前尝试发布此内容,但没有得到任何回应。 我真的需要一些帮助。 如果您无法回答,您能告诉我在哪里可以找到付费的Magento支持吗?
这是解决方案,也很难找到....
if(empty($quote)){
$checkout = Mage::getSingleton('checkout/session');
$quote = $checkout->getQuote();
$session = Mage::getSingleton('customer/session');
if($session->isLoggedIn()) {
// look up customer
$customerSession = $session->getCustomer();
$customer = Mage::getModel('customer/customer')->load($customerSession->getId());
$quote->assignCustomer($customer);
$quote->setIsMultiShipping(false);
}
$quote->save();
}