Created
May 2, 2023 12:28
-
-
Save vmulot/1ad9ba49514135ae615b6245072d70b4 to your computer and use it in GitHub Desktop.
Multisite product image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('./vendor/autoload.php'); | |
define('DEBUG', true); | |
define('PS_SHOP_PATH', 'myawesome https url'); | |
define('PS_WS_AUTH_KEY', 'my awesome api key'); | |
try { | |
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG); | |
$xml = $webService->get(['url' => PS_SHOP_PATH . '/api/products?schema=synopsis']); | |
$fields = $xml->children()->children(); | |
unset($fields->id); | |
unset($fields->position_in_category); | |
unset($fields->manufacturer_name); | |
unset($fields->id_default_combination); | |
unset($fields->associations); | |
unset($fields->quantity); | |
$fields->price = '10.00'; | |
$fields->active = 0; | |
$fields->id_category_default = 2; | |
$fields->name->language[0] = 'Test product webservice'; | |
//TODO : tester sans nom pour la 2e langue. | |
$fields->name->language[1] = 'Test product webservice'; | |
$fields->description->language[0] = 'Test product webservice Desc'; | |
$fields->description->language[1] = 'Test product webservice Desc'; | |
$fields->state = 1; | |
$fields->show_price = 1; | |
$fields->available_for_order = 1; | |
$fields->redirect_type = '404'; | |
$fields->minmal_quantity = 1; | |
$fields->id_tax_rules_group = 1; | |
//$fields->quantity = 100; | |
$create = $webService->add([ | |
'resource' => 'products', | |
'postXml' => $xml->asXML(), | |
'id_group_shop' => 1, | |
]); | |
$product = $create->children()->children(); | |
echo 'Product created. Id: ' . $product->id; | |
} catch (PrestaShopWebserviceException $ex) { | |
echo 'Error: <br />' . $ex->getMessage(); | |
} | |
if(isset($product->id)) | |
{ | |
$urlImage = PS_SHOP_PATH . '/api/images/products/'.$product->id.'/?id_group_shop=1'; | |
$image_path = './test-image.jpg'; | |
$image_mime = 'image/jpg'; | |
$args['image'] = new CurlFile($image_path, $image_mime); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); | |
curl_setopt($ch, CURLOPT_URL, $urlImage); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $args); | |
$result = curl_exec($ch); | |
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if (200 == $httpCode) { | |
echo 'Product image was successfully created.'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment