以编程方式创建报价无效 [英] Create quote programmatically doesn't work

查看:72
本文介绍了以编程方式创建报价无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式创建报价.如果sales_flat_quote表为空,则该方法有效.如果不为空,则无法使用.到目前为止,这是我在indexAction()中的代码.

I need to create a quote programmatically. It works if the sales_flat_quote table is empty. If it's not empty then it doesn't work. This is my code so far in indexAction().

$customer_id = Mage::getSingleton('customer/session')->getId();
$product_id = $this->getRequest()->getParam('product_id');
$store_id = Mage:app()->getStore()->getId();
$customerObj = Mage::getModel('customer/customer')->load($customer_id);
$quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
$storeObj = $quoteObj->getStore()->load($store_id);
$quoteObj->setStore($storeObj);
$productModel = Mage::getModel('catalog/product');
$productObj = $productModel->load($product_id);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
$quoteItem->setQuote($quoteObj);
$quoteItem->setQty('1');
$quoteItem->setStoreId($store_id);
$quoteObj->addItem($quoteItem);
$quoteObj->setStoreId($store_id);
$quoteObj->collectTotals();
$quoteObj->save();
$this->_redirect('checkout/cart/');

有什么想法,为什么sales_flat_quote表必须为空才能起作用?

Any idea why the sales_flat_quote table must be empty in order for this to work?

推荐答案

听起来前端购物车代码无法将您的报价与特定客户联系起来.我将在

It sounds like the frontend cart code can't connect your quote with a specific customer. I'd start debugging in the

app/code/core/Mage/Checkout/Model/Cart.php

文件.特别是getQuotesaveQuote方法.确定为什么Magento在前端加载空报价,并且该问题应得到解决.

file. Specifically the getQuote and saveQuote methods. Determine why Magento is loading an empty quote on the frontend, and the problem should be illuminated.

根据以下注释,您似乎在此处实例化代码对象

Based on the comments below, it looks like you're instantiating your code object here

$quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);

这每次都会创建一个新的报价模型对象.如果要保存现有报价,则需要加载它,而不是每次都创建一个新的报价模型对象.您可以使用以下代码通过ID加载现有的报价

This creates a new quote model object each time. If you want to save an existing quote, you need to load it instead of creating a new quote model object each time. You could load an existing quote by id with the following

Mage::getModel('sales/quote')->load($quote_id);

还似乎quote对象具有loadByCustomerId方法,所以您可能会遇到类似这样的事情

It also appears the quote object has a loadByCustomerId method, so you might have some luck with something like this

$quoteObj = Mage::getModel('sales/quote');
$quoteObj = $quoteObj->loadByCustomerId($quoteObj, $customer_id);

这篇关于以编程方式创建报价无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆