【问题标题】:how to add new custom option when creating new order programmatically in magento?在magento中以编程方式创建新订单时如何添加新的自定义选项?
【发布时间】:2015-02-27 23:01:40
【问题描述】:

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

<?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());
?>

【问题讨论】:

标签: php magento option


【解决方案1】:

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

$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 */
)
));

希望这能给你一个前进的方向......

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-22
    • 2016-08-12
    相关资源
    最近更新 更多