BigCommerce Uploading Tracking Numbers

2019-08-05 15:35发布

Is there currently a way to upload a tracking number back to an order on BigCommerce in php? I can see on BigCommerce's API Doc for Shipments that there is a parameter to specific a tracking number for a PUT command. I also see that there is an update function within the Shipment.php file. However, I am unsure how to call the function that would allow me to do that, or if it is even possible upload a tracking number.

Below is a snippet from shipment.php

namespace Bigcommerce\Api\Resources;

use Bigcommerce\Api\Resource;
use Bigcommerce\Api\Client;

class Shipment extends Resource
{
    ...

    public function create()
    {
        return Client::createResource('/orders/' . $this->order_id . '/shipments', $this->getCreateFields());
    }

    public function update()
    {
        return Client::createResource('/orders/' . $this->order_id . '/shipments' . $this->id, $this->getCreateFields());
    }
}

Here is also the link to the API Doc for PUT.
https://developer.bigcommerce.com/api/stores/v2/orders/shipments#update-a-shipment

1条回答
beautiful°
2楼-- · 2019-08-05 16:08

You can use the shipment object directly to create a new shipment, as long as you pass in required fields (as shown on the doc page).

<?php

$shipment = new Bigcommerce\Api\Resources\Shipment();
$shipment->order_address_id = $id; // destination address id
$shipment->items = $items; // a list of order items to send with the shipment
$shipment->tracking_number = $track; // the string of the tracking id
$shipment->create();

You can also pass in the info directly as an array to the createResource function:

<?php

$shipment = array(
   'order_address_id' => $id,
   'items' => $items,
   'tracking_number' => $track
);

Bigcommerce\Api\Client::createResource("/orders/1/shipments", $shipment);

Doing a PUT is similar. You can traverse to it from an order object:

<?php

$order = Bigcommerce\Api\Client::getOrder($orderId);

foreach($order->shipments as $shipment) {
    if ($shipment->id == $idToUpdate) {
       $shipment->tracking_number = $track;
       $shipment->update();
       break;
    }
}

Or pull it back directly as an object and re-save it:

<?php

$shipment = Bigcommerce\Api\Client::getResource("/orders/1/shipments/1", "Shipment");
$shipment->tracking_number = $track;
$shipment->update();

Or update it directly:

<?php

$shipment = array(
   'tracking_number' => $track
);

Bigcommerce\Api\Client::updateResource("/orders/1/shipments/1", $shipment);
查看更多
登录 后发表回答