Magento以编程方式创建类型为"date"的自定义选项的订单. [英] Magento programmatically create order with custom option of type "date"
问题描述
我正在尝试根据来自某个地方的文本文件提供的数据在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
标识为"7"的自定义选项的类型为日期"(从Magento的Web后端配置),这肯定是引起问题的原因:当将自定义选项7更改为文本字段"时,此代码片段效果很好. /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}
那么,如何使此类型为"date"的自定义选项接受日期字符串?这样传递日期字符串甚至正确吗?
谢谢大家!
我猜对了,我猜对了!是的!
$request->setData(array(
'product' => $product->getId(),
'qty' => 1,
'options' => array(
30 => 'some text...',
8 => 'some other text...',
7 => array(
'year' => 2014,
'month' => 1,
'day' => 23
)
)
));
此内容已正确保存为日期"类型的选项.
i'm trying to automatically create new orders in magento based from data given by textfiles coming from somewhere. This is the basic code i'm using:
$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
Custom option with id "7" is of type "date" (configured from Magento's web backend), and this is definitely what causes problems: this snippet works well when custom option 7 is changed to type "text field".
If i inspect the contents of the $quote
object after calling addProduct()
, i see that the value of custom option 7 is not a date string, as expected, but a single digit (which appears to be exactly the first digit of the date formatted as Y-m-d H:i:s
, thus the first digit of the year, as i tried with different years).
If i change the type of option 7 to text, the whole date string is correctly saved inside the $quote
object.
As additional information, if i let the script go on through all the order-creating process, what i get is an exception that says:
Unsupported ISO8601 format (2)
where the number 2
is the first digit of the date i was talking before. So i tried to use the iso8601
format:
7 => date('c')
with no effect at all.
The stack trace of that exception is:
#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}
So, how to make this custom option of type "date" accept date strings? Is it even correct to pass date strings that way?
Thank you all!
I guessed it and i guessed it right! Yay!
$request->setData(array(
'product' => $product->getId(),
'qty' => 1,
'options' => array(
30 => 'some text...',
8 => 'some other text...',
7 => array(
'year' => 2014,
'month' => 1,
'day' => 23
)
)
));
This is correctly saved as an option of type 'date'.
这篇关于Magento以编程方式创建类型为"date"的自定义选项的订单.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!