Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

Php Api Shipments Rate Topic   - - - - -

 

Posted 14 April 2021 - 06:02 AM #1

Hello,

 

i want to create a PHP file to read tracking number from parameter of URL and then to POST to cs cart API.

 

My problem is how to POST in the JSON the mandatory field of products.

<?php

$A = false;
if(isset($_GET['wo'])){
    $WO = $_GET['wo'];
}

$B = false;
if(isset($_GET['v'])){
    $V = $_GET['v'];
}

$curl = curl_init();
...
...
...
...
...
...

$data_shipment =  array(
   "carrier" => "",
   "order_id" => $A,
   "products" => array(
        "3910130838" =>  "1", //HERE is my problem, how to GET the product ids from order and POST them
    ),
   "shipping" => "TEST",
   "shipping_id" => "1",
   "user_id" => "0",
   "tracking_number" => $B
);

$update_shipment = callAPI('POST', 'https://www.test.com/api/shipments', json_encode($data_shipment));

Can anyone help me with this?



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 12075 posts

Posted 14 April 2021 - 03:49 PM #2

You might have to use a multi call approach.  I.e. GET's to retrieve the products in an order and the PUTS/POST to create the shipments.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 

Posted 14 April 2021 - 07:06 PM #3

You might have to use a multi call approach.  I.e. GET's to retrieve the products in an order and the PUTS/POST to create the shipments.

I already make this to read the status of order

$get_order = callAPI('GET', 'https://www.test.com/api/orders/'.$WO, json_encode($result, true));

$obj = json_decode($get_order);
$details = $obj->details;
$currentstatus = $obj->status;

i don't know how to read the products id's and quantity and then how to POST



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 12075 posts

Posted 14 April 2021 - 07:50 PM #4

Not familiar with whatever wrapper you're using so I don't know what it's actually returning in $get_order.  But if you get a specific order via the api by using  "orders/123" and then json_decode() the result, you should have a 'products' array that contains the products from the order.  You can then look a that to see how to reference the product_id.

 

Note that casting an array to a StdClass kind of defeats the purpose of doing a json_decode($get_order,true).  (and make sure you use true as the 2nd argument to json_decode.)  Instead you should assume that $obj is an array().  But if you want to use it as an object, you will most likely have to cast your $obj->status to a string.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.


 

Posted 14 April 2021 - 08:56 PM #5

Not familiar with whatever wrapper you're using so I don't know what it's actually returning in $get_order.  But if you get a specific order via the api by using  "orders/123" and then json_decode() the result, you should have a 'products' array that contains the products from the order.  You can then look a that to see how to reference the product_id.

 

Note that casting an array to a StdClass kind of defeats the purpose of doing a json_decode($get_order,true).  (and make sure you use true as the 2nd argument to json_decode.)  Instead you should assume that $obj is an array().  But if you want to use it as an object, you will most likely have to cast your $obj->status to a string.

i just do it like this..

$get_order = callAPI('GET', 'https://www.test.com/api/orders/'.$WO, json_encode($result, true));
$obj = json_decode($get_order);

foreach ($obj->products as $products) {
    $item_id = $products->{'item_id'};
    $amount = $products->{'amount'};
}


//and then i request a POST



$data_shipment =  array(
   "carrier" => "",
   "order_id" => $WO,
   "products" => array(
        $item_id =>  $amount,
    ),
   "shipping" => $M,
   "shipping_id" => $CSCOURIER,
   "user_id" => "0",
   "tracking_number" => $V
);

$update_shipment = callAPI('POST', 'https://www.test.com/api/shipments', json_encode($data_shipment));

Thank you for your help.



 
  • tbirnseth
  • CS Cart Expert
  • Authorized Reseller
  • Join Date: 08-Nov 08
  • 12075 posts

Posted 15 April 2021 - 06:07 PM #6

I'm confused by your code.  Each product (unless this is all one package) may have different tracking info. So the $data_shipment should be inside the loop.  Each product is simply overwriting $item_id and $amount.

 

I'd recommend you do an die("<pre>obj:". print_r($obj,true)); After your json_decode() to see what your data is and then code from there.


EZ Merchant Solutions: Custom (USA based) B2B Development, Consulting, Development and Special Projects (get a quote here).
Commercial addons, payment methods and modifications to meet your business and operations needs.