Jump to content

I need the function to create the order via Prestashop webservice


andreekit

Recommended Posts

Hi everyone,

 

I am facing just the same problem as described below:

http://stackoverflow.com/questions/18542991/prestashop-webservice-create-order

 

I successfully completed those steps:

1) create Customer (opt.)
2) create Address (opt.)
3) check Products availibility (this is really better...)
4) create Cart with order_rows and with product id's and quantities

 

However, I am not able to complete the last step:

5) Create Order with this

 

I need the well-working function to create the new order. In previous steps, I got all variables required for the order. But I cannot make the code provided by the link work as it must. 

 

I am ready to pay to the master who can fix the code, to make it work well.

If you can do it, just name the price.

ASAP!

 

 

Link to comment
Share on other sites

  • 1 year later...
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);

            $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/customers/?schema=synopsis'));

 

            $customer = array();

            $product = array();

 

            if (!empty(Tools::getValue('c_email')))

                $customer['email'] = Tools::getValue('c_email');

            else

                $customer['email'] = '[email protected]';

 

            $customer['firstname'] = Tools::getValue('fname');

            $customer['lastname'] = Tools::getValue('lname');

            $customer['address1'] = Tools::getValue('c_address');

            $customer['city'] = Tools::getValue('c_city');

            $customer['phone'] = Tools::getValue('c_phone');

 

            $id['country'] = '165';

            $id['lang'] = '1';

            $id['currency'] = '1';

            $id['carrier'] = '3';

 

            $product['quantity'] = Tools::getValue('c_qty');

            $product['id'] = $id_product;

            $product['price'] = Product::getPriceStatic($product['id']);

            $product['name'] = Product::getProductName($product['id']);

 

            $product['total'] = $product['price'] * $product['quantity'];

 

            $xml->customer->firstname = $customer['firstname'];

            $xml->customer->lastname = $customer['lastname'];

            $xml->customer->email = $customer['email'];

            $xml->customer->newsletter = '1';

            $xml->customer->optin = '1';

            $xml->customer->active = '1';

 

 

            $opt = array('resource' => 'customers');

            $opt['postXml'] = $xml->asXML();

            $xml = $webService->add($opt);

 

            // ID of created customer

 

            $id['customer'] = $xml->customer->id;

 

            // CREATE Address

 

            $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/addresses?schema=synopsis'));

 

            $xml->address->id_customer = $id['customer'];

            $xml->address->firstname = $customer['firstname'];

            $xml->address->lastname = $customer['lastname'];

            $xml->address->address1 = $customer['address1'];

            $xml->address->city = $customer['city'];

            $xml->address->phone_mobile = $customer['phone'];

            $xml->address->id_country = $id['country'];

            $xml->address->alias = '-';

 

            $opt = array('resource' => 'addresses');

            $opt['postXml'] = $xml->asXML();

            $xml = $webService->add($opt);

 

            // ID of created address

 

            $id['address'] = $xml->address->id;

 

            // CREATE Cart

 

            $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/carts?schema=blank'));

 

            $xml->cart->id_customer = $id['customer'];

            $xml->cart->id_address_delivery = $id['address'];

            $xml->cart->id_address_invoice = $id['address'];

            $xml->cart->id_currency = $id['currency'];

            $xml->cart->id_lang = $id['lang'];

            $xml->cart->id_carrier = $id['carrier'];

 

            $xml->cart->associations->cart_rows->cart_row->id_product = $product['id'];

            $xml->cart->associations->cart_rows->cart_row->quantity = $product['quantity'];

            if(!empty(Tools::getValue('product_attr')))

                $xml->cart->associations->cart_rows->cart_row->id_product_attribute = Tools::getValue('product_attr');

 

            $opt = array('resource' => 'carts');

            $opt['postXml'] = $xml->asXML();

            $xml = $webService->add($opt);

 

            // ID of created cart

 

            $id['cart'] = $xml->cart->id;

 

            // CREATE Order

 

            $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/orders?schema=blank'));

 

            $xml->order->id_customer = $id['customer'];

            $xml->order->id_address_delivery = $id['address'];

            $xml->order->id_address_invoice = $id['address'];

            $xml->order->id_cart = $id['cart'];

            $xml->order->id_currency = $id['currency'];

            $xml->order->id_lang = $id['lang'];

            $xml->order->id_carrier = 3;

            $xml->order->current_state = 3;

            $xml->order->valid = 0;

            $xml->order->payment = 'Cash on delivery';

            $xml->order->module = 'cashondelivery';

            $xml->order->total_paid = $product['total'];

            $xml->order->total_paid_tax_incl = $product['total'];

            $xml->order->total_paid_tax_excl = $product['total'];

            $xml->order->total_paid_real = '0';

            $xml->order->total_products = $product['total'];

            $xml->order->total_products_wt = $product['total'];

            $xml->order->conversion_rate = '1';

 

 

            $xml->order->associations->order_rows->order_row->product_id = $product['id'];

            $xml->order->associations->order_rows->order_row->product_quantity = $product['quantity'];

 

 

            $opt = array('resource' => 'orders');

            $opt['postXml'] = $xml->asXML();

            $xml = $webService->add($opt);

 

            $id['order'] = $xml->order->id;

            $id['secure_key'] = $xml->order->secure_key;

 

            $xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/order_histories?schema=blank'));

 

            $xml->order_history->id_order = $id['order'];

            $xml->order_history->id_order_state = '3';

 

            $opt = array('resource' => 'order_histories');

            $opt['postXml'] = $xml->asXML();

            $xml = $webService->add($opt);

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

I'm working on a custom REST webservice that receives requests from 2 company apps. To manage the cart I can do it just with the class, but to process an order is a mess doing it programmatically, so I'm trying calling PS native WebService.

I've already a cart with all the needed information, but when I call the webservice for the order I get an error:

 

This call to PrestaShop Web Services failed and returned an HTTP status of 400. That means: Bad Request.

 

 

My questions are:

  • if we've a cart identifier what should send at $xml->order->associations->order_rows or why I need to send the cart content again?
  • how should add multiple order_row elements as order_rows childrens?

I made like the following code, but only the first element is well formed:

$r = 0;
foreach ($source_xml->position as $position)
{
    if (!(int)$position->menge)
        continue;
    $xml->order->associations->order_rows->order_row[$r]->product_id = (int)$position->artikelnummer;
    $xml->order->associations->order_rows->order_row[$r]->product_quantity = (int)$position->menge;
    $r ++;
}

Also made like this, with same result:

$base_row = $xml->order->associations->order_rows->order_row;
$r = 0;
foreach ($source_xml->position as $position)
{
    if (!(int)$position->menge)
        continue;
    $row = $base_row;
    $row->id = $r;
    $row->product_id = (int)$position->artikelnummer;
    $row->product_quantity = (int)$position->menge;
    $row->product_attribute_id = 0;
                        
    $xml->order->associations->order_rows->order_row[$r] = $row;
    $r ++;
}
Edited by gu78 (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...

1- you don't need to add products to order, add products to cart is enough

2- you didnt respect fields names this code worked good for me

 

            $i = 0;
            foreach ($product_list as $product) {
                
                // check if product reference exist in remote host
                $productopt = array('resource' => 'products');
                $productopt['filter']['reference'] = $product['reference'];
                $productxml = $webService->get($productopt);
                if(!$x = $productxml->products[0]->product['id']){
                    Logger::AddLog("Product reference ".$product['reference']." not found on remote host !");
                }
                
                $xml->cart->associations->cart_rows->cart_row[$i]->id_product = $product['id_product'];
                $xml->cart->associations->cart_rows->cart_row[$i]->quantity = $product['quantity'];
                $xml->cart->associations->cart_rows->cart_row[$i]->id_product_attribute = $product['id_product_attribute'];
 
                $i++;
            }
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...