Otra vez, de nuevo, me repito más que el ajo xDD vengo a traer otro CODE-KATA o HOWTO. Esta vez sobre la API Soap de Magento. Se trata de un pequeño script para ver la información de un pedido.
El punto de partida es que tenemos ya configurada la API Soap de Magento, con un usuario de la API, con los suficientes privilegios para hacer peticiones de información sobre los pedidos. Entonces conectamos a dicho proyecto, a su endpoint, con las credenciales de acceso, y obtenemos los datos del pedido.
Vamos al grano..
Conectando a la API Soap de Magento
Esto es muy sencillo, es más sencillo que con la API REST, así que el inicio de la conexión en PHP queda de la forma siguiente:
$user = 'apiuser'; $password = 'apikey'; $endpoint = 'http://www.tusitio.com/api/v2_soap/?wsdl'; // Create Soap client. $ret = 'Connecting.. '; try { $proxy = new \SoapClient($endpoint); $ret .= 'ok!<br>'; } catch (Exception $e) { $ret .= $e->getMessage().'<br>'; } // Do login. try { $sessionId = $proxy->login($user, $password); $ret .= 'Logged in, session ID: '.$sessionId.'<br>'; } catch (Exception $e) { $ret .= $e->getMessage().'<br>'; }
Si todo ha ido bien, hasta aquí ya tenemos una sessión de usuario conectado al Magento en cuestión.
Obteniendo la información del pedido
Para esto sólo nos queda añadir el siguiente código en PHP:
try { $result = $proxy->salesOrderInfo($sessionId, $salesOrderIncrementId); $ret .= 'Data updated!<br>'; } catch (Exception $e) { $ret .= $e->getMessage().'<br>'; } ob_start(); var_dump($result); $ret .= ob_get_clean();
Debemos de ver algo parecido a esto por pantalla:
object(stdClass)[876] public 'increment_id' => string 'P00123123' (length=9) public 'store_id' => string '6' (length=1) public 'created_at' => string '2018-09-28 11:40:06' (length=19) public 'updated_at' => string '2018-09-28 11:40:08' (length=19) public 'customer_id' => string '87156' (length=5) public 'tax_amount' => string '9.3700' (length=6) public 'shipping_amount' => string '0.0000' (length=6) public 'discount_amount' => string '0.0000' (length=6) public 'subtotal' => string '44.6400' (length=7) public 'grand_total' => string '54.0100' (length=7) public 'total_qty_ordered' => string '5.0000' (length=6) public 'base_tax_amount' => string '9.3700' (length=6) public 'base_shipping_amount' => string '0.0000' (length=6) public 'base_discount_amount' => string '0.0000' (length=6) public 'base_subtotal' => string '44.6400' (length=7) public 'base_grand_total' => string '54.0100' (length=7) public 'billing_address_id' => string '309444' (length=6) public 'shipping_address_id' => string '309445' (length=6) public 'store_to_base_rate' => string '1.0000' (length=6) public 'store_to_order_rate' => string '1.0000' (length=6) public 'base_to_global_rate' => string '1.0000' (length=6) public 'base_to_order_rate' => string '1.0000' (length=6) public 'weight' => string '1.0000' (length=6) public 'store_name' => string 'B2B B2B store B2B Español' (length=26) public 'status' => string 'pendtrans' (length=9) public 'state' => string 'new' (length=3) public 'global_currency_code' => string 'EUR' (length=3) public 'base_currency_code' => string 'EUR' (length=3) public 'store_currency_code' => string 'EUR' (length=3) public 'order_currency_code' => string 'EUR' (length=3) public 'shipping_method' => string 'freeshipping_freeshipping' (length=25) public 'shipping_description' => string 'Método 2: Recogida en tienda' (length=102) public 'customer_email' => string 'user1@tusitio.com' (length=22) public 'customer_firstname' => string 'user1' (length=5) public 'customer_lastname' => string 'test' (length=4) public 'quote_id' => string '324875' (length=6) public 'is_virtual' => string '0' (length=1) public 'customer_group_id' => string '36' (length=2) public 'customer_note_notify' => string '1' (length=1) public 'customer_is_guest' => string '0' (length=1) public 'order_id' => string '154957' (length=6) public 'shipping_address' => object(stdClass)[871] public 'parent_id' => string '154957' (length=6) public 'address_type' => string 'shipping' (length=8) public 'firstname' => string 'Shipping firstname 1 0' (length=22) public 'lastname' => string 'Shipping lastname 1 0' (length=21) public 'street' => string 'Calle de pruebas 1 0' (length=20) public 'city' => string 'Alicante' (length=8) public 'region' => string 'Alicante' (length=8) public 'postcode' => string '03003' (length=5) public 'country_id' => string 'ES' (length=2) public 'telephone' => string '123123123' (length=9) public 'region_id' => string '133' (length=3) public 'address_id' => string '309445' (length=6) public 'billing_address' => object(stdClass)[878] public 'parent_id' => string '154957' (length=6) public 'address_type' => string 'billing' (length=7) public 'firstname' => string 'Shipping firstname 1 0' (length=22) public 'lastname' => string 'Shipping lastname 1 0' (length=21) public 'company' => string 'user1 test' (length=10) public 'street' => string 'CL test test' (length=12) public 'city' => string 'Alicante' (length=8) public 'region' => string 'Alicante' (length=8) public 'postcode' => string '03003' (length=5) public 'country_id' => string 'ES' (length=2) public 'telephone' => string '123123123' (length=9) public 'region_id' => string '133' (length=3) public 'address_id' => string '309444' (length=6) public 'items' => array (size=2) 0 => object(stdClass)[879] public 'item_id' => string '188721' (length=6) public 'order_id' => string '154957' (length=6) public 'quote_item_id' => string '413109' (length=6) public 'created_at' => string '2018-09-28 11:40:06' (length=19) public 'updated_at' => string '2018-09-28 11:40:08' (length=19) public 'product_id' => string '11155' (length=5) public 'product_type' => string 'simple' (length=6) public 'product_options' => string 'a:1:{s:15:"info_buyRequest";a:2:{s:3:"sku";s:3:"123";s:3:"qty";d:2;}}' (length=69) public 'weight' => string '0.2000' (length=6) public 'is_virtual' => string '0' (length=1) public 'sku' => string '123' (length=3) public 'name' => string 'Nombre de producto 1' (length=51) public 'free_shipping' => string '0' (length=1) public 'is_qty_decimal' => string '0' (length=1) public 'no_discount' => string '0' (length=1) public 'qty_canceled' => string '0.0000' (length=6) public 'qty_invoiced' => string '0.0000' (length=6) public 'qty_ordered' => string '2.0000' (length=6) public 'qty_refunded' => string '0.0000' (length=6) public 'qty_shipped' => string '0.0000' (length=6) public 'price' => string '7.4400' (length=6) public 'base_price' => string '7.4400' (length=6) public 'original_price' => string '7.4380' (length=6) public 'base_original_price' => string '7.4380' (length=6) public 'tax_percent' => string '21.0000' (length=7) public 'tax_amount' => string '3.1200' (length=6) public 'base_tax_amount' => string '3.1200' (length=6) public 'tax_invoiced' => string '0.0000' (length=6) public 'base_tax_invoiced' => string '0.0000' (length=6) public 'discount_percent' => string '0.0000' (length=6) public 'discount_amount' => string '0.0000' (length=6) public 'base_discount_amount' => string '0.0000' (length=6) public 'discount_invoiced' => string '0.0000' (length=6) public 'base_discount_invoiced' => string '0.0000' (length=6) public 'amount_refunded' => string '0.0000' (length=6) public 'base_amount_refunded' => string '0.0000' (length=6) public 'row_total' => string '14.8800' (length=7) public 'base_row_total' => string '14.8800' (length=7) public 'row_invoiced' => string '0.0000' (length=6) public 'base_row_invoiced' => string '0.0000' (length=6) public 'row_weight' => string '0.4000' (length=6) public 'gift_message_available' => string '0' (length=1) public 'weee_tax_applied' => string 'a:0:{}' (length=6) public 'weee_tax_applied_amount' => string '0.0000' (length=6) public 'weee_tax_applied_row_amount' => string '0.0000' (length=6) public 'base_weee_tax_applied_amount' => string '0.0000' (length=6) public 'base_weee_tax_applied_row_amount' => string '0.0000' (length=6) public 'weee_tax_disposition' => string '0.0000' (length=6) public 'weee_tax_row_disposition' => string '0.0000' (length=6) public 'base_weee_tax_disposition' => string '0.0000' (length=6) public 'base_weee_tax_row_disposition' => string '0.0000' (length=6) 1 => object(stdClass)[880] public 'item_id' => string '188722' (length=6) public 'order_id' => string '154957' (length=6) public 'quote_item_id' => string '413110' (length=6) public 'created_at' => string '2018-09-28 11:40:06' (length=19) public 'updated_at' => string '2018-09-28 11:40:08' (length=19) public 'product_id' => string '11154' (length=5) public 'product_type' => string 'simple' (length=6) public 'product_options' => string 'a:1:{s:15:"info_buyRequest";a:2:{s:3:"sku";s:3:"456";s:3:"qty";d:3;}}' (length=69) public 'weight' => string '0.2000' (length=6) public 'is_virtual' => string '0' (length=1) public 'sku' => string '456' (length=3) public 'name' => string 'Nombre de producto 2' (length=50) public 'free_shipping' => string '0' (length=1) public 'is_qty_decimal' => string '0' (length=1) public 'no_discount' => string '0' (length=1) public 'qty_canceled' => string '0.0000' (length=6) public 'qty_invoiced' => string '0.0000' (length=6) public 'qty_ordered' => string '3.0000' (length=6) public 'qty_refunded' => string '0.0000' (length=6) public 'qty_shipped' => string '0.0000' (length=6) public 'price' => string '9.9200' (length=6) public 'base_price' => string '9.9200' (length=6) public 'original_price' => string '9.9174' (length=6) public 'base_original_price' => string '9.9174' (length=6) public 'tax_percent' => string '21.0000' (length=7) public 'tax_amount' => string '6.2500' (length=6) public 'base_tax_amount' => string '6.2500' (length=6) public 'tax_invoiced' => string '0.0000' (length=6) public 'base_tax_invoiced' => string '0.0000' (length=6) public 'discount_percent' => string '0.0000' (length=6) public 'discount_amount' => string '0.0000' (length=6) public 'base_discount_amount' => string '0.0000' (length=6) public 'discount_invoiced' => string '0.0000' (length=6) public 'base_discount_invoiced' => string '0.0000' (length=6) public 'amount_refunded' => string '0.0000' (length=6) public 'base_amount_refunded' => string '0.0000' (length=6) public 'row_total' => string '29.7600' (length=7) public 'base_row_total' => string '29.7600' (length=7) public 'row_invoiced' => string '0.0000' (length=6) public 'base_row_invoiced' => string '0.0000' (length=6) public 'row_weight' => string '0.6000' (length=6) public 'gift_message_available' => string '0' (length=1) public 'weee_tax_applied' => string 'a:0:{}' (length=6) public 'weee_tax_applied_amount' => string '0.0000' (length=6) public 'weee_tax_applied_row_amount' => string '0.0000' (length=6) public 'base_weee_tax_applied_amount' => string '0.0000' (length=6) public 'base_weee_tax_applied_row_amount' => string '0.0000' (length=6) public 'weee_tax_disposition' => string '0.0000' (length=6) public 'weee_tax_row_disposition' => string '0.0000' (length=6) public 'base_weee_tax_disposition' => string '0.0000' (length=6) public 'base_weee_tax_row_disposition' => string '0.0000' (length=6) public 'payment' => object(stdClass)[881] public 'parent_id' => string '154957' (length=6) public 'amount_ordered' => string '54.0100' (length=7) public 'shipping_amount' => string '0.0000' (length=6) public 'base_amount_ordered' => string '54.0100' (length=7) public 'base_shipping_amount' => string '0.0000' (length=6) public 'method' => string 'banktransfer' (length=12) public 'cc_exp_month' => string '0' (length=1) public 'cc_exp_year' => string '0' (length=1) public 'cc_ss_start_month' => string '0' (length=1) public 'cc_ss_start_year' => string '0' (length=1) public 'payment_id' => string '154723' (length=6) public 'status_history' => array (size=2) 0 => object(stdClass)[882] public 'parent_id' => string '154957' (length=6) public 'created_at' => string '2018-09-28 11:40:08' (length=19) public 'is_customer_notified' => string '2' (length=1) public 'status' => string 'pendtrans' (length=9) public 'comment' => string 'Cambio de estado de pedido ..sin usuario de administración asociado.' (length=70) 1 => object(stdClass)[883] public 'parent_id' => string '154957' (length=6) public 'created_at' => string '2018-09-28 11:40:08' (length=19) public 'is_customer_notified' => string '1' (length=1) public 'status' => string 'pendtrans' (length=9) public 'fooman_surcharge_amount' => string '0.0000' (length=6) public 'base_fooman_surcharge_amount' => string '0.0000' (length=6) public 'fooman_surcharge_tax_amount' => string '0.0000' (length=6) public 'base_fooman_surcharge_tax_amount' => string '0.0000' (length=6) public 'fooman_surcharge_amount_invoiced' => string '0.0000' (length=6) public 'base_fooman_surcharge_amount_invoiced' => string '0.0000' (length=6) public 'fooman_surcharge_amount_refunded' => string '0.0000' (length=6) public 'base_fooman_surcharge_amount_refunded' => string '0.0000' (length=6) public 'fooman_surcharge_tax_amount_invoiced' => string '0.0000' (length=6) public 'base_fooman_surcharge_tax_amount_invoiced' => string '0.0000' (length=6) public 'fooman_surcharge_tax_amount_refunded' => string '0.0000' (length=6) public 'base_fooman_surcharge_tax_amount_refunded' => string '0.0000' (length=6)
Aquí lo dejo, otro día más.
¡Un saludo!