2

Here is the part of my code to create an order programmtically:

    //add products
$this->_getOrderCreateModel()->resetShippingMethod(true);
if (is_array($post['order']['add_products']))
    $this->_getOrderCreateModel()->addProducts($post['order']['add_products']);

$this->_getOrderCreateModel()
        ->saveQuote();

//apply custom product price
$productsData = array();
foreach ($this->_getOrderCreateModel()->getQuote()->getItemsCollection() as $item) {
    $productId = $item->getproduct_id();
    foreach ($object['products'] as $srcProduct) {
        if ($productId == $srcProduct['product_id']) {
            if (Mage::getStoreConfig('tax/calculation/price_includes_tax') == 1) {
                $item->setCustomPrice($srcProduct['priceInclTax']);
                $item->setOriginalCustomPrice($srcProduct['priceInclTax']); 

            } else {
                $item->setCustomPrice($srcProduct['priceExclTax']);
                $item->setOriginalCustomPrice($srcProduct['priceExclTax']);

            }
        }
    }
}

I've tried to add the following code to above code but not work

$product = Mage::getModel('catalog/product')->load($product_id); 
$item->addOption(new Varien_Object(
    array(
        'product' => $product,
        'code' => '8',
        'value' => '5'
    )
)); 

anyone knows how to do that?

3
  • You want to add custom option programetically into item, right? Jan 9, 2018 at 12:05
  • @Dhiren Vasoya yes
    – hkguile
    Jan 9, 2018 at 13:02
  • I post answer, please check. Jan 9, 2018 at 13:18

1 Answer 1

1

To add product with the custom option you want to used this code.

$product = Mage::getModel('catalog/product')->load($product_id); 
$Infobyreuest = array(
        'qty' => 1,
        'CUSTOM_OPTION_ID'=>'CUSTOM_OPTION_VALUE',
);
$quote->addProduct($product, new Varien_Object($Infobyreuest));

NOTE : In above code you need to replace 'CUSTOM_OPTION_ID'=>'CUSTOM_OPTION_VALUE' with your Id and value which you want to add.

3
  • i replace 'CUSTOM_OPTION_ID'=>'CUSTOM_OPTION_VALUE' as '8' => '5', it this correct?
    – hkguile
    Jan 9, 2018 at 13:39
  • yes, but have you sure that 8 and 5 are corect value for custom options. Jan 9, 2018 at 13:41
  • i replace this it works : 'options' => array( 8 => 5 )
    – hkguile
    Jan 9, 2018 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.