Skip to content

Commit 94e5618

Browse files
authored
Merge pull request #200 from bc-fabiob/SHIPPING-249
Add shipping zone and method to client api
2 parents a03188a + 222560e commit 94e5618

6 files changed

Lines changed: 301 additions & 0 deletions

File tree

src/Bigcommerce/Api/Client.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,4 +1943,73 @@ public static function deleteWebhook($id)
19431943
{
19441944
return self::deleteResource('/hooks/' . $id);
19451945
}
1946+
1947+
/**
1948+
* Return a collection of shipping-zones
1949+
*
1950+
* @return mixed
1951+
*/
1952+
public static function getShippingZones()
1953+
{
1954+
return self::getCollection('/shipping/zones/', 'ShippingZone');
1955+
}
1956+
1957+
/**
1958+
* Return a shipping-zone by id
1959+
*
1960+
* @param int $id shipping-zone id
1961+
* @return mixed
1962+
*/
1963+
public static function getShippingZone($id)
1964+
{
1965+
return self::getResource('/shipping/zones/' . $id, 'ShippingZone');
1966+
}
1967+
1968+
1969+
/**
1970+
* Delete the given shipping-zone
1971+
*
1972+
* @param int $id shipping-zone id
1973+
* @return mixed
1974+
*/
1975+
public static function deleteShippingZone($id)
1976+
{
1977+
return self::deleteResource('/shipping/zones/' . $id);
1978+
}
1979+
1980+
/**
1981+
* Return a shipping-method by id
1982+
*
1983+
* @param $zoneId
1984+
* @param $methodId
1985+
* @return mixed
1986+
*/
1987+
public static function getShippingMethod($zoneId, $methodId)
1988+
{
1989+
return self::getResource('/shipping/zones/'. $zoneId . '/methods/'. $methodId, 'ShippingMethod');
1990+
}
1991+
1992+
/**
1993+
* Return a collection of shipping-methods
1994+
*
1995+
* @param $zoneId
1996+
* @return mixed
1997+
*/
1998+
public static function getShippingMethods($zoneId)
1999+
{
2000+
return self::getCollection('/shipping/zones/' . $zoneId . '/methods', 'ShippingMethod');
2001+
}
2002+
2003+
2004+
/**
2005+
* Delete the given shipping-method by id
2006+
*
2007+
* @param $zoneId
2008+
* @param $methodId
2009+
* @return mixed
2010+
*/
2011+
public static function deleteShippingMethod($zoneId, $methodId)
2012+
{
2013+
return self::deleteResource('/shipping/zones/'. $zoneId . '/methods/'. $methodId);
2014+
}
19462015
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Bigcommerce\Api\Resources;
4+
5+
use Bigcommerce\Api\Client;
6+
use Bigcommerce\Api\Resource;
7+
8+
class ShippingMethod extends Resource
9+
{
10+
protected $ignoreOnCreate = array(
11+
'id',
12+
);
13+
14+
protected $ignoreOnUpdate = array(
15+
'id',
16+
);
17+
18+
public function create($zoneId)
19+
{
20+
return Client::createResource('/shipping/zones/' . $zoneId . '/methods', $this->getCreateFields());
21+
}
22+
23+
public function update($zoneId)
24+
{
25+
return Client::updateResource('/shipping/zones/' . $zoneId . '/methods/' . $this->id, $this->getUpdateFields());
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Bigcommerce\Api\Resources;
4+
5+
use Bigcommerce\Api\Client;
6+
use Bigcommerce\Api\Resource;
7+
8+
class ShippingZone extends Resource
9+
{
10+
protected $ignoreOnCreate = array(
11+
'id',
12+
);
13+
14+
protected $ignoreOnUpdate = array(
15+
'id',
16+
);
17+
18+
public function create()
19+
{
20+
return Client::createResource('/shipping/zones/', $this->getCreateFields());
21+
}
22+
23+
public function update()
24+
{
25+
return Client::updateResource('/shipping/zones/'. $this->id, $this->getUpdateFields());
26+
}
27+
}

test/Unit/Api/Resources/RealTest.php

Whitespace-only changes.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Bigcommerce\Test\Unit\Api\Resources;
4+
5+
use Bigcommerce\Api\Client;
6+
use Bigcommerce\Api\Resources\ShippingMethod;
7+
8+
class ShippingMethodTest extends ResourceTestBase
9+
{
10+
public function testCreateShippingMethod()
11+
{
12+
$input = array(
13+
"name" => "USPS Endicia",
14+
"type" => "endicia",
15+
"settings" => array(
16+
"carrier_options" => array(
17+
"delivery_services" => array(
18+
"PriorityExpress",
19+
"PriorityMailExpressInternational",
20+
"FirstClassPackageInternationalService",
21+
"Priority",
22+
"PriorityMailInternational",
23+
"First",
24+
"ParcelSelect",
25+
"MediaMail"
26+
),
27+
"packaging" => array(
28+
"FlatRateLegalEnvelope",
29+
"FlatRatePaddedEnvelope",
30+
"Parcel",
31+
"SmallFlatRateBox",
32+
"MediumFlatRateBox",
33+
"LargeFlatRateBox",
34+
"FlatRateEnvelope",
35+
"RegionalRateBoxA",
36+
"RegionalRateBoxB"
37+
),
38+
"show_transit_time" => true,
39+
)
40+
),
41+
"enabled" => true
42+
);
43+
$method = new ShippingMethod((object)$input);
44+
$this->connection->expects($this->once())
45+
->method('post')
46+
->with($this->basePath . '/shipping/zones/1/methods', (object)$input);
47+
48+
$method->create(1);
49+
}
50+
51+
public function testUpdateShippingMethod()
52+
{
53+
$input = array(
54+
"name" => "Ship by Weight",
55+
"type" => "weight",
56+
"settings" => array(
57+
"default_cost" => 1,
58+
"default_cost_type" => "fixed_amount",
59+
"range" => array(
60+
array(
61+
"lower_limit" => 0,
62+
"upper_limit" => 20,
63+
"shipping_cost" => 8
64+
)
65+
)
66+
),
67+
"enabled" => true
68+
);
69+
$updateResource = array_merge(array('id' => 1), $input);
70+
$method = new ShippingMethod((object)$updateResource);
71+
$this->connection->expects($this->once())
72+
->method('put')
73+
->with($this->basePath . '/shipping/zones/1/methods/1', (object)$input);
74+
75+
$method->update(1);
76+
}
77+
78+
public function testGetShippingMethod()
79+
{
80+
$this->connection->expects($this->once())
81+
->method('get')
82+
->with($this->basePath . '/shipping/zones/1/methods/2');
83+
84+
Client::getShippingMethod(1, 2);
85+
}
86+
87+
public function testGetShippingMethods()
88+
{
89+
$this->connection->expects($this->once())
90+
->method('get')
91+
->with($this->basePath . '/shipping/zones/1/methods');
92+
93+
Client::getShippingMethods(1);
94+
}
95+
96+
public function testDeleteShippingMethod()
97+
{
98+
99+
$this->connection->expects($this->once())
100+
->method('delete')
101+
->with($this->basePath . '/shipping/zones/1/methods/2');
102+
103+
Client::deleteShippingMethod(1, 2);
104+
}
105+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Bigcommerce\Test\Unit\Api\Resources;
4+
5+
use Bigcommerce\Api\Client;
6+
use Bigcommerce\Api\Resources\ShippingZone;
7+
8+
class ShippingZoneTest extends ResourceTestBase
9+
{
10+
public function testCreateShippingZone()
11+
{
12+
$input = array(
13+
'name' => 'United States',
14+
'type' => 'country',
15+
'locations' => array(
16+
array('country_iso2' => 'US'),
17+
),
18+
);
19+
$zone = new ShippingZone((object)$input);
20+
$this->connection->expects($this->once())
21+
->method('post')
22+
->with($this->basePath . '/shipping/zones/', (object)$input);
23+
24+
$zone->create();
25+
}
26+
27+
public function testUpdateShippingZone()
28+
{
29+
$input = array(
30+
'name' => 'United States',
31+
'type' => 'country',
32+
'locations' => array(
33+
array('country_iso2' => 'US'),
34+
),
35+
);
36+
$updateResource = array_merge(array('id' => 1), $input);
37+
$zone = new ShippingZone((object)$updateResource);
38+
39+
$this->connection->expects($this->once())
40+
->method('put')
41+
->with($this->basePath . '/shipping/zones/1', (object)$input);
42+
43+
$zone->update();
44+
}
45+
46+
public function testGetShippingZone()
47+
{
48+
$this->connection->expects($this->once())
49+
->method('get')
50+
->with($this->basePath . '/shipping/zones/1');
51+
52+
Client::getShippingZone(1);
53+
}
54+
55+
public function testGetShippingZones()
56+
{
57+
$this->connection->expects($this->once())
58+
->method('get')
59+
->with($this->basePath . '/shipping/zones/');
60+
61+
Client::getShippingZones();
62+
}
63+
64+
public function testDeleteShippingZone()
65+
{
66+
67+
$this->connection->expects($this->once())
68+
->method('delete')
69+
->with($this->basePath . '/shipping/zones/1');
70+
71+
Client::deleteShippingZone(1);
72+
}
73+
}

0 commit comments

Comments
 (0)