Magento programmatically create order with custom

2019-07-28 10:11发布

问题:

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!

回答1:

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'.



标签: php magento date