eCommerce

How to Create an Order in Magento using SOAP API from Shopify Website Order

  By     |   July 6th, 2017

Recently I have a requirement .That client has 2 websites which is running on both Magento and Shopify having the same products and same quantity . The requirement was need to place an order in Magento website when an order received in shopify website using Magento API. I have done this requirement using Magento SOAP V2 API.

First you need to create an API user in Magento. This API user is also related to a so-called API role. By default, there is neither API user nor API role, so you need to configure both.

Logging into the Magento Admin Panel

First an API role needs to be added. Login to the Magento Admin Panel and navigate to System > Web Services > Roles.

Create a new API role

This page lists the current API roles. If there none listed yet, click on Add New Role.

Every role needs a name. This can be anything. Just make sure it is descriptive enough for yourself.

The second tab on the left (Role Resources) allows you to select which resources should be available for users with this role. While you can select exactly which privileges are given, we recommend you select All unless you’re willing to spend hours with experimenting.

Create a new API user

Now that the role is created, you can add an user that makes use of this role.

The strange thing here is that the user fields here seem to indicate a person or individual, while in fact we’re creating a system account of some kind. The fields First Name, Last Name and Email just need to be filled in with any value you like. Make sure the User Name and Api Key are secure enough.

The second tab on the left is called User Role, and that’s where you select the role earlier created

 

<?php
//get recent orders from shopify website using json, You can specify the udpated date using query string.
$data = file_get_contents("https://shopify-website.myshopify.com/admin/orders.json");
//?updated_at_min=2017-06-25T01:45:27-11:00
$data = json_decode($data, TRUE);
//establishing connection using SOAP api
$proxy     = new SoapClient('https://magentodomain/api/v2_soap/?wsdl');
$username  = 'adarshe'; //API username
$password  = '5d41402abc4b2a76b'; //API Key
$sessionId = $proxy->login($username, $password);
$cartId    = $proxy->shoppingCartCreate($sessionId, 1);
//shopify orders through loop
foreach ($data['orders'] as $dat):
    
//customer info
    $email        = $dat['customer']['email'];
    $created_at   = $dat['customer']['created_at'];
    $updated_at   = $dat['customer']['updated_at'];
    $first_name   = $dat['customer']['first_name'];
    $last_name    = $dat['customer']['last_name'];
    $orders_count = $dat['customer']['orders_count'];
    $total_spent  = $dat['customer']['total_spent'];
    $phone        = $dat['customer']['phone'];
//customer address info
    $first_name   = $dat['customer']['default_address']['first_name'];
    $last_name    = $dat['customer']['default_address']['last_name'];
    $company      = $dat['customer']['default_address']['company'];
    $address1     = $dat['customer']['default_address']['address1'];
    $address2     = $dat['customer']['default_address']['address2'];
    $city         = $dat['customer']['default_address']['city'];
    $province     = $dat['customer']['default_address']['province'];
    $country      = $dat['customer']['default_address']['country'];
    $zip          = $dat['customer']['default_address']['zip'];
    $phone        = $dat['customer']['default_address']['phone'];
    $country_code = $dat['customer']['default_address']['country_code'];
    $country_name = $dat['customer']['default_address']['country_name'];
  
  //Create customer if not exist in magnento
    $complexFilter = array(
        'complex_filter' => array(
            array(
                'key' => 'email',
                'value' => array(
                    'key' => 'in',
                    'value' => $email
                )
            )
        )
    );
  
    $result= $proxy->customerCustomerList($sessionId, $complexFilter);
    if (count($result) == 0):
        try {
           $customer_id = $proxy->customerCustomerCreate($sessionId, array(
                'email' => $email,
                'firstname' => $first_name,
                'lastname' => $last_name,
                'password' => '111111',
                'website_id' => 1,
                'store_id' => 1,
                'group_id' => 1
            ));
            $created_at    = date("Y-m-d m:m:s");
            $updated_at    = date("Y-m-d m:m:s");
            $password_hash = md5('111111');
            $group_id      = 1;
        }
        catch (SoapFault $e) {
            $result = $e->getMessage();
        }
 
    else:
       
        foreach ($result as $res):
            $customer_id   = $res->customer_id;
            $created_at    = $res->created_at;
            $updated_at    = $res->updated_at;
            $password_hash = $res->password_hash;
            $group_id      = $res->group_id;
        endforeach;
    endif;
  
    //set customer info for create order
    $customer['customer_id']   = $customer_id; // customer id
    $customer['created_at']    = $created_at;
    $customer['updated_at']    = $updated_at;
    $customer['store_id']      = 1;
    $customer['website_id']    = 1;
    $customer['created_in']    = 'Default Store View';
    $customer['email']         = $email;
    $customer['firstname']     = $first_name;
    $customer['lastname']      = $last_name;
    $customer['group_id']      = $group_id;
    $customer['password_hash'] = $password_hash;
    $customer['mode']          = 'customer';
    $proxy->shoppingCartCustomerSet($sessionId, $cartId, $customer);
    //end
  
    //order info
     
    $email_id         = $dat['email'];
    $created_at       = $dat['created_at'];
    $updated_at       = $dat['updated_at'];
    $order_number     = $dat['number'];
    $payment_method   = $dat['gateway'];
    $total_price      = $dat['total_price'];
    $subtotal_price   = $dat['subtotal_price'];
    $financial_status = $dat['financial_status'];
    $total_tax        = $dat['total_tax'];
    $currency         = $dat['currency'];
    $financial_status = $dat['financial_status'];
    $total_discounts  = $dat['total_discounts'];
    $email_id         = $dat['email'];
  
    //find the sku of corresponding products in Magento using shopify SKU
    foreach ($dat['line_items'] as $items):
        $complexFilter = array(
            'complex_filter' => array(
                array(
                    'key' => 'sku',
                    'value' => array(
                        'key' => 'in',
                        'value' => $items['sku']
                    )
                )
            )
        );
        $shop_quanty = $items['quantity'];
        $result = $proxy->catalogProductList($sessionId, $complexFilter);
        $products = array();
        foreach ($result as $res):
            $product_id  = $res->product_id;
            $product_sku = $res->sku;
            $products[] = array(
                'product_id' => $res->product_id,
                'sku' => $res->sku,
                'qty' => $shop_quanty
            );
        endforeach; 
    endforeach;
  
    foreach ($dat['shipping_lines'] as $shipping):
    $price = $shipping['price'];
        $title = $shipping['title'];
        $code  = $shipping['code'];
    endforeach;
    //billing address from shopify order
    $first_name = $dat['billing_address']['first_name'];
    $last_name    = $dat['billing_address']['last_name'];
    $address1     = $dat['billing_address']['address1'];
    $phone        = $dat['billing_address']['phone'];
    $city         = $dat['billing_address']['city'];
    $province     = $dat['billing_address']['province'];
    $country      = $dat['billing_address']['country'];
    $address2     = $dat['billing_address']['address2'];
    $country_code = $dat['billing_address']['country_code'];
    $zip          = $dat['billing_address']['zip'];
    //shipping address from shopify order
    $first_name = $dat['shipping_address']['first_name'];
    $last_name    = $dat['shipping_address']['last_name'];
    $address1     = $dat['shipping_address']['address1'];
    $phone        = $dat['shipping_address']['phone'];
    $city         = $dat['shipping_address']['city'];
    $province     = $dat['shipping_address']['province'];
    $country      = $dat['shipping_address']['country'];
    $address2     = $dat['shipping_address']['address2'];
    $country_code = $dat['shipping_address']['country_code'];
    $zip          = $dat['billing_address']['zip'];
    
    
    try {
        $proxy->shoppingCartProductAdd($sessionId, $cartId, $products);
    }
    catch (SoapFault $e) {
        $error['product'] = $e->getMessage();
    }
    
    $address = array(
        
        array(
            'mode' => 'shipping',
            'firstname' => $first_name,
            'lastname' => $last_name,
            'street' => 'streetname',
            'city' => $city,
            'region_id' => 1,
            'region' => 'region',
            'telephone' => $phone,
            'postcode' => $zip,
            'country_id' => $country_code,
            'is_default_shipping' => 0,
            'is_default_billing' => 0,
            'customer_address_id' => 1,
            'customer_id' => $customer_id 
        ),
        
        array(
            'mode' => 'billing',
            'firstname' => $first_name,
            'lastname' => $last_name,
            'street' => 'streetname',
            'city' => $city,
            'region_id' => 1,
            'region' => 'region',
            'telephone' => $phone,
            'postcode' => $zip,
            'country_id' => $country_code,
            'is_default_shipping' => 0,
            'is_default_billing' => 0,
            'customer_address_id' => 1,
            'customer_id' => $customer_id 
        )
        
    );
    
    
    // add customer address
    try {
        $result = $proxy->customerCustomerInfo($sessionId, '2');
    }
    catch (SoapFault $e) {
        $error['shipping'] = $e->getMessage();
    }
    
    // add customer address
    try {
        $proxy->shoppingCartCustomerAddresses($sessionId, $cartId, $address);
    }
    catch (SoapFault $e) {
        $error['shipping'] = $e->getMessage();
    }
    
    try {
        // add shipping method
        $proxy->shoppingCartShippingMethod($sessionId, $cartId, 'freeshipping_freeshipping');
    }
    catch (SoapFault $e) {
        $result = $e->getMessage();
    }
    
    
    // add payment method   
    
    //enter payment method code here
    $paymentMethod = array(
        'method' => 'cashondelivery'
    );
    $proxy->shoppingCartPaymentMethod($sessionId, $cartId, $paymentMethod);
    
    // place the order
    $orderId = $proxy->shoppingCartOrder($sessionId, $cartId, null, null);
  //add comment in comment history of order
    $result  = $proxy->salesOrderAddComment($sessionId, $orderId, 'pending', 'Added From Shopify Website');
    echo $orderId;

endforeach;
//end shopify orders through loop
?>

 

Want to build your own eCommerce or enhance your eCommerce website features ?

Contact us now for free consultation

Contact Us
RECENT COMMENTS