We have chunk of a code which helps to add Configurable product while placing order but the issue is only product shows inside order not options off the configurable product as displayed in following image.
Following code is used to placed the order please let me know if you found any bug inside the code.
require_once 'app/Mage.php'; umask(0); Mage::app("default"); $recurringdata = array( "frequencytime" => "30", "payment" => array( "cc_number" => "4111111111111111", "cc_exp_month" => "10", "cc_exp_year" => "2023", "cc_cid" => "121", "cc_type" => "VI" ), "billingadd" => "samebilling", "sku" => "TA", "first_name" => "banana", "last_name" => "banana", "email" => "[email protected]", "phone" => "123456", "address" => "Test Address", "unit" => "202", "city" => "Arizona", "zip" => "86005", "country" => "US", "state" => "NY", "samplepro_name" => "", "samplepro_price" => "", "sampleprobuy" => "no", "samplepro_id" => "", "currentsku" => "TA", "shipping" => "$9.99", "tax_total" => "$4.05", "currentpro_name" => "Test Product", "currentpro_id" => "303", "currentpro_price" => "$60.00", "currentpro_single_price" => "$60.00", "currentpro_qty" => "1", "total" => "$74.04", "countryshippingadd" => "yes" ); //print_r($recurringdata);exit; $finalordertax = substr($recurringdata['tax_total'], 1); $finalordershipping = substr($recurringdata['shipping'], 1); $storeId = 1; $transaction = Mage::getModel('core/resource_transaction'); $reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId); $quoteObj = Mage::getModel('sales/quote')->setStoreId($storeId); $quoteObj->setCustomerEmail($recurringdata['email']); $Storecurrencycode = Mage::app()->getStore()->getCurrentCurrencyCode(); $Basecurrencycode = Mage::app()->getStore()->getBaseCurrencyCode(); $o_date = date('Y-m-d H:i:s'); $order = Mage::getModel('sales/order') ->setIncrementId($reservedOrderId) ->setStoreId($storeId) ->setQuoteId($quoteObj) ->setData('created_at',$o_date) ->setGlobal_currency_code($Storecurrencycode) ->setBase_currency_code($Basecurrencycode) ->setStore_currency_code($Storecurrencycode) ->setOrder_currency_code($Storecurrencycode); // set Customer data $order->setCustomer_email($recurringdata['email']) ->setCustomerFirstname($recurringdata['first_name']) ->setCustomerLastname($recurringdata['last_name']) ->setCustomer_is_guest('1'); // set Billing Address if($recurringdata['billingadd'] == 'differentbelling'){ $billingAddress = Mage::getModel('sales/order_address') ->setStoreId($storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) ->setFirstname($recurringdata['first_name']) ->setLastname($recurringdata['last_name']) ->setStreet($recurringdata['address']) ->setCity($recurringdata['city']) ->setCountry_id($recurringdata['country']) ->setRegion($recurringdata['state']) ->setPostcode($recurringdata['zip']) ->setTelephone($recurringdata['phone']); $billingstatelength = strlen($recurringdata['state']); if($billingstatelength == '2'){ $regionmodel = Mage::getModel('directory/region')->loadByCode($recurringdata['state'], $recurringdata['country']); $billingstate_id = $regionmodel->getId(); $billingAddress->setRegion_id($billingstate_id); } $order->setBillingAddress($billingAddress); }else{ $billingAddress = Mage::getModel('sales/order_address') ->setStoreId($storeId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) ->setFirstname($recurringdata['first_name']) ->setLastname($recurringdata['last_name']) ->setStreet($recurringdata['address']) ->setCity($recurringdata['city']) ->setCountry_id($recurringdata['country']) ->setRegion($recurringdata['state']) ->setPostcode($recurringdata['zip']) ->setTelephone($recurringdata['phone']); $billingstatelength = strlen($recurringdata['state']); if($billingstatelength == '2'){ $regionmodel = Mage::getModel('directory/region')->loadByCode($recurringdata['state'], $recurringdata['country']); $billingstate_id = $regionmodel->getId(); $billingAddress->setRegion_id($billingstate_id); } $order->setBillingAddress($billingAddress); } //set Shipping Address $shippingAddress = Mage::getModel('sales/order_address') ->setStoreId($setStoreId) ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) ->setFirstname($recurringdata['first_name']) ->setLastname($recurringdata['last_name']) ->setStreet($recurringdata['address']) ->setCity($recurringdata['city']) ->setCountry_id($recurringdata['country']) ->setRegion($recurringdata['state']) ->setPostcode($recurringdata['zip']) ->setTelephone($recurringdata['phone']); $shippingstatelength = strlen($recurringdata['state']); if($shippingstatelength == '2'){ $regionmodel = Mage::getModel('directory/region')->loadByCode($recurringdata['state'], $recurringdata['country']); $shippingstate_id = $regionmodel->getId(); $shippingAddress->setRegion_id($shippingstate_id); } $order->setShippingAddress($shippingAddress) ->setShipping_method('ups_03') ->setShippingDescription('UPS - UPS Ground') ->setShippingAmount($finalordershipping); $cc_number = $recurringdata['payment']['cc_number']; $orderPayment = Mage::getModel('sales/order_payment') ->setStoreId($storeId) ->setMethod('authnetcim') ->setCcNumber($cc_number) ->setCcType($recurringdata['payment']['cc_type']) ->setCcExpMonth($recurringdata['payment']['cc_exp_month']) ->setCcExpYear($recurringdata['payment']['cc_exp_year']) ->setCcLast4(substr($cc_number,-4)) ->setCcCid($recurringdata['payment']['cc_cid']); $order->setPayment($orderPayment); // add product // 194 Is product id And 178 or 175 is my option id(super_attribute id) $subTotal = 0; $products = array( '194' => array( 'qty' => '1', 'price' => '$32.75', 'super_attribute' => array( 178 => 36, 175 => 13 ) ) ); foreach ($products as $productId=>$product) { $finalorderpri = substr($product['price'], 1); $_product = Mage::getModel('catalog/product')->load($productId); $rowTotal = $finalorderpri * $product['qty']; $orderItem = Mage::getModel('sales/order_item') ->setStoreId($storeId) ->setQuoteItemId(0) ->setQuoteParentItemId(NULL) ->setProductId($productId) ->setProductType($_product->getTypeId()) ->setQtyBackordered(NULL) ->setTotalQtyOrdered($product['qty']) ->setQtyOrdered($product['qty']) ->setName($_product->getName()) ->setSku($_product->getSku()) ->setPrice($finalorderpri) ->setBasePrice($_product->getPrice()) ->setOriginalPrice($_product->getPrice()) ->setRowTotal($rowTotal) ->setBaseRowTotal($rowTotal); $subTotal += $rowTotal; $order->addItem($orderItem); } $finalorderGrandTotal = ($subTotal + $finalordershipping + $finalordertax); $order->setSubtotal($subTotal) ->setBaseSubtotal($subTotal) ->setGrandTotal($finalorderGrandTotal) ->setTaxAmount($finalordertax) ->setBaseGrandTotal($finalorderGrandTotal); $transaction->addObject($order); $transaction->addCommitCallback(array($order, 'place')); $transaction->addCommitCallback(array($order, 'save')); try{ $transaction->save(); $order->save(); }catch(Exception $e){ echo $e->getMessage();exit; }
Please suggest me how super attribute set in configurable product when create order programatically.