1

Could you tell me how can I create Order using magento v2_soap api?

1
  • 1
    Welcome to Magento.SE! This question is too vague or the answer would be too long for the format of this site. I'm glad that you're here and you're interested but we typically ask for more specific questions or for you to show what you have tried up to this point. In general, be more specific.
    – philwinkle
    Aug 6, 2013 at 20:16

2 Answers 2

8

You'll want to use the methods in the cart hierarchy to create a cart, add items to it, and then user the order method.

Here's the sample code the from API docs.

/**
 * Example of order creation
 * Preconditionsare as follows:
 * 1. Create a customer
 * 2. Сreate a simple product */

$user = 'apiUser';
$password = 'apiKey';
    $proxy = new SoapClient('http://magentohost/api/v2_soap/?wsdl');
    $sessionId = $proxy->login($user, $password);
    $cartId = $proxy->shoppingCartCreate($sessionId, 1);
    // load the customer list and select the first customer from the list
    $customerList = $proxy->customerCustomerList($sessionId, array());
    $customer = (array) $customerList[0];
    $customer['mode'] = 'customer';
    $proxy->shoppingCartCustomerSet($sessionId, $cartId, $customer);
    // load the product list and select the first product from the list
    $productList = $proxy->catalogProductList($sessionId);
    $product = (array) $productList[0];
    $product['qty'] = 1;
    $proxy->shoppingCartProductAdd($sessionId, $cartId, array($product));

    $address = array(
        array(
            'mode' => 'shipping',
            'firstname' => $customer['firstname'],
            'lastname' => $customer['lastname'],
            'street' => 'street address',
            'city' => 'city',
            'region' => 'region',
            'telephone' => 'phone number',
            'postcode' => 'postcode',
            'country_id' => 'country ID',
            'is_default_shipping' => 0,
            'is_default_billing' => 0
        ),
        array(
            'mode' => 'billing',
            'firstname' => $customer['firstname'],
            'lastname' => $customer['lastname'],
            'street' => 'street address',
            'city' => 'city',
            'region' => 'region',
            'telephone' => 'phone number',
            'postcode' => 'postcode',
            'country_id' => 'country ID',
            'is_default_shipping' => 0,
            'is_default_billing' => 0
        ),
    );
     // add customer address
    $proxy->shoppingCartCustomerAddresses($sessionId, $cartId, $address);
    // add shipping method
    $proxy->shoppingCartShippingMethod($sessionId, $cartId, 'flatrate_flatrate');

    $paymentMethod =  array(
        'po_number' => null,
        'method' => 'checkmo',
        'cc_cid' => null,
        'cc_owner' => null,
        'cc_number' => null,
        'cc_type' => null,
        'cc_exp_year' => null,
        'cc_exp_month' => null
    );
     // add payment method
    $proxy->shoppingCartPaymentMethod($sessionId, $cartId, $paymentMethod);
     // place the order
    $orderId = $proxy->shoppingCartOrder($sessionId, $cartId, null, null);
0
1

By default Magento does not allow you to create orders through the API.
Take a look in app/code/code/Mage/Sales/etc/wsdl.xml inside the tag <portType> and see what methods are supported for the Sales module.
In general you can only retrieve lists or individual objects, or add comments to them. For the an order you can change the status.
You are allowed to create shipments, invoices or credit memos, but not orders.
To be able to do it, you need to extend the wsdl and add your class with methods for creating an order.
No offense, but this might prove to be a big task for a beginner (even for someone with a little experience also).
If you want to give it a go read first how to extend the web service and how to override models in magento.
You can also take a look at this. It's a workaround on creating an order by going through the checkout process using the API. (I haven't tested it).
[EDIT]
I will leave this answer here as it serves as general knowledge, but I highly approve of Alan's answer. He really know what's his talking about. :)

6
  • 1
    This is good generate information, but the API supports (in newer Magento versions) creating orders by building up a cart object, adding items, and then using the cart's "order" method (maps to createOrder in the class) See Mage/Checkout/etc/api.xml for the definition. Aug 12, 2013 at 1:07
  • @AlanStorm Have you any idea of this error "Credit card number mismatch with credit card type." while creating an order with API
    – hardik
    Apr 7, 2014 at 16:37
  • @hardik Sounds like your number is failing the validate method in app/code/core/Mage/Payment/Model/Method/Cc.php Apr 7, 2014 at 16:40
  • @AlanStorm I have tested with my 3 different VISA cards and it still shows that same error.
    – hardik
    Apr 7, 2014 at 16:54
  • @AlanStorm magento.stackexchange.com/questions/17980/… here is my question.
    – hardik
    Apr 7, 2014 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.