create order programatically customer as registered in magento SOAP


After fighting a lot , i have finally found some some solution.

 

yupeee…

 

 


<?php
<pre>


$proxy = new SoapClient('http://hpcstorelocal.com/api/soap/?wsdl');
$sessionId = $proxy->login('pk_admin', 'pk1234567');

echo "<br />sessionId : ".$sessionId;
$store_id = 2;
$website_id = 2;


// Create a quote, get quote identifier
$shoppingCartId = $proxy->call($sessionId, 'cart.create', array($store_id));
echo "<br />shoppingCartId : ".$shoppingCartId;

$customer = array(
    'entity_id' => 224,
    'mode' => 'customer'
);

/*
$customer = array(
    'firstname' => 'Kaory',
    'lastname' => 'Makimura',
    'email' => 'pk@magento.com',
    "website_id" => $website_id,
    "store_id" => $store_id,
    'password' => '4cdf28a5ba761efefe8212962bf3bc8a:lE',
    'mode' => 'register'
);

 */
$resultCustomerSet = $proxy->call($sessionId, 'cart_customer.set', array( $shoppingCartId, $customer, 1) );


//$resultCustomerSet = $proxy->call($sessionId, 'cart_customer.set', array( $shoppingCartId, 223) );
//$resultCustomerSet = $proxy->call($sessionId, 'cart_customer.set', array($shoppingCartId, $customer));
echo "<br />resultCustomerSet : ".$resultCustomerSet;


echo "---------------------";

/*
// Set customer, for example guest
$customerAsGuest = array(
    "firstname" => "Pragnesh",
    "lastname" => "Karia",
    "email" => "pk@magento.com",
    "website_id" => $website_id,
    "store_id" => $store_id,
    "mode" => "guest"
);
$resultCustomerSet = $proxy->call($sessionId, 'cart_customer.set', array($shoppingCartId, $customerAsGuest));
echo "<br />resultCustomerSet : ".$resultCustomerSet;
*/



// Set customer addresses, for example guest's addresses
$arrAddresses = array(
    array(
        "mode" => "shipping",
        "firstname" => "S Firstname ssss",
        "lastname" => "S Lastname",
        "company" => "S Company",
        "street" => "S Street",
        "city" => "S City",
        "region" => "S Region",
        "postcode" => "testPostcode ssss",
        "country_id" => "IN",
        "telephone" => "0123456789",
        "fax" => "0123456789",
        "is_default_shipping" => 0,
        "is_default_billing" => 0
    ),
    array(
        "mode" => "billing",
        "email" => "pk@pk.com",
        "firstname" => "B Firstname",
        "lastname" => "B Lastname  ddddd",
        "company" => "B Companyc ddd",
        "street" => "B Street",
        "city" => "B City",
        "region" => "B Region",
        "postcode" => "B 400078",
        "country_id" => "IN",
        "telephone" => "0123456789",
        "fax" => "0123456789",
        "is_default_shipping" => 0,
        "is_default_billing" => 0
    )
);
$resultCustomerAddresses = $proxy->call($sessionId, "cart_customer.addresses", array($shoppingCartId, $arrAddresses));
echo "<br />resultCustomerAddresses : ".$resultCustomerAddresses;



// add products into shopping cart
$arrProducts = array(
    array(
        "product_id" => "1",
        "qty" => 1
    ),
    array(
         "product_id" => "2",
        "quantity" => 1
    )
);
$resultCartProductAdd = $proxy->call($sessionId, "cart_product.add", array($shoppingCartId, $arrProducts));
echo "<br />resultCartProductAdd : ".$resultCartProductAdd;





/*
Not required as of now
// update product in shopping cart
$arrProducts = array(
    array(
        "product_id" => "1",
        "qty" => 5
    ),
);
$resultCartProductUpdate = $proxy->call($sessionId, "cart_product.update", array($shoppingCartId, $arrProducts));

// remove products from shopping cart, for example by SKU
$arrProducts = array(
    array(
        "sku" => "testSKU"
    ),
);
$resultCartProductRemove = $proxy->call($sessionId, "cart_product.remove", array($shoppingCartId, $arrProducts));
*/




// get list of products
$shoppingCartProducts = $proxy->call($sessionId, "cart_product.list", array($shoppingCartId));
echo "<br />shoppingCartProducts : <br />";
echo '<pre>';
print_r($shoppingCartProducts);
echo '</pre>';


$resultShippingMethods = $proxy->call($sessionId, "cart_shipping.list", array($shoppingCartId));
echo '<pre>';
print_r($resultShippingMethods);
echo '</pre>';

//$randShippingMethodIndex = rand(1, count($resultShippingMethods));
/*
echo '<pre>';
print_r($resultShippingMethods);
echo '</pre>';
echo '<pre>';
print_r($randShippingMethodIndex);
echo '</pre>';
die();
*/

/*
// get list of shipping methods
$resultShippingMethods = $proxy->call($sessionId, "cart_shipping.list", array($shoppingCartId));
echo "<br />1 resultShippingMethods : <br />";
echo '<pre>';
print_r($resultShippingMethods);
echo '</pre>';
// set shipping method
$randShippingMethodIndex = rand(1, count($resultShippingMethods));
$shippingMethod = $resultShippingMethods[$randShippingMethodIndex]["code"];
$resultShippingMethod = $proxy->call($sessionId, "cart_shipping.method", array($shoppingCartId, $shippingMethod));
echo "PRAGS :".$shippingMethod;

// set shipping method
$shippingMethod = array(
    "method" => "flatrate_flatrate"
);

*/


//$shippingMethod = $resultShippingMethods[0]["code"];
/*
 Array
(
    [0] => Array
        (
            [carrier] => freeshipping
            [carrier_title] => Free Shipping
             => freeshipping_freeshipping
            [method] => freeshipping
            [method_description] =>
            [price] => 0.0000
            [error_message] =>
            [method_title] => Free
            [carrierName] => Free Shipping
        )

    [1] => Array
        (
            [carrier] => flatrate
            [carrier_title] => Flat Rate
             => flatrate_flatrate
            [method] => flatrate
            [method_description] =>
            [price] => 2.0000
            [error_message] =>
            [method_title] => Fixed
            [carrierName] => Flat Rate
        )

)
 */

//shipping method CODE
$shippingMethod = 'flatrate_flatrate'; //freeshipping_freeshipping //flatrate_flatrate
$resultShippingMethod = $proxy->call($sessionId, "cart_shipping.method", array($shoppingCartId, $shippingMethod));



/*
// get list of payment methods
$resultPaymentMethods = $proxy->call($sessionId, "cart_payment.list", array($shoppingCartId));

echo "<br />resultPaymentMethods : <br />";
echo '<pre>';
print_r($resultPaymentMethods);
echo '</pre>';
*/


// set payment method
$paymentMethod = array(
    "method" => "purchaseorder",
    "po_number" => "c_o.php PK"
);
$resultPaymentMethod = $proxy->call($sessionId, "cart_payment.method", array($shoppingCartId, $paymentMethod));




/*not required right now
// add coupon
$couponCode = "aCouponCode";
$resultCartCouponRemove = $proxy->call($sessionId, "cart_coupon.add", array($shoppingCartId, $couponCode));

// remove coupon
$resultCartCouponRemove = $proxy->call($sessionId, "cart_coupon.remove", array($shoppingCartId));
*/




// get total prices
$shoppingCartTotals = $proxy->call($sessionId, "cart.totals", array($shoppingCartId));
echo "<br />shoppingCartTotals : <br />";
echo '<pre>';
print_r($shoppingCartTotals);
echo '</pre>';



// get full information about shopping cart
$shoppingCartInfo = $proxy->call($sessionId, "cart.info", array($shoppingCartId));
echo "<br />shoppingCartInfo : <br />";
echo '<pre>';
print_r($shoppingCartInfo);
echo '</pre>';




/*
//not required right now
// get list of licenses
$shoppingCartLicenses = $proxy->call($sessionId, "cart.licenseAgreement", array($shoppingCartId));
echo "<br />shoppingCartLicences : <br />";
echo '<pre>';
print_r($shoppingCartLicences);
echo '</pre>';



// check if license is existed
$licenseForOrderCreation = null;
if (count($shoppingCartLicenses)) {
    $licenseForOrderCreation = array();
    foreach ($shoppingCartLicenses as $license) {
        $licenseForOrderCreation[] = $license['agreement_id'];
    }
}
echo "<br />licenseForOrderCreation : <br />";
echo '<pre>';
print_r($licenseForOrderCreation);
echo '</pre>';

*/


// create order
$resultOrderCreation = $proxy->call($sessionId, "cart.order", array($shoppingCartId, null));

echo "<br />resultOrderCreation : <br />";
echo '<pre>';
print_r($resultOrderCreation);
echo '</pre>';



echo "<h1>Done</h1>";
?></pre>
&nbsp;

 

thanks to : http://www.magentocommerce.com/boards/viewthread/232778/#t331307

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools