问题
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:
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);
来源:https://stackoverflow.com/questions/17663111/bigcommerce-uploading-tracking-numbers