create order programatically customer as guest in magento SOAP 11 comments



<?php
<pre><?php
$proxy = new SoapClient('http://yoursite.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;


// Set customer, for example guest
$customerAsGuest = array(
    "firstname" => "Pragnesh",
    "lastname" => "Karia",
    "email" => "guest_pk@pk.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",
        "lastname" => "S Lastname",
        "company" => "S Company",
        "street" => "S Street",
        "city" => "S City",
        "region" => "S Region",
        "postcode" => "testPostcode",
        "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",
        "company" => "B Company",
        "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"];
$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" => "annet-pk 001"
);
$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>
?>

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


11 thoughts on “create order programatically customer as guest in magento SOAP

  • William Mapp

    Hi Prag,

    I’m attempting to programmatically create a new order using this same setup, but each time I call cart_customer.addresses, Magento returns with Customer address data is empty. I can successfully execute cart_customer.set.

    When I dump the parameters passed to cart_customer.addresses, the values are valid, here they are:

    (
    82557895c2f304aa49da1f7f47ff7ea5,
    “cart_customer.addresses”,
    (
    1084,
    {
    city = “Upper Marlboro”;
    company = none;
    “country_id” = US;
    fax = 2405358049;
    firstname = UserFirstname;
    “is_default_billing” = 0;
    “is_default_shipping” = 0;
    lastname = UserLastname;
    mode = shipping;
    postcode = 20774;
    region = MD;
    street = “1000 Street Name Lane”;
    telephone = “(202)555-1212”;
    }
    )
    )

    I’m making my function calls using XMLRPC from iPhone.

    Any ideas?

    Thanks,
    Will Mapp

    • Pragnesh Karia Post author

      Thanks for your interest..

      First of all , i want to know , your customer is GUEST or SITE Customer.?
      if he is GUEST , then you need to work around the code below.

      $customerAsGuest = array(
          "firstname" => "Pragnesh",
          "lastname" => "Karia",
          "email" => "pk@magento.com",
          "website_id" => $website_id,
          "store_id" => $store_id,
          "mode" => "guest"
      );
      

      I have commented out the code , in full code.
      You can do trial and error in it.

      Or other wise send me the Link where you are working.
      If possible i will try to help you at my best.

      Thanks…!!!

      Cheers..!!

  • Will Mapp

    Hi Pragnesh,

    First, thanks for responding.

    Im sending the customer info as a guest.

    Magento returns success when that call is made, yet it still complains about empty address data. I’ll post the dump of the object tomorrow.

  • William Mapp

    Hi Prag,

    I am sending the customer information first.

    Here’s the dump of the object before it gets pushed to Magento:

    (
    197,
    {
    email = “first_last_name@yahoo.com”;
    firstname = First;
    lastname = Last;
    mode = guest;
    “store_id” = 0;
    “website_id” = 0;
    }

    Magento returns this result:

    1

    Then I send this:

    (
    25110809a75788fa4f4a410b95a09fee,
    “cart_customer.addresses”,
    (
    197,
    {
    city = “Upper Marlboro”;
    company = none;
    “country_id” = US;
    fax = 2403450049;
    firstname = First;
    “is_default_billing” = 0;
    “is_default_shipping” = 0;
    lastname = Last;
    mode = shipping;
    postcode = 20774;
    region = MD;
    “region_id” = 94;
    street = “1000 Address Lane”;
    telephone = “(202) 345-1234”;
    }
    )
    )

    And Magento returns this:

    faultCode1051faultStringCustomer address data is empty.

    Let me know what you think.

    Thanks,
    Will

  • William Mapp

    Is it possible to trace the block of code this exception is being thrown from?

    All of the address info is filled in and I just don’t understand this error. Could it be that the properties are incorrect?

  • malek

    thank you for this article, but i have a problem, in fact im developing an iphone application with magento api, i want to add products to cart with the webservice and if the customer validate her commende, he will be redirected to the web site to add information ( name,adress and the others steps) can you tell how if it’s possible to pass the session id and the cart id to the web site ?
    if no, i have to do steps my self, but the step of paiement ( card number.. ) is not described,how can i do it ( i cant have the result of payment ( im not in web page to catch the result ) ), have you a solution for this ?

    thanks allot for help .