Magento Forum

   
How to set product attributes while creating order programmatically
 
katherinbridges
Jr. Member
 
Total Posts:  29
Joined:  2009-08-27
 

Hi,
I am using magento 1.7.0.2, I have a script which places order with product,customer info. The order is created successfully, but I am facing a problem to set product attribute while running this script to create order. I am posting my second function which sets information however my first function sets customer, product and billing/shipping address.

public function createOrder($quoteId
    
{
        $status_arr 
= array();
        
$quoteObj Mage::getModel('sales/quote')->load($quoteId);
        
$items $quoteObj->getAllItems();
        
$quoteObj->reserveOrderId();        
        
$convertQuoteObj Mage::getSingleton('sales/convert_quote');
        
$orderObj $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());        
        
$orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
        
$orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
        
$orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));        
        foreach (
$items as $item{
            $item
->setOriginalPrice($item->getProduct()->getPrice());
            
$orderItem $convertQuoteObj->itemToOrderItem($item);
            
$options = array();

            
$orderItem->setProductOptions(new Varien_Object(
            array(
            
'product' => $item->getProduct(),
            
//'code' => 'shoe_size',
            
'code' => '502',
            
'value' => '41' // Option id goes here. If more options, then comma separate 
            
)
            ));
        
        if (
$productOptions $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
            $options 
$productOptions;
        
}
        
if ($addOptions $item->getOptionByCode('additional_options')) {
            $options[
'additional_options'unserialize($addOptions->getValue());
        
}
        
if ($options{
            
//$orderItem->setProductOptions($options);
        
}
            
if ($item->getParentItem()) {
                $orderItem
->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
            
}
            $orderObj
->addItem($orderItem);
        
}        
        $orderObj
->setCanShipPartiallyItem(false);
      
$orderObj->setState(Mage_Sales_Model_Order::STATE_NEWtrue);      
        try 
{
            $orderObj
->place();
        
catch (Exception $e){        
            Mage
::log($e->getMessage());
            
Mage::log($e->getTraceAsString());
        
}
        $orderObj
->sendNewOrderEmail(); 
        
$orderObj->setEmailSent(true);
        
$orderObj->save(); 
        
$last_insert_order_id $orderObj->getRealOrderId();
        return 
$last_insert_order_id;        
    
}
I am testing this after installing magento sample data, where men’s shoes[id=93], its attribute is size/8.
I will be very thankful any kind of help.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top