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