/
createProduct.php
227 lines (197 loc) · 8.19 KB
/
createProduct.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/*
*
* Example of using the PrestShop WebService to create a product with
* its stock and its image
*
*/
define('DEBUG', true);
define('_PS_DEBUG_SQL_', true);
define('PS_SHOP_PATH', 'http://yourdomain.com');
define('PS_WS_AUTH_KEY', '12345H26EDWN12345TSB512345HNUIFD');
require_once ('../PSWebServiceLibrary.php');
//Here we take the product schema and add our datas
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/products?schema=blank'));
$resources = $xml->children()->children();
$resources->id;
$resources->id_manufacturer;
$resources->id_supplier;
$resources->id_category_default = 10;
$resources->new;
$resources->cache_default_attribute;
$resources->id_default_image = 1;
$resources->id_default_combination;
$resources->id_tax_rules_group;
$resources->position_in_category;
$resources->type = "virtual";
$resources->id_shop_default;
$resources->reference = "ref_product";
$resources->supplier_reference;
$resources->location;
$resources->width;
$resources->height;
$resources->depth;
$resources->weight;
$resources->quantity_discount;
$resources->ean13;
$resources->upc;
$resources->cache_is_pack;
$resources->cache_has_attachments;
$resources->is_virtual;
$resources->on_sale;
$resources->online_only;
$resources->ecotax;
$resources->minimal_quantity;
$resources->price = 22.65;
$resources->wholesale_price;
$resources->unity;
$resources->unit_price_ratio;
$resources->additional_shipping_cost;
$resources->customizable;
$resources->text_fields;
$resources->uploadable_files;
$resources->active = 1;
$resource->redirect_type;
$resource->id_product_redirected;
$resources->available_for_order = 1;
$resources->available_date;
$resources->condition;
$resources->show_price = 1;
$resources->indexed;
$resources->visibility;
$resources->advanced_stock_management;
$resources->date_add;
$resources->date_upd;
$node = dom_import_simplexml($resources->meta_description->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata meta description"));
$resources->meta_description->language[0][0] = "meta description";
$resources->meta_description->language[0][0]['id'] = 1;
$resources->meta_description->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->meta_keywords->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata meta keywords"));
$resources->meta_keywords->language[0][0] = "meta keywords1, keywords2, keywords3";
$resources->meta_keywords->language[0][0]['id'] = 1;
$resources->meta_keywords->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->meta_title->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata meta title"));
$resources->meta_title->language[0][0] = "meta title";
$resources->meta_title->language[0][0]['id'] = 1;
$resources->meta_title->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->link_rewrite->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata link_rewrite"));
$resources->link_rewrite->language[0][0] = "link-rewrite";
$resources->link_rewrite->language[0][0]['id'] = 1;
$resources->link_rewrite->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->name->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata name"));
$resources->name->language[0][0] = "New product name";
$resources->name->language[0][0]['id'] = 1;
$resources->name->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->description->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata description"));
$resources->description->language[0][0] = "description";
$resources->description->language[0][0]['id'] = 1;
$resources->description->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->description_short->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata description_short"));
$resources->description_short->language[0][0] = "description_short";
$resources->description_short->language[0][0]['id'] = 1;
$resources->description_short->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->available_now->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata In stock"));
$resources->available_now->language[0][0] = "In stock";
$resources->available_now->language[0][0]['id'] = 1;
$resources->available_now->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$node = dom_import_simplexml($resources->available_later->language[0][0]);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection("cdata available_later"));
$resources->available_later->language[0][0] = "available_later";
$resources->available_later->language[0][0]['id'] = 1;
$resources->available_later->language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/1';
$resources->associations->categories->addChild('category')->addChild('id', 1);
$resources->associations->categories->addChild('category')->addChild('id', 4);
//Here we call to add a new product
try {
$opt = array('resource' => 'products');
$opt['postXml'] = $xml->asXML();
$xml_request = $webService->add($opt);
} catch (PrestaShopWebserviceException $ex) {
echo '<b>Error : '.$ex->getMessage().'</b>';
$trace = $ex->getTrace();
print_r($trace);
}
//We take the reponse of call to do
$xml_response = $xml_request['response'];
$response = new SimpleXMLElement($xml_response);
$resources = $response->children()->children();
/*
When new product created a new stock available id was created and we can take this id to use.
*/
$stock_available_id = $resources->associations->stock_availables->stock_available[0]->id;
$id_created_product = $resources->id;
/*
Here we get the sotck available with were product id
*/
try
{
$opt = array('resource' => 'stock_availables');
$opt['id'] = $stock_available_id;
$xml = $webService->get($opt);
}
catch (PrestaShopWebserviceException $e)
{
// Here we are dealing with errors
$trace = $e->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error<br />'.$e->getMessage();
}
$resources = $xml->children()->children();
//There we put our stock
$resources->quantity = 6666;
/*
There we call to save our stock quantity.
*/
try
{
$opt = array('resource' => 'stock_availables');
$opt['putXml'] = $xml->asXML();
$opt['id'] = $stock_available_id;
$xml = $webService->edit($opt);
// if WebService don't throw an exception the action worked well and we don't show the following message
echo "Successfully updated.";
}
catch (PrestaShopWebserviceException $ex)
{
// Here we are dealing with errors
$trace = $ex->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error<br />'.$ex->getMessage();
}
/*
Here we add an image a created product
*/
$url = "http://prestashop16.conf/api/images/products/$id_created_product";
/**.
* Uncomment the following line in order to update an existing image
*/
//$url = 'http://myprestashop.com/api/images/products/1/2?ps_method=PUT';
$image_path = '/path/your/image.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@'.$image_path.';type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);