rafapas22 Posted February 23, 2017 Share Posted February 23, 2017 Please I need help, I am creating order with web service and all my orders are created with errors, why? This is my code: <?php /* * * 1) create Customer (opt.) * 2) create Address (opt.) * 3) create Cart with order_rows and with product id's and quantities * 4) create the Order * */ require_once dirname(__FILE__).'/config/config.inc.php'; //////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// ENTRY DATA - VARIABLES ////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// //** CUSTOMERS **// // Required $passwd = 'hhee'; $lastname = 'bielsa'; $firstname = 'marcelo'; $email = '[email protected]'; // Others $id_lang = $id_default_group = $id_gender = $id_shop_customer = $id_shop_group_customer = '1'; $date_now = date('Y-m-d H:i:s'); //** END CUSTOMERS **// //** ADDRESS **// // Required $id_country = '6'; //España $city = 'Narnia'; $address1 = 'Calle Falsa 123'; // Others $phone_mobile = '666666666'; $postcode = '30740'; //** END ADDRESS **// //** CARTS **// // Required $id_currency = '1'; $products = array( array( 'id_product' => '1', 'id_product_attribute' => '1', 'quantity' => '2', 'name' => 'pantalones cortos vaqueros', 'reference' => 'inventado 1', 'product_price' => '119.95' ), array( 'id_product' => '2', 'id_product_attribute' => '1', 'quantity' => '4', 'name' => 'traje negro de vieja', 'reference' => 'inventado 2', 'product_price' => '111.95' ) ); // Others $id_carrier = '1'; //** END CARTS **// //** ORDERS **// // Required $order_module = 'bankwire'; //Nombre del módulo de Forma de pago $order_payment = 'Bank wire'; // Forma de pago para el Frontend $total_paid = $total_paid_real = '123.45'; $total_products = $total_products_wt = '119.95'; //Supongo que será precio sin envio y tal, solo de los productos // Others $id_status = '3'; //Estado del pedido $total_discounts = $total_discounts_tax_incl = $total_discounts_tax_excl = '0.00'; $total_paid_tax_incl = $total_paid_tax_excl = '123.45'; $total_shipping = $total_shipping_tax_incl = $total_shipping_tax_excl = '3.50'; //** END ORDERS **// //////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// ENTRY DATA - VARIABLES ////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// /* Loading Class Request*/ $classes_to_load = array( 'ConfigurationWS', 'CustomersWS', 'AddressWS' ); foreach ($classes_to_load as $classname) { if (file_exists(dirname(__FILE__).'/'.$classname.'.php')) { require_once dirname(__FILE__).'/'.$classname.'.php'; } } $configuration = new ConfigurationWS(); $customer = new CustomersWS(); $address = new AddressWS(); if ($customer->exists($email)){ $data_customer = $customer->getDataCustomer($email); $id_customer = $data_customer['id_customer']; $lastname = $data_customer['lastname']; $firstname = $data_customer['firstname']; $id_lang = $data_customer['id_lang']; $id_default_group = $data_customer['id_default_group']; $id_gender = $data_customer['id_gender']; $id_shop_customer = $data_customer['id_shop']; $id_shop_group_customer = $data_customer['id_shop_group']; } else { // Dejamos los valores por defecto $id_customer = 0; } if ($address->exists($address1,$id_customer)){ $data_address = $address->getDataAdress($address1,$id_customer); $id_address = $data_address['id_address']; $id_country = $data_address['id_country']; $city = $data_address['city']; $address1 = $data_address['address1']; $phone_mobile = $data_address['phone_mobile']; $postcode = $data_address['postcode']; } else { // Dejamos los valores por defecto $id_address = 0; } $id_cart = 0; //Quizás se pueda recuperar un carrito pero ahora mismo.... try { $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); /* * 1. Create new customer */ if($id_customer == 0){ // Getting the empty XML document to send back completed $xml = $webService->get( array( 'url' => PS_SHOP_PATH .'api/customers?schema=blank' ) ); // Adding dinamic values // Required $xml->customer->passwd = $passwd; $xml->customer->lastname = $lastname; $xml->customer->firstname = $firstname; $xml->customer->email = $email; // Others $xml->customer->id_lang = $id_lang; $xml->customer->id_shop = $id_shop_customer; $xml->customer->id_shop_group = $id_shop_group_customer; $xml->customer->id_default_group = $id_default_group; // Customers $xml->customer->active = 1; $xml->customer->newsletter = 0; $xml->customer->newsletter_date_add = $date_now; $xml->customer->last_passwd_gen = $date_now; $xml->customer->date_add = $date_now; $xml->customer->date_upd = $date_now; $xml->customer->id_gender = $id_gender; $xml->customer->associations->groups->group[0]->id = $id_default_group; // customers // Adding the new customer $opt = array( 'resource' => 'customers' ); $opt['postXml'] = $xml->asXML(); $xml = $webService->add( $opt ); $id_customer = $xml->customer->id; } /* * 2. Create an address */ if($id_address == 0){ // Getting the empty XML document to send back completed $xml = $webService->get( array( 'url' => PS_SHOP_PATH .'api/addresses?schema=blank' ) ); // Adding dinamic and mandatory fields // Required $xml->address->id_customer = $id_customer; $xml->address->id_country = $id_country; $xml->address->alias = $firstname.' '.$lastname.'\'alias'; $xml->address->lastname = $lastname; $xml->address->firstname = $firstname; $xml->address->city = $city; $xml->address->address1 = $address1; // Others $xml->address->phone_mobile = $phone_mobile; $xml->address->postcode = $postcode; $xml->address->date_add = $date_now; $xml->address->date_upd = $date_now; // Adding the new Customer's Addresss $opt = array( 'resource' => 'addresses' ); $opt['postXml'] = $xml->asXML(); $xml = $webService->add( $opt ); $id_address = $xml->address->id; } /* * 3. Create new cart * */ if($id_cart == 0){ // Getting the empty XML document to send back completed $xml = $webService->get( array( 'url' => PS_SHOP_PATH .'api/carts?schema=blank' ) ); // Adding dinamic and mandatory fields // Required $xml->cart->id_currency = $id_currency; $xml->cart->id_lang = $id_lang; $xml->cart->associations->cart_rows->cart_row[0]->id_product = $products[0]['id_product']; $xml->cart->associations->cart_rows->cart_row[0]->id_product_attribute = $products[0]['id_product_attribute']; $xml->cart->associations->cart_rows->cart_row[0]->id_address_delivery = $id_address; $xml->cart->associations->cart_rows->cart_row[0]->quantity = $products[0]['quantity']; // Others $xml->cart->id_address_delivery = $id_address; $xml->cart->id_address_invoice = $id_address; $xml->cart->id_customer = $id_customer; $xml->cart->carrier = $id_carrier; $xml->cart->date_add = $date_now; $xml->cart->date_upd = $date_now; // Adding the new customer's cart $opt = array( 'resource' => 'carts' ); $opt['postXml'] = $xml->asXML(); $xml = $webService->add( $opt ); $id_cart = $xml->cart->id; } /* * 4. Create the order * */ // Getting the structure of an order $xml = $webService->get(array('url' => PS_SHOP_PATH .'api/orders/?schema=blank')); // Adding dinamic and required fields // Required $xml->order->id_address_delivery = $id_address; // Customer 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_customer = $id_customer; $xml->order->id_carrier = $id_carrier; $xml->order->module = $order_module; $xml->order->payment = $order_payment; $xml->order->total_paid = $total_paid; $xml->order->total_paid_real = $total_paid_real; $xml->order->total_products = $total_products; $xml->order->total_products_wt = $total_products_wt; $xml->order->conversion_rate = 1; // Others $xml->order->valid = 0; $xml->order->current_state = $id_status; $xml->order->total_paid_tax_incl = $total_paid_tax_incl; $xml->order->total_paid_tax_excl = $total_paid_tax_excl; $xml->order->total_shipping = $total_shipping; $xml->order->total_shipping_tax_incl = $total_shipping_tax_incl; $xml->order->total_shipping_tax_excl = $total_shipping_tax_excl; // Order Row. Required $xml->order->associations->order_rows->order_row[0]->product_id = $products[0]['id_product']; $xml->order->associations->order_rows->order_row[0]->product_attribute_id = $products[0]['id_product_attribute']; $xml->order->associations->order_rows->order_row[0]->product_quantity = $products[0]['quantity']; // Order Row. Others $xml->order->associations->order_rows->order_row[0]->product_name = $products[0]['name']; $xml->order->associations->order_rows->order_row[0]->product_reference = $products[0]['reference']; $xml->order->associations->order_rows->order_row[0]->product_price = $products[0]['product_price']; $xml->order->associations->order_rows->order_row[0]->unit_price_tax_incl = $products[0]['product_price']; $xml->order->associations->order_rows->order_row[0]->unit_price_tax_excl = $products[0]['product_price']; // Creating the order $opt = array( 'resource' => 'orders' ); $opt['postXml'] = $xml->asXML(); $xml = $webService->add( $opt ); $id_order = $xml->order->id; echo "Customer: ".$id_customer." address: ".$id_address." cart: ".$id_cart." Order: .".$id_order; } catch (PrestaShopWebserviceException $e) { // Here we are dealing with errors $trace = $e->getTrace(); if ($trace[0]['args'][0] == 404) echo 'Bad ID'; else if ($trace[0]['args'][0] == 401) echo 'Bad auth key'; else echo 'Other error<br />'.$e->getMessage(); } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now