Question

I am trying to update the tracking number of an order in Bigcommerce using the API. This is the code I am using:

//update BC of order status
$filter = array('status_id' => 2);
$order_status_update = BigCommerce::updateResource('/orders/' . 105, $filter);

$order = Bigcommerce::getOrder(105);
foreach($order->products as $shipment) 
{
    $filter = array(
        'order_address_id' => $shipment->order_address_id,
        'items'=> array(
            'order_product_id' => $shipment->product_id,
            'quantity' => $shipment->quantity
            ),
        'tracking_number' => 'tracking number'
        );
    $add_tracking = BigCommerce::createResource('/orders/105/shipments', $filter);
    var_dump($add_tracking);
}

I have followed the instructions from here: https://developer.bigcommerce.com/api/stores/v2/orders/shipments#list-shipments BigCommerce Uploading Tracking Numbers

But I can't seem to get it to work. Can someone help?

In advance, thanks for your help!

Akshay

Was it helpful?

Solution

The payload for creating a shipment is invalid due to the items field needing to be formatted as an object array and the use of the product_id as opposed to the ID of the product within the order.

The code provided is attempting to create one shipment per product in the order, is this intended? Ideally you would ship all items in one shipment, which is why the items field is meant to be an array of product objects.

Additionally, by creating a shipment for an order the order status will automatically change to "Shipped", so the first PUT request is unnecessary.

If you were trying to create a single shipment per order and are assuming no orders will have multiple addresses then this code should work.

$order = Bigcommerce::getOrder(105);

$filter = array(
    'order_address_id' => $order->shipping_addresses[0]->id,
    'tracking_number' => '123456'
);

foreach($order->products as $product) {
    $items[] = array(
        'order_product_id' => $product->id,
        'quantity' => $product->quantity
    );
}

$filter['items'] => $items;

$add_tracking = BigCommerce::createResource('/orders/105/shipments', $filter);
var_dump($add_tracking);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top