I was able to create an order with a bundle product using the following code
<?php
require_once 'app/Mage.php';
define('MAGENTO_ROOT', getcwd());
$compilerConfig = MAGENTO_ROOT . '/includes/config.php';
if (file_exists($compilerConfig)) {
include $compilerConfig;
}
class JLR_Orders_Create {
public function CreateOrder2() {
try {
$storeObject = Mage::getModel('core/store')->load(4); // Store ID
}
catch(Exeception $ex)
{
echo $ex->getMessage();
}
$cart_api = Mage::getModel('checkout/cart_api');
$quoteId = $cart_api->create($storeObject->getStoreId());
$quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($quoteId);
$product = Mage::getModel('catalog/product');
$productId = $product->getIdBySku('SBTEMP075'); // Bundle Product SKU
$params = array(
'product' => $productId,
'related_product' => null,
'bundle_option' => array(
164 => '3235', // Bundle Options
165 => '3240',
166 => '3241'
),
'bundle_option_qty' => array(
164 => 1, // Bundle Quantities
165 => 1,
166 => 1
),
);
$request = new Varien_Object();
$request->setData($params);
$_value['Email'] = 'ZsdfsXcrtdfert@gmail.com';
$_value['First Name'] = 'Jeffrey';
$_value['Last Name'] = 'Roberts';
$_value['Address'] = 'Biscayne Blvd';
$_value['City'] = 'Miami';
$_value['Post Code'] = '33137';
$_value['Telephone'] = '305-555-1155';
// create customer
$customer = Mage::getModel('customer/customer');
$password = 'stackexchange';
$customer->setWebsiteId(4); // Set Website ID
$customer->loadByEmail($_value['Email']);
$customer->setWebsiteId(4); // Set Website ID AGAIN!
if(!$customer->getId()) {
$customer->setEmail($_value['Email']);
$customer->setFirstname($_value['First Name']);
$customer->setLastname($_value['Last Name']);
$customer->setPassword($password);
$customer->setMode(Mage_Checkout_Model_Cart_Customer_Api::MODE_REGISTER);
}
else
{
$customer->setMode(Mage_Checkout_Model_Cart_Customer_Api::MODE_CUSTOMER);
}
try {
$customer->save();
$customer->setConfirmation(null);
$customer->save();
}
catch (Exception $ex) {
error_log(var_dump($ex));
}
if (! $customer->getId() ){
return $this;
}
$dataShipping = array(
'firstname' => $_value['First Name'],
'lastname' => $_value['Last Name'],
'street' => $_value['Address'],
'city' => $_value['City'],
'region' => '',
'region_id' => '',
'postcode' => $_value['Post Code'],
'country_id' => 'CZ', //todo: un-hardcode this.
'telephone' => $_value['Telephone'],
);
$customerAddress = Mage::getModel('customer/address');
if ($defaultShippingId = $customer->getDefaultShipping()){
$customerAddress->load($defaultShippingId);
} else {
$customerAddress
->setCustomerId($customer->getId())
->setIsDefaultShipping('1')
->setSaveInAddressBook('1')
;
$customer->addAddress($customerAddress);
}
try {
$customerAddress
->addData($dataShipping)
->save()
;
} catch(Exception $e){
Mage::log('Address Save Error::' . $e->getMessage());
}
$customer->save();
$product = new Mage_Catalog_Model_Product();
$product->load($productId);
$quote->addProduct($product, $request);
$quote->setCustomer($customer);
$quote->assignCustomer($customer);
$quote->getShippingAddress()->addData($dataShipping)->setShippingMethod('tablerate_bestway');
$quote->getShippingAddress()->setShippingMethod('tablerate_bestway');
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$quote->setPayment($quote->getPayment()->setMethod('free'));
$quote->collectTotals();
$quote->save();
$convertQuoteObj = Mage::getModel('sales/convert_quote');
$orderObj = $convertQuoteObj->toOrder($quote);
$orderPaymentObj=$convertQuoteObj->paymentToOrderPayment($quote->getPayment());
$orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quote->getBillingAddress()));
$orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quote->getPayment()));
$orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quote->getShippingAddress()));
$qty=1;
foreach ($quote->getShippingAddress()->getAllItems() as $item) {
//@var $item Mage_Sales_Model_Quote_Item
$item->setQty($qty);
$orderItem = $convertQuoteObj->itemToOrderItem($item);
if ($item->getParentItem()) {
$orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
}
$orderObj->addItem($orderItem);
}
$orderObj->setCanShipPartiallyItem(false);
$totalDue=$orderObj->getTotalDue();
echo "<p>total due: $totalDue</p>";
$orderObj->place(); //calls _placePayment
$orderObj->save();
$orderId=$orderObj->getId();
echo "<p>orderId: $orderId</p>";
}
}
Mage::app();
$JLR = new JLR_Orders_Create();
$JLR->CreateOrder2();
I have create new extension which is give the relational diagram of simple products and parent Configurable product.
Step1>: Create Api.php
at app\code\local\Amit\SimpleConfigRelation\Model
this give simple product ids and it reverent variance value and attribute id
and option id and label
code is
<?php
class Amit_SimpleConfigRelation_Model_Api extends Mage_Catalog_Model_Api_Resource
{
public function getRelationship($productId, $storeId = null){
$result = array();
$this->_configProductId =$productId;
$product = $this->_getProduct($productId, $storeId);
$this->_getProduct = Mage::getModel('catalog/product');
// $this->_getProduct->setStoreId($storeId);
$this->_getProduct->load($productId);
if($this->_getProduct->getId()){
$result['dropdata']=$this->alldropdownData();
}
return $result;
}
public function getChildsByParentId()
{
}
public function getVarienPrice()
{
}
public function alldropdownData()
{
$attributes = array();
$options = array();
$currentProduct = $this->_getProduct;
$products = array();
$skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();
/* getting all all simple products */
$allProducts = $this->_getProduct->getTypeInstance(true)
->getUsedProducts(null, $this->_getProduct);
foreach ($allProducts as $product) {
$products[] = $product;
}
/* getting all configurable attribute of that products */
$AllowAttributes=$this->_getProduct->getTypeInstance(true)
->getConfigurableAttributes($this->_getProduct);
$producVarien=array();
foreach ($products as $product) {
$productsData[$product->getId()]=$product->getData();
foreach ($AllowAttributes as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
$attributeValue = $product->getData($productAttribute->getAttributeCode());
/* Simple Product data */
if (!isset($producVarien[$product->getId()])) {
$producVarien[$product->getId()] = array();
}
if (!isset($producVarien[$product->getId()][$productAttributeId])) {
$producVarien[$product->getId()][$productAttributeId] = array();
}
/* getting option text value */
if ($productAttribute->usesSource()) {
$MYabel = $productAttribute->getSource()->getOptionText($attributeValue );
}else{
$MYabel='';
}
$info = array(
'id' => $productAttribute->getId(),
'code' => $productAttribute->getAttributeCode(),
'label' => $attribute->getLabel(),
'optionslabel' => $MYabel
);
$producVarien[$product->getId()][$productAttributeId] = array('value'=>$attributeValue,'all'=>$info );
/* end of simple product data */
if (!isset($options[$productAttributeId])) {
$options[$productAttributeId] = array();
}
if (!isset($options[$productAttributeId][$attributeValue])) {
$options[$productAttributeId][$attributeValue] = array();
}
$options[$productAttributeId][$attributeValue][] = $productId;
}
}
return $producVarien;
}
}
[Result]
Step2: Create api.xml
at app\code\local\Amit\SimpleConfigRelation\etc
for getting their relation by api access
SOAP
$client = new SoapClient('http://yourhost/api/soap/?wsdl');
$session = $client->login('******', '******');
$date = $client->call($session, 'simpleconfigrelation_api.getrelationship',array($configproID));
XML-RPC
$client = new Zend_XmlRpc_Client('http://yourhost/api/xmlrpc/');
$session = $client->call('login', array('******', '******'));
$date=$client->call('call', array($session, 'simpleconfigrelation_api.getrelationship'));
And code of this file
<?xml version="1.0"?>
<config>
<api>
<resources>
<simpleconfigrelation_api translate="title" module="simpleconfigrelation">
<title>Myapi</title>
<acl>simpleconfigrelation/api</acl>
<model>simpleconfigrelation/api</model>
<methods>
<getrelationship translate="title" module="simpleconfigrelation">
<title>Relation Between</title>
<acl>simpleconfigrelation/getrelationship</acl>
</getrelationship>
<getchildsbyparentid translate="title" module="simpleconfigrelation">
<title>Child Product iDs</title>
<acl>simpleconfigrelation/getchildsbyparentid</acl>
</getchildsbyparentid>
<getvarienprice translate="title" module="simpleconfigrelation">
<title>Get Extra price on Varien</title>
<acl>simpleconfigrelation/getvarienprice</acl>
</getvarienprice>
</methods>
</simpleconfigrelation_api>
</resources>
<acl>
<resources>
<simpleconfigrelation translate="title" module="simpleconfigrelation">
<title>SimpleConfigRelation</title>
<sort_order>2000</sort_order>
<getrelationship translate="title" module="simpleconfigrelation">
<title>Relation Between</title>
</getrelationship>
<getchildsbyparentid translate="title" module="simpleconfigrelation">
<title>Child Product iDs</title>
</getchildsbyparentid>
<getvarienprice translate="title" module="simpleconfigrelation">
<title>Get Extra price on Varien</title>
</getvarienprice>
</simpleconfigrelation>
</resources>
</acl>
</api>
</config>
Step3: Create config.xml
at app\code\local\Amit\SimpleConfigRelation\etc
and
code is
<?xml version="1.0"?>
<config>
<modules>
<Amit_SimpleConfigRelation>
<version>0.1.0</version>
</Amit_SimpleConfigRelation>
</modules>
<global>
<helpers>
<simpleconfigrelation>
<class>Amit_SimpleConfigRelation_Helper</class>
</simpleconfigrelation>
</helpers>
<models>
<simpleconfigrelation>
<class>Amit_SimpleConfigRelation_Model</class>
<resourceModel>simpleconfigrelation_mysql4</resourceModel>
</simpleconfigrelation>
</models>
</global>
</config>
Step4:create module file at Amit_SimpleConfigRelation.xml
at app\etc\modules
and code is
<?xml version="1.0"?>
<config>
<modules>
<Amit_SimpleConfigRelation>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Api />
</depends>
<version>0.1.0</version>
</Amit_SimpleConfigRelation>
</modules>
</config>
Study on How can I create a Custom SOAP/XML-RPC v1 & V2 Api in magento?
Best Answer
Background
It is not possible with the out-of-box API alone; documentation seems pretty clear on this as it documents no way of establishing selections, options for a bundle
Aside from that, the lack of an api.xml file in the
Mage_Bundle
package is telling that there is no support. Contrast that with another product type which arrived after 1.0, such as Downloadable, and it is apparent that the lack of support seems to be intentional.So, why would they intentionally omit support here? The core team can probably chime in to that end. My guess is that the number of options available and complexity around designing an API like this just didn't pan out in the cost/benefit calculations.
So, wut do?
As Oleksii pointed out already you will have to extend the Magento API to allow this to happen.
A barebones standalone script which creates a bundle selection/option would look something like this:
So at the most all we would have to do is provide an API interface to the required options. Create a new module with a model that extends the Magento API abstract model, and a method that takes the
$items
and$selectionRawData
as parameters:Remember that
$items
and$selectionRawData
are arrays (examples are listed above in the standalone script code block).Now, create an api.xml file in your
etc
directory of your module with the following contents:That's it.
And to call the new API we use:
Caveats/prerequisites
save
call will choke, throwing exceptionsradio
button type - there are others:dropdown
,checkbox
for instance.$items
array creates a fixed-price bundle item. This may be ignored or may choke for a dynamic price bundle.References:
http://kavinduthundeniya.blogspot.de/2012/11/magento-extention-api-for-bundle.html
https://stackoverflow.com/questions/3108775/programmatically-add-bundle-products-in-magento-using-the-sku-id-of-simple-it