简体   繁体   English

Magento以编程方式使用“日期”类型的自定义选项创建订单

[英]Magento programmatically create order with custom option of type “date”

i'm trying to automatically create new orders in magento based from data given by textfiles coming from somewhere. 我试图根据来自某个地方的文本文件给出的数据自动在magento中创建新订单。 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". 标识为“ 7”的自定义选项的类型为“日期”(从Magento的Web后端配置),这绝对是导致问题的原因:当将自定义选项7更改为“文本字段”时,此代码片段效果很好。

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 Ymd H:i:s , thus the first digit of the year, as i tried with different years). 如果我在调用addProduct()之后检查$quote对象的内容,我会看到自定义选项7的值不是预期的日期字符串,而是一位数字(似乎恰好是日期的第一位)格式为Ymd H:i:s ,因此是年份的第一位数字,就像我尝试使用不同的年份一样)。

If i change the type of option 7 to text, the whole date string is correctly saved inside the $quote object. 如果我将选项7的类型更改为文本,则整个日期字符串将正确保存在$quote对象内。

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. 其中数字2是我之前谈论的日期的第一位数字。 So i tried to use the iso8601 format: 所以我尝试使用iso8601格式:

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? 那么,如何使此类型为“ date”的自定义选项接受日期字符串? 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'. 可以将其正确保存为“日期”类型的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM