如何在magento中以编程方式创建新订单时添加新的自定义选项?

时间:2015-01-01 03:58:18

标签: php magento option

我正在以编程方式创建新订单。我想添加新的自定义选项  在创建新订单时,我不知道。如何创建自定义  选项以及如何在下面的代码中添加自定义选项,请帮助我。

<?php
require_once ‘app/Mage.php’;Mage::app();

$quote = Mage::getModel(‘sales/quote’)
->setStoreId(Mage::app()->getStore(‘default’)->getId());

if ('existing') {
// for customer orders:
$customer = Mage::getModel(‘customer/customer’)
->setWebsiteId(1)
->loadByEmail(‘customer@email.com’);
$quote->assignCustomer($customer);
} else {
// for guest orders only:
$quote->setCustomerEmail(‘customer@email.com’);
}

// add product(s)
$product = Mage::getModel(‘catalog/product’)->load(4);
$buyInfo = array(
‘qty’ => 1,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));

$addressData = array(
‘firstname’ => ‘Test’,
‘lastname’ => ‘Test’,
‘street’ => ‘Sample Street 10′,
‘city’ => ‘Somewhere’,
‘postcode’ => ’123456′,
‘telephone’ => ’123456′,
‘country_id’ => ‘US’,
‘region_id’ => 12, // id from directory_country_region table
);

$billingAddress = $quote->getBillingAddress()->addData($addressData);

$shippingAddress = $quote->getShippingAddress()->addData($addressData);

$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod(‘flatrate_flatrate’)
->setPaymentMethod(‘checkmo’);

$quote->getPayment()->importData(array(‘method’ => ‘checkmo’));

$quote->collectTotals()->save();

$service = Mage::getModel(‘sales/service_quote’, $quote);
$service->submitAll();
$order = $service->getOrder();

printf(“Created order %s\n”, $order->getIncrementId());
?>

1 个答案:

答案 0 :(得分:0)

我不确定你真的想做什么...但我认为,你正在寻找类似的东西 而不是......

$product = Mage::getModel(‘catalog/product’)->load(4);
$buyInfo = array(
‘qty’ => 1,
// custom option id => value id
// or
// configurable attribute id => value id
);

USE AND UTILIZE THIS CODE FROM INCHOO SITE

当然,你需要根据自己的需要定制它......

$item = $this->_getOrderCreateModel()->getQuote()->getItemByProduct($this->_product);


$item->addOption(new Varien_Object(
array(
'product' => $this->_product,
'code' => 'option_ids',
'value' => '5' /* Option id goes here. If more options, then comma separate */
)
));

希望这能为你提供一个继续前进的方向......