Skip to content

Commit da89ff8

Browse files
authored
Merge pull request #19 from rkistaps/master
Products API
2 parents 4f47b45 + 3df2e39 commit da89ff8

36 files changed

+2345
-13
lines changed

examples/Products/delete.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
use Printful\Exceptions\PrintfulApiException;
4+
use Printful\Exceptions\PrintfulException;
5+
use Printful\PrintfulApiClient;
6+
use Printful\PrintfulProducts;
7+
8+
require_once __DIR__ . '/../../vendor/autoload.php';
9+
10+
/**
11+
* This example fill will demonstrate how to delete Product and it's variants using Products Api
12+
*/
13+
14+
// Replace this with your API key
15+
$apiKey = '';
16+
17+
// Delete product example
18+
// Docs for this endpoint can be found here: https://www.printful.local/docs/products#actionDeleteProduct
19+
try {
20+
21+
// product id in Printful store, which we want to delete
22+
$id = 1;
23+
24+
// you can also use your external id
25+
// $externalId = 32142;
26+
// $id = '@'.$externalId;
27+
28+
// create ApiClient
29+
$pf = new PrintfulApiClient($apiKey);
30+
31+
// create Products Api object
32+
$productsApi = new PrintfulProducts($pf);
33+
34+
$productsApi->deleteProduct($id);
35+
36+
} catch (PrintfulApiException $e) { // API response status code was not successful
37+
echo 'Printful API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
38+
} catch (PrintfulException $e) { // API call failed
39+
echo 'Printful Exception: ' . $e->getMessage();
40+
var_export($pf->getLastResponseRaw());
41+
}
42+
43+
// Delete variant example
44+
// Docs for this endpoint can be found here: https://www.printful.local/docs/products#actionDeleteVariant
45+
try {
46+
47+
// variant id in Printful store, which we want to delete
48+
// remember that you can not delete products last variant, you should delete whole product instead
49+
$id = 1;
50+
51+
// you can also use your external id
52+
// $externalId = 32142;
53+
// $id = '@'.$externalId;
54+
55+
// create ApiClient
56+
$pf = new PrintfulApiClient($apiKey);
57+
58+
// create Products Api object
59+
$productsApi = new PrintfulProducts($pf);
60+
61+
$productsApi->deleteVariant($id);
62+
63+
} catch (PrintfulApiException $e) { // API response status code was not successful
64+
echo 'Printful API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
65+
} catch (PrintfulException $e) { // API call failed
66+
echo 'Printful Exception: ' . $e->getMessage();
67+
var_export($pf->getLastResponseRaw());
68+
}

examples/Products/get-products.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Printful\Exceptions\PrintfulApiException;
4+
use Printful\Exceptions\PrintfulException;
5+
use Printful\PrintfulApiClient;
6+
use Printful\PrintfulProducts;
7+
use Printful\Structures\Sync\Responses\SyncProductsResponse;
8+
9+
require_once __DIR__ . '/../../vendor/autoload.php';
10+
11+
/**
12+
* This example fill will demonstrate how to get list of products in your store using Products API
13+
* Docs for this endpoint can be found here: https://www.printful.com/docs/products#listProducts
14+
*/
15+
16+
17+
// Replace this with your API key
18+
$apiKey = '';
19+
20+
try {
21+
// create ApiClient
22+
$pf = new PrintfulApiClient($apiKey);
23+
24+
// create Products Api object
25+
$productsApi = new PrintfulProducts($pf);
26+
27+
// set some paging info
28+
$offset = 0;
29+
$limit = 20;
30+
31+
/** @var SyncProductsResponse $list */
32+
$list = $productsApi->getProducts($offset, $limit);
33+
34+
} catch (PrintfulApiException $e) { // API response status code was not successful
35+
echo 'Printful API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
36+
} catch (PrintfulException $e) { // API call failed
37+
echo 'Printful Exception: ' . $e->getMessage();
38+
var_export($pf->getLastResponseRaw());
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
use Printful\Exceptions\PrintfulApiException;
4+
use Printful\Exceptions\PrintfulException;
5+
use Printful\PrintfulApiClient;
6+
use Printful\PrintfulProducts;
7+
use Printful\Structures\Sync\Responses\SyncProductRequestResponse;
8+
9+
require_once __DIR__ . '/../../vendor/autoload.php';
10+
11+
/**
12+
* This example fill will demonstrate how to get a single Product using id and external id
13+
* Docs for this endpoint can be found here: https://www.printful.com/docs/products#getSingleSyncProduct
14+
*/
15+
16+
// Replace this with your API key
17+
$apiKey = '';
18+
19+
try {
20+
// create ApiClient
21+
$pf = new PrintfulApiClient($apiKey);
22+
23+
// create Products Api object
24+
$productsApi = new PrintfulProducts($pf);
25+
26+
// product id in your Printful store
27+
$productId = 1;
28+
29+
// get product by id
30+
/** @var SyncProductRequestResponse $response */
31+
$response = $productsApi->getProduct($productId);
32+
33+
// actual product can be found $response->syncProduct
34+
// and its variants $response->syncVariants
35+
36+
// product id in your store(saved with external_id)
37+
$externalProductId = 15946;
38+
39+
// get product by external_id
40+
/** @var SyncProductRequestResponse $response */
41+
$response = $productsApi->getProduct('@' . $externalProductId);
42+
43+
// actual product can be found $response->syncProduct
44+
// and products variants $response->syncVariants
45+
46+
} catch (PrintfulApiException $e) { // API response status code was not successful
47+
echo 'Printful API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
48+
} catch (PrintfulException $e) { // API call failed
49+
echo 'Printful Exception: ' . $e->getMessage();
50+
var_export($pf->getLastResponseRaw());
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use Printful\Exceptions\PrintfulApiException;
4+
use Printful\Exceptions\PrintfulException;
5+
use Printful\PrintfulApiClient;
6+
use Printful\PrintfulProducts;
7+
use Printful\Structures\Sync\Responses\SyncVariantResponse;
8+
9+
require_once __DIR__ . '/../../vendor/autoload.php';
10+
11+
/**
12+
* This example fill will demonstrate how to get a single Variant using id and external id
13+
* Docs for this endpoint can be found here: https://www.printful.com/docs/products#getSingleSyncVariant
14+
*/
15+
16+
// Replace this with your API key
17+
$apiKey = '';
18+
19+
try {
20+
// create ApiClient
21+
$pf = new PrintfulApiClient($apiKey);
22+
23+
// create Products Api object
24+
$productsApi = new PrintfulProducts($pf);
25+
26+
// variant id in your Printful store
27+
$variantId = 1;
28+
29+
// get variant by id
30+
/** @var SyncVariantResponse $product */
31+
$product = $productsApi->getProduct($variantId);
32+
33+
34+
// variant id in your store(saved with external_id)
35+
$externalVariantId = 15946;
36+
37+
// get variant by external_id
38+
/** @var SyncVariantResponse $product */
39+
$product = $productsApi->getProduct('@' . $externalVariantId);
40+
41+
} catch (PrintfulApiException $e) { // API response status code was not successful
42+
echo 'Printful API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
43+
} catch (PrintfulException $e) { // API call failed
44+
echo 'Printful Exception: ' . $e->getMessage();
45+
var_export($pf->getLastResponseRaw());
46+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
use Printful\Exceptions\PrintfulApiException;
4+
use Printful\Exceptions\PrintfulException;
5+
use Printful\Exceptions\PrintfulSdkException;
6+
use Printful\PrintfulApiClient;
7+
use Printful\PrintfulProducts;
8+
use Printful\Structures\Sync\SyncProductCreationParameters;
9+
10+
require_once __DIR__ . '/../../vendor/autoload.php';
11+
12+
/**
13+
* This example fill will demonstrate usage of Products API using data in an array
14+
* Docs for this endpoint can be found here: https://www.printful.com/docs/products#actionCreateProduct
15+
*/
16+
17+
// Replace this with your API key
18+
$apiKey = '';
19+
20+
try {
21+
// create ApiClient
22+
$pf = new PrintfulApiClient($apiKey);
23+
24+
// create Products Api object
25+
$productsApi = new PrintfulProducts($pf);
26+
27+
$data = [
28+
'sync_product' => [
29+
'external_id' => 1, // set id in my store for this product (optional)
30+
'name' => 'My new shirt',
31+
'thumbnail' => 'https://www.my-webshop.com/shirt.jpg', // set thumbnail url
32+
],
33+
'sync_variants' => [ // add sync variants
34+
[
35+
'external_id' => 1, // set id in my store for this variant (optional)
36+
'retail_price' => 21.00, // set retail price that this item is sold for (optional)
37+
'variant_id' => 4011, // set variant in from Printful Catalog(https://www.printful.com/docs/catalog)
38+
'files' => [
39+
[
40+
'url' => 'https://www.my-webshop.com/shirt.jpg',
41+
],
42+
[
43+
'type' => 'back', // set print file placement on item. If not set, default placement for this product will be used
44+
'id' => 1, // file id from my File library in Printful (https://www.printful.com/docs/files)
45+
],
46+
],
47+
'options' => [
48+
[
49+
'id' => 'embroidery_type',
50+
'value' => 'flat'
51+
],
52+
],
53+
],
54+
[
55+
'external_id' => 2, // set id in my store for this variant (optional)
56+
'retail_price' => 21.00, // set retail price that this item is sold for (optional)
57+
'variant_id' => 4012, // set variant in from Printful Catalog(https://www.printful.com/docs/catalog)
58+
'files' => [
59+
[
60+
'url' => 'https://www.my-webshop.com/shirt.jpg',
61+
],
62+
[
63+
'type' => 'back', // set print file placement on item. If not set, default placement for this product will be used
64+
'id' => 1, // file id from my File library in Printful (https://www.printful.com/docs/files)
65+
],
66+
],
67+
'options' => [
68+
[
69+
'id' => 'embroidery_type',
70+
'value' => 'flat'
71+
],
72+
],
73+
],
74+
75+
],
76+
];
77+
78+
$creationParams = SyncProductCreationParameters::fromArray($data);
79+
80+
$product = $productsApi->createProduct($creationParams);
81+
82+
} catch (PrintfulApiException $e) { // API response status code was not successful
83+
echo 'Printful API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
84+
} catch (PrintfulSdkException $e) { // SDK did not call API
85+
echo 'Printful SDK Exception: ' . $e->getMessage();
86+
} catch (PrintfulException $e) { // API call failed
87+
echo 'Printful Exception: ' . $e->getMessage();
88+
var_export($pf->getLastResponseRaw());
89+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
use Printful\Exceptions\PrintfulApiException;
4+
use Printful\Exceptions\PrintfulException;
5+
use Printful\Exceptions\PrintfulSdkException;
6+
use Printful\PrintfulApiClient;
7+
use Printful\PrintfulProducts;
8+
use Printful\Structures\Sync\Requests\SyncProductRequest;
9+
use Printful\Structures\Sync\Requests\SyncVariantRequest;
10+
use Printful\Structures\Sync\SyncProductCreationParameters;
11+
12+
require_once __DIR__ . '/../../vendor/autoload.php';
13+
14+
/**
15+
* This example fill will demonstrate usage of Products API in mixed fashion
16+
* Docs for this endpoint can be found here: https://www.printful.com/docs/products#actionCreateProduct
17+
*/
18+
19+
// Replace this with your API key
20+
$apiKey = '';
21+
22+
try {
23+
// create ApiClient
24+
$pf = new PrintfulApiClient($apiKey);
25+
26+
// create Products Api object
27+
$productsApi = new PrintfulProducts($pf);
28+
29+
// create product request
30+
$productRequest = SyncProductRequest::fromArray([
31+
'external_id' => 1, // set id in my store for this product (optional)
32+
'name' => 'My new shirt', // set product name
33+
'thumbnail' => 'https://www.my-webshop.com/shirt.jpg', // // set thumbnail url (optional)
34+
]);
35+
36+
// create creationParams
37+
$creationParams = new SyncProductCreationParameters($productRequest);
38+
39+
// create variant A (Bella + Canvas 3001, S, White)
40+
$syncVariantRequest = SyncVariantRequest::fromArray([
41+
'external_id' => 1, // set id in my store for this variant (optional)
42+
'variant_id' => 4011, // set variant in from Printful Catalog(https://www.printful.com/docs/catalog)
43+
'retail_price' => 21.00, // set retail price that this item is sold for (optional)
44+
'files' => [
45+
[
46+
'url' => 'https://www.my-webshop.com/shirt.jpg',
47+
],
48+
[
49+
'type' => 'back', // set print file placement on item. If not set, default placement for this product will be used
50+
'id' => 1, // file id from my File library in Printful (https://www.printful.com/docs/files)
51+
],
52+
],
53+
'options' => [
54+
[
55+
'id' => 'embroidery_type',
56+
'value' => 'flat'
57+
],
58+
],
59+
]);
60+
61+
// add variant to creation params
62+
$creationParams->addSyncVariant($syncVariantRequest);
63+
64+
// create variant B (Bella + Canvas 3001, M, White)
65+
$syncVariantRequest = SyncVariantRequest::fromArray([
66+
'external_id' => 2, // set id in my store for this variant (optional)
67+
'variant_id' => 4012, // set variant in from Printful Catalog(https://www.printful.com/docs/catalog)
68+
'retail_price' => 21.00, // set retail price that this item is sold for (optional)
69+
'files' => [
70+
[
71+
'url' => 'https://www.my-webshop.com/shirt.jpg',
72+
],
73+
[
74+
'type' => 'back', // set print file placement on item. If not set, default placement for this product will be used
75+
'id' => 1, // file id from my File library in Printful (https://www.printful.com/docs/files)
76+
],
77+
],
78+
'options' => [
79+
[
80+
'id' => 'embroidery_type',
81+
'value' => 'flat'
82+
],
83+
],
84+
]);
85+
86+
// add variant to creation params
87+
$creationParams->addSyncVariant($syncVariantRequest);
88+
89+
$product = $productsApi->createProduct($creationParams);
90+
91+
} catch (PrintfulApiException $e) { // API response status code was not successful
92+
echo 'Printful API Exception: ' . $e->getCode() . ' ' . $e->getMessage();
93+
} catch (PrintfulSdkException $e) { // SDK did not call API
94+
echo 'Printful SDK Exception: ' . $e->getMessage();
95+
} catch (PrintfulException $e) { // API call failed
96+
echo 'Printful Exception: ' . $e->getMessage();
97+
var_export($pf->getLastResponseRaw());
98+
}

0 commit comments

Comments
 (0)