Skip to content

Commit 3fa8736

Browse files
kievinsbriedis
authored andcommitted
Added support for order costs estimate (#16)
Added support for order costs estimate. Now you calculate all costs for an order through the API
1 parent 7b60535 commit 3fa8736

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/PrintfulOrder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Printful;
44

55
use Printful\Structures\Order\Order;
6+
use Printful\Structures\Order\OrderCostGroup;
67
use Printful\Structures\Order\OrderCreationParameters;
78
use Printful\Structures\Order\OrderItemCreationParameters;
89
use Printful\Structures\Order\OrderList;
@@ -126,6 +127,21 @@ public function getList($offset = 0, $limit = 10, $status = null)
126127
return new OrderList($rawOrders, $total, $offset);
127128
}
128129

130+
/**
131+
* @param OrderCreationParameters $parameters
132+
* @return OrderCostGroup
133+
* @throws Exceptions\PrintfulApiException
134+
* @throws Exceptions\PrintfulException
135+
*/
136+
public function estimateCosts(OrderCreationParameters $parameters)
137+
{
138+
$request = $this->getRequestBodyFromParams($parameters);
139+
140+
$raw = $this->printfulClient->post('orders/estimate-costs', $request);
141+
142+
return OrderCostGroup::fromArray($raw);
143+
}
144+
129145
/**
130146
* Formats request body array from order creation parameters object
131147
*
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Printful\Structures\Order;
4+
5+
class OrderCostGroup
6+
{
7+
/** @var OrderCostsItem|null */
8+
public $printfulCosts;
9+
10+
/** @var OrderCostsItem|null */
11+
public $retailCosts;
12+
13+
/**
14+
* @param array $raw
15+
* @return OrderCostGroup
16+
*/
17+
public static function fromArray(array $raw)
18+
{
19+
$orderCosts = new self;
20+
21+
$orderCosts->printfulCosts = !empty($raw['costs']) ? OrderCostsItem::fromArray($raw['costs']) : null;
22+
$orderCosts->printfulCosts = !empty($raw['retail_costs']) ? OrderCostsItem::fromArray($raw['retail_costs']) : null;
23+
24+
return $orderCosts;
25+
}
26+
}

tests/Order/OrderCreationAndRetrievalTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use Printful\PrintfulOrder;
55
use Printful\Structures\Order\Order;
6+
use Printful\Structures\Order\OrderCostGroup;
7+
use Printful\Structures\Order\OrderCostsItem;
68
use Printful\Structures\Order\OrderCreationParameters;
79
use Printful\Structures\Order\OrderItemCreationParameters;
810
use Printful\Structures\Order\OrderItemFile;
@@ -96,6 +98,18 @@ public function testOrderListCanBeRetrieved()
9698
self::assertInstanceOf(OrderList::class, $orderList, 'Order List retrieved');
9799
}
98100

101+
public function testOrderCostsCanBeEstimated()
102+
{
103+
$externalId = uniqid();
104+
$params = $this->getTestOrderParams();
105+
$params->externalId = $externalId;
106+
107+
$estimate = $this->printfulOrder->estimateCosts($params);
108+
109+
self::assertInstanceOf(OrderCostGroup::class, $estimate, 'Costs estimate retrieved');
110+
self::assertInstanceOf(OrderCostsItem::class, $estimate->printfulCosts, 'Cost estimate correct type');
111+
}
112+
99113
/**
100114
* @return OrderCreationParameters
101115
*/

0 commit comments

Comments
 (0)