我需要在prestashop 1.7中添加产品的封面图片 以下是我正在使用的代码我非常感谢您在prestashop论坛中搜索过的帮助,并且无处找到答案。
它不会添加任何图像,并且产品保持为空,因此不显示图像
<?php
include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/init.php');
$url ="https://frasesparami.com/wp-content/uploads/2016/11/Reflexion-Sobre-El-Amor.jpg";
addProduct("testProduct","impoted_product_55",12.0,1000,[6,8],6,$url);
function addProduct($p_name,$p_link_wr,$p_price,$p_quantity,$p_id_cat,$p_id_catdef,$p_img_url){
$default_lang = Configuration::get('PS_LANG_DEFAULT');
$product = new Product();
$product->name = [$default_lang => $p_name];
$product->link_rewrite = [$default_lang => $p_link_wr];
$product->price = (int)$p_price;
$product->quantity = $p_quantity;
$product->id_category = $p_id_cat;
$product->id_category_default = $p_id_catdef;
if($product->add())
{
$product->updateCategories($product->id_category);
StockAvailable::setQuantity((int)$product->id, null, $product->quantity, Context::getContext()->shop->id);
}
$id_product = $product->id;
$image = new Image();
$image->id_product = (int) $product->id;
$image->position = Image::getHighestPosition($product->id) + 1;
$image->cover = true;
$image->add();
$image->associateTo(Context::getContext()->shop->id);
if (!copyImg($product->id, $image->id, $p_img_url, 'products', true))
{
//$image->delete();
}
}
function copyImg($id_entity, $id_image, $url, $entity = 'products', $regenerate = true) {
$tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
$watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
switch ($entity) {
default:
case 'products':
$image_obj = new Image($id_image);
$path = $image_obj->getPathForCreation();
break;
case 'categories':
$path = _PS_CAT_IMG_DIR_ . (int) $id_entity;
break;
case 'manufacturers':
$path = _PS_MANU_IMG_DIR_ . (int) $id_entity;
break;
case 'suppliers':
$path = _PS_SUPP_IMG_DIR_ . (int) $id_entity;
break;
}
$url = str_replace(' ', '%20', trim($url));
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (!ImageManager::checkImageMemoryLimit($url))
return false;
// 'file_exists' doesn't work on distant file, and getimagesize makes the import slower.
// Just hide the warning, the processing will be the same.
if (Tools::copy($url, $tmpfile)) {
ImageManager::resize($tmpfile, $path . '.jpg');
$images_types = ImageType::getImagesTypes($entity);
if ($regenerate)
foreach ($images_types as $image_type) {
ImageManager::resize($tmpfile, $path . '-' . stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
if (in_array($image_type['id_image_type'], $watermark_types))
Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
}
}
else {
unlink($tmpfile);
return false;
}
unlink($tmpfile);
return true;
}
我在使用php 7.2的debian上使用apache