【发布时间】:2014-01-22 18:02:24
【问题描述】:
我正在尝试根据来自某个地方的文本文件提供的数据在 magento 中自动创建新订单。这是我正在使用的基本代码:
$product = Mage::getModel('catalog/product')->load($productId);
$request = new Varien_Object();
$request->setData(array(
'product' => $product->getId(),
'qty' => 1,
'options' => array(
30 => 'some text...',
8 => 'some other text...',
7 => date('Y-m-d H:i:s')
)
));
$quote = Mage::getModel('sales/quote')
->setStoreId($storeId)
->setIsMultiShipping(false)
->setCheckoutMethod('guest')
->setCustomerId(null)
->setCustomerEmail($customerEmail)
->setCustomerIsGuest(true)
->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
$quote->addProduct($product, $request);
// ...the interesting part ends here
ID为“7”的自定义选项属于“日期”类型(从Magento的Web后端配置),这绝对是导致问题的原因:当自定义选项7更改为“文本字段”时,此sn-p运行良好.
如果我在调用addProduct() 后检查$quote 对象的内容,我看到自定义选项7 的值不是日期字符串,正如预期的那样,而是一个数字(这似乎是第一个日期的数字格式为Y-m-d H:i:s,因此是年份的第一个数字,因为我尝试使用不同的年份)。
如果我将选项 7 的类型更改为文本,则整个日期字符串会正确保存在 $quote 对象中。
作为附加信息,如果我让脚本继续执行所有订单创建过程,我得到的是一个例外,上面写着:
Unsupported ISO8601 format (2)
2 是我之前所说的日期的第一位数字。所以我尝试使用iso8601 格式:
7 => date('c')
完全没有效果。
该异常的堆栈跟踪是:
#0 app/code/core/Zend/Date.php(1091): Zend_Date->_calculate('set', '2', 'c', 'it_IT')
#1 app/code/core/Zend/Date.php(210): Zend_Date->set('2', 'c', 'it_IT')
#2 app/code/core/Mage/Core/Model/Locale.php(494): Zend_Date->__construct('2', 'c', Object(Zend_Locale))
#3 app/code/core/Mage/Catalog/Model/Product/Option/Type/Date.php(167): Mage_Core_Model_Locale->date('2', 'c', NULL, false)
#4 app/code/core/Mage/Catalog/Model/Product/Type/Abstract.php(620): Mage_Catalog_Model_Product_Option_Type_Date->getFormattedOptionValue('2')
#5 app/code/core/Mage/Sales/Model/Convert/Quote.php(141): Mage_Catalog_Model_Product_Type_Abstract->getOrderOptions(Object(Mage_Catalog_Model_Product))
#6 app/code/core/Mage/Sales/Model/Service/Quote.php(170): Mage_Sales_Model_Convert_Quote->itemToOrderItem(Object(Mage_Sales_Model_Quote_Item))
#7 app/code/core/Mage/Sales/Model/Service/Quote.php(249): Mage_Sales_Model_Service_Quote->submitOrder()
#8 wbs-import-orders.php(309): Mage_Sales_Model_Service_Quote->submitAll()
#9 {main}
那么,如何让这个“日期”类型的自定义选项接受日期字符串呢?以这种方式传递日期字符串是否正确?
谢谢大家!
【问题讨论】: