2

I am working on web-services of Magento. I need to show a sold product by the pos application in Magento backend.

  • you will need to give much more information - even when you do I think this will require a custom module – scampbell Jul 27 '13 at 16:45
  • I have to enter information in magento when product sold by third party application POS – ZOE RULE Jul 27 '13 at 17:09
  • what POS application? Does it have the ability to use Magento API? – scampbell Jul 30 '13 at 6:17
  • @scampbell Yes My POS(point of sale) Application uses magento API.I am not getting the way how to add sold product by POS with payment information in magento database.Suppose I purchase item with price 1$ from POS then It will also goes to magento.Thanks please help – ZOE RULE Jul 30 '13 at 7:30
  • Do you need to save the customer or can you customer be the POS? – brentwpeterson Sep 11 '13 at 12:43
14

The best method will be to use the Magento SOAP API v2 with WS-I compliance enabled as this version is compatible with .NET, Java and the like.

What you need to do:

  • Start an API session
  • Create a cart
  • Add the products to the cart
  • Set the customer for the cart
  • Add the billing and shipping address to the customer
  • Set the shipping method
  • Set the payment method
  • Create the order (i.e. "sale")

I'll include code from an example PHP class so that you get an idea how it works. Your programming language may vary but the basics and calls should be the same.

Start an API session

If you didn't do it yet you need to create an API user (see System > Web Services > SOAP/XML-RPC - Roles and System > Web Services > SOAP/XML-RPC - Users).

Use the credentials you entered to authenticate and obtain a session key in your script.

$client = new MagentoSoapClient('http://test02.magentoshops.vm/api/v2_soap?wsdl=1');

$client->login("Admin", "Admin123");

MagentoSoapClient is an example class I wrote for this question. I published it on GitHub so it's easier to follow the code: Magento SOAP v2 WS-I Examples. In the following sections I'll show the main calls (like above) and explain the called methods (like beneath).

public function login($name, $password)
{
    $data = array(
        'username' => $name,
        'apiKey' => $password
    );
    $response = $this->_client->login($data);
    $this->_sessionId = $response->result;
}

When using Magento SOAP API v2 with WS-I compliance enabled, you have to pass the arguments as an object (or associative array in PHP).

$this->_client is the SOAP client instance. login is the API method you have to call.

The response you get from Magento is an object. result contains the actual data.

Log in using the credentials you entered in the backend.

The login method returns a session ID. Keep it. From now on you'll need it for every call to identify yourself.

Documentation: http://www.magentocommerce.com/api/soap/introduction.html

Create a cart

Create a cart (or "quote"). The cart will be transformed into an order in the last step.

$cartId = $client->createCart();

The response is a cart id (or "quote id"). Keep it. You will need it for the following steps.

public function createCart()
{
    $data = array(
        'sessionId' => $this->_sessionId
    );
    $response = $this->_client->shoppingCartCreate($data);
    return $response->result;
}

shoppingCartCreate is the API method you want to call. Please note the session ID that I pass with every call.

Please follow the documentation links for all arguments that you can pass on to Magento. For example I'm omitting the store codes but you may want to create the order in a particular store.

Documentation: http://www.magentocommerce.com/api/soap/checkout/cart/cart.create.html

Add the products to the cart

Let's add a simple product to the cart.

$products = array(
    array(
        'sku' => 'test',
        'qty' => 1
    )
);
$productsAdded = $client->addProductsToCart($cartId, $products);

Yes, it's pretty simple (as long as you don't have options or more complex products). You can add several products in one call.

public function addProductsToCart($cartId, array $products)
{
    $data = array(
        'sessionId' => $this->_sessionId,
        'quoteId' => $cartId,
        'productsData' => $products
    );
    $response = $this->_client->shoppingCartProductAdd($data);
    return $response->result;
}

The called API method is shoppingCartProductAdd. Please note that from now on always we're not only passing the session ID but also the cart ID ("quote ID").

Documentation: http://www.magentocommerce.com/api/soap/checkout/cartProduct/cart_product.add.html

Set the customer for the cart

Now you need to set a customer for the cart.

$customer = array(
    'firstname' => 'John',
    'lastname' => 'Doe',
    'email' => 'posorder@company.com',
    'mode' => 'guest'
);
$customerSet = $client->setCustomerForCart($cartId, $customer);

This is as simple as it gets. Depending on your business needs you may want to use an existing Magento customer or register a new one. For simplicity I checkout as a guest.

public function setCustomerForCart($cartId, array $customer)
{
    $data = array(
        'sessionId' => $this->_sessionId,
        'quoteId' => $cartId,
        'customerData' => $customer
    );
    $response = $this->_client->shoppingCartCustomerSet($data);
    return $response->result;
}

The method is shoppingCartCustomerSet.

Documentation: http://www.magentocommerce.com/api/soap/checkout/cartCustomer/cart_customer.set.html

Add the billing and shipping address to the customer

In the next step, you have to set the billing and shipping address.

$billingAdress = array(
    'mode' => 'billing',
    'firstname' => 'John',
    'lastname' => 'Doe',
    'street' => ' Street 123',
    'city' => 'Vienna',
    'region' => 'WI',
    'postcode' => '1170',
    'country_id' => 'AT',
    'telephone' => '+431234567890',
    'is_default_billing' => 1
);
$shippingAddress = array(
    'mode' => 'shipping',
    'firstname' => 'John',
    'lastname' => 'Doe',
    'street' => ' Street 123',
    'city' => 'Vienna',
    'region' => 'WI',
    'postcode' => '1170',
    'country_id' => 'AT',
    'telephone' => '+431234567890',
    'is_default_shipping' => 1
); 
$addresses = array(
    $billingAdress,
    $shippingAddress
);
$addressesAdded = $client->addAddressesToCart($cartId, $addresses);

Note that you have to provide special "regions" and "country IDs". You may have to retrieve the available countries and available regions from Magento and build a mapping to translate your codes into Magentos codes. Or you provide fixed data if you don't have to care about the addresses.

This one and the following calls return a boolean value which tells you if the action was successful.

public function addAddressesToCart($cartId, array $addresses)
{
    $data = array(
        'sessionId' => $this->_sessionId,
        'quoteId' => $cartId,
        'customerAddressData' => $addresses
    );
    $response = $this->_client->shoppingCartCustomerAddresses($data);
    return $response->result;
}

Again, not much to see here. shoppingCartCustomerAddresses is the API method to go.

Documentation: http://www.magentocommerce.com/api/soap/checkout/cartCustomer/cart_customer.addresses.html

Set the shipping method

Now you have to define the shipping method.

$shippingMethodSet = $client->setShippingMethodForCart($cartId, 'flatrate_flatrate');

I used the default shipping method but you may want to retrieve a list of available shipping methods using shoppingCartShippingList.

public function setShippingMethodForCart($cartId, $shippingMethod)
{
    $data = array(
        'sessionId' => $this->_sessionId,
        'quoteId' => $cartId,
        'shippingMethod' => $shippingMethod
    );
    $response = $this->_client->shoppingCartShippingMethod($data);
    return $response->result;
}

The shipping method is set using the API method shoppingCartShippingMethod.

Documentation: http://www.magentocommerce.com/api/soap/checkout/cartShipping/cart_shipping.method.html

Set the payment method

Now we're doing the same for the payment method.

$paymentMethodSet = $client->setPaymentMethodForCart($cartId, 'checkmo');

Again, this is a default payment method ("check money order"). You can retrieve the list of available payment methods using shoppingCartPaymentList.

public function setPaymentMethodForCart($cartId, $paymentMethod)
{
    $data = array(
        'sessionId' => $this->_sessionId,
        'quoteId' => $cartId,
        'paymentData' => array(
            'method' => $paymentMethod
        )
    );
    $response = $this->_client->shoppingCartPaymentMethod($data);
    return $response->result;
}

As you can see the API method is shoppingCartPaymentMethod. Have a look at the documentation for additional data you can pass here.

Documentation: http://www.magentocommerce.com/api/soap/checkout/cartPayment/cart_payment.method.html

Create the order

Finally! We can create the order.

$orderId = $client->createOrderFromCart($cartId);

The response is the "increment order ID" by Magento. If everything went well you will see an order in your Magento backend now. You can use the other calls mentioned in the documentation to get more information on the order.

public function createOrderFromCart($cartId)
{
    $data = array(
        'sessionId' => $this->_sessionId,
        'quoteId' => $cartId
    );
    $response = $this->_client->shoppingCartOrder($data);
    return $response->result;
}

Our last API call is shoppingCartOrder.

Documentation: http://www.magentocommerce.com/api/soap/checkout/cart/cart.order.html

Conclusion

As long as you don't become a victim of bugs, the Magento API is not too difficult to handle (once you get a grip on it).

Be sure to activate the WSDL cache in the backend as calls can take a long time otherwise. With all caches enabled, the calls should be fast (excluding the last one to create the order, this may take a few seconds).

If you find that Magento is blocking your POS application you may have to implement a queue and create the orders asynchronously.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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