prestashop - 如何在 Prestashop 1.7 中将图像添加到产品对象
问题描述
我编写了一个自定义模块来创建产品,导入产品。一切都很好,但我在链接到产品图片时遇到了问题。当我在源视图中检查产品图片的链接时,当我看到商店的正面时,链接是:
http://example.com/30-home_default/Array.jpg
URL 中有一个数组,因为 prestashop 从以下位置进入:
$product->link_rewrite = $link_rewrite;
$link_rewrite 有两个索引(两种语言)。如何解决这个问题?
public function createProductsObject($productID, $productName, $price, $weight, $category, $description, $link_rewrite, $singleStock, $getCategoryID, $getImages, $attrs) {
$product = new Product;
$product->name = $productName;
$product->ean13 = '';
$product->reference = '';
$product->id_category_default = $getCategoryID;
$product->category = $getCategoryID;
$product->indexed = 1;
$product->description = $description;
$product->condition = 'new';
$product->redirect_type = '404';
$product->visibility = 'both';
$product->id_supplier = 1;
$product->link_rewrite = $link_rewrite;
$product->quantity = $singleStock;
$product->price = round($price - (18.69 / 100) * $price, 2);
$product->active = 1;
$product->psoft_hurtobergamo_id = $productID;
$product->add();
$product->addToCategories($getCategoryID);
$shops = 1;
$count = 0;
foreach ($getImages->children() AS $image) {
$url = $image->attributes()->url->__toString();
$id_product = $product->id;
$image = new Image();
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
if ($count == 0) {
$image->cover = true;
$count = 1;
} else {
$image->cover = false;
}
if (($image->validateFields(false, true)) === true &&
($image->validateFieldsLang(false, true)) === true && $image->add()) {
if (Configuration::get('PSOFT_HURTO_BERGAMO_THUMB') == '0') {
$productThumb = false;
} else {
$productThumb = true;
}
$image->associateTo($shops);
if (!self::copyImg($id_product, $image->id, $url, 'products', $productThumb)) {
$image->delete();
}
}
}
protected static function copyImg($id_entity, $id_image = null, $url, $entity = 'products', $regenerate = false) {
$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;
}
$url = urldecode(trim($url));
$parced_url = parse_url($url);
if (isset($parced_url['path'])) {
$uri = ltrim($parced_url['path'], '/');
$parts = explode('/', $uri);
foreach ($parts as &$part) {
$part = rawurlencode($part);
}
unset($part);
$parced_url['path'] = '/' . implode('/', $parts);
}
if (isset($parced_url['query'])) {
$query_parts = array();
parse_str($parced_url['query'], $query_parts);
$parced_url['query'] = http_build_query($query_parts);
}
if (!function_exists('http_build_url')) {
require_once(_PS_TOOL_DIR_ . 'http_build_url/http_build_url.php');
}
$url = http_build_url('', $parced_url);
$orig_tmpfile = $tmpfile;
if (Tools::copy($url, $tmpfile)) {
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (!ImageManager::checkImageMemoryLimit($tmpfile)) {
@unlink($tmpfile);
return false;
}
$tgt_width = $tgt_height = 0;
$src_width = $src_height = 0;
$error = 0;
ImageManager::resize($tmpfile, $path . '.jpg', null, null, 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height);
$images_types = ImageType::getImagesTypes($entity, true);
if ($regenerate) {
$previous_path = null;
$path_infos = array();
$path_infos[] = array($tgt_width, $tgt_height, $path . '.jpg');
foreach ($images_types as $image_type) {
$tmpfile = self::get_best_path($image_type['width'], $image_type['height'], $path_infos);
if (ImageManager::resize($tmpfile, $path . '-' . stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height'], 'jpg', false, $error, $tgt_width, $tgt_height, 5, $src_width, $src_height)) {
// the last image should not be added in the candidate list if it's bigger than the original image
if ($tgt_width <= $src_width && $tgt_height <= $src_height) {
$path_infos[] = array($tgt_width, $tgt_height, $path . '-' . stripslashes($image_type['name']) . '.jpg');
}
if ($entity == 'products') {
if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '.jpg')) {
unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '.jpg');
}
if (is_file(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '_' . (int) Context::getContext()->shop->id . '.jpg')) {
unlink(_PS_TMP_IMG_DIR_ . 'product_mini_' . (int) $id_entity . '_' . (int) Context::getContext()->shop->id . '.jpg');
}
}
}
if (in_array($image_type['id_image_type'], $watermark_types)) {
Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
}
}
}
} else {
@unlink($orig_tmpfile);
return false;
}
unlink($orig_tmpfile);
return true;
}
protected static function get_best_path($tgt_width, $tgt_height, $path_infos) {
$path_infos = array_reverse($path_infos);
$path = '';
foreach ($path_infos as $path_info) {
list($width, $height, $path) = $path_info;
if ($width >= $tgt_width && $height >= $tgt_height) {
return $path;
}
}
return $path;
}
我在 Netbeans 中使用调试,但我找不到解决问题的方法。
谢谢
解决方案
从产品中获取图像
<?php
//after you saved a product and you have the id (eg. product id: 1) for language 1
$p = new Product(1,false,1);
$productImages = $p->getImages(1);
设置产品图片
<?php
//we need to create a new image ObjectModel
$image = new Image();
$image->id_product = 1; //associate
$image->add(false);
请仔细看看Image
课堂
谈论oververrides
您可以通过覆盖Product
class来实现这一点。
由于您在一个模块中,因此您必须在override
模块文件夹中放置一个文件夹。
如果模块命名为示例mymodule
:
放置modules/mymodule/override/classes
一个这样的Product.php
类:
<?php
class Product extends ProductCore{
}
请注意,这些覆盖在模块安装时启用并在模块卸载时删除。
推荐阅读
- postgresql - 在 postgres 中匿名化 JSONB 的更好方法?
- c# - 总结在列表属性列表中
- reactjs - 在 useState 定义的对象中设置对象
- r - 为什么我在 R 中的摘要只包括我的一些变量?
- javascript - 使用 JavaScript 循环遍历复杂的 JSON
- python - 在 SQLAlchemy 中执行许多 dml 语句的最佳性能方式
- python - 如何使 setup.py 的 install_requires 部分跳过已安装的依赖项
- reactjs - ReactJs在TextInput中显示状态值
- jenkins - 詹金斯当条款没有按预期工作时
- assembly - 如何在颜色(在源代码中)为十六进制的 16 位汇编模拟器中显示带有彩虹渐变的对角线?