我正在使用Prestashop。在类别页面上为产品提取所有图像时遇到问题。例如: -如何在类别页面上获取产品的所有图像 - Prestashop
我有一个名为“abc”的产品。 这个产品有3幅图像。一个是封面图片和另外两张图片。我想在类别页面上显示所有三个图像,目前它只显示封面图像。
我正在使用Prestashop。在类别页面上为产品提取所有图像时遇到问题。例如: -如何在类别页面上获取产品的所有图像 - Prestashop
我有一个名为“abc”的产品。 这个产品有3幅图像。一个是封面图片和另外两张图片。我想在类别页面上显示所有三个图像,目前它只显示封面图像。
使用类。在prestashop文件夹中,有一个名为override的文件夹。创建一个PHP文件,并尝试使用类覆盖来做到这一点。
class CategoryController extends CategoryControllerCore
{
function __construct() {
parent::__construct();
}
public function process()
{
parent::process();
$productImages = array();
$newImages = array();
foreach($this->cat_products as $product)
{
$new_product = new ProductCore($product['id_product']);
$images = $new_product->getImages((int)self::$cookie->id_lang);
foreach ($images AS $k => $image)
{
$productImages[(int)$image['id_image']] = $image;
}
$newImages[$product['id_product']] = $productImages;
$productImages = null;
$new_product = null;
}
if (count($newImages))
self::$smarty->assign('images', $newImages);
}
}
虽然我在我的一个客户项目中解决问题。我也想在产品列表页面上显示所有图像。
我使用@Ravi库马尔回答代码,并与负责$图像变量在模板文件中像这样工作
<ul id="thumbs_list_frame">
{if isset($images[$product.id_product])}
{foreach from=$images[$product.id_product] item=image name=thumbnails}
{assign var=imageIds value="`$product.id_product`-`$image.id_image`"}
<li id="thumbnail_{$image.id_image}">
<img id="thumb_{$image.id_image}" src="{$link->getImageLink($product.link_rewrite, $imageIds, 'large_default')}" alt="{$image.legend|htmlspecialchars}" alt="" title="{$product->name|escape:'htmlall':'UTF-8'}" width="{$largeSize.width}" height="{$largeSize.height}" />
</li>
{/foreach}
{/if}
</ul>
下面我写我的功能写在categoryController.php文件
/**
* Assign list of products template vars
*/
public function assignProductList()
{
$hookExecuted = false;
Hook::exec('actionProductListOverride', array(
'nbProducts' => &$this->nbProducts,
'catProducts' => &$this->cat_products,
'hookExecuted' => &$hookExecuted,
));
// The hook was not executed, standard working
if (!$hookExecuted)
{
$this->context->smarty->assign('categoryNameComplement', '');
$this->nbProducts = $this->category->getProducts(null, null, null, $this->orderBy, $this->orderWay, true);
$this->pagination((int)$this->nbProducts); // Pagination must be call after "getProducts"
$this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay);
}
// Hook executed, use the override
else
// Pagination must be call after "getProducts"
$this->pagination($this->nbProducts);
$productImages = array();
$newImages = array();
foreach ($this->cat_products as &$product)
{
if ($product['id_product_attribute'] && isset($product['product_attribute_minimal_quantity']))
$product['minimal_quantity'] = $product['product_attribute_minimal_quantity'];
$new_product = new ProductCore($product['id_product']);
$images = $new_product->getImages((int)self::$cookie->id_lang);
foreach ($images AS $k => $image)
{
$productImages[(int)$image['id_image']] = $image;
}
$newImages[$product['id_product']] = $productImages;
$productImages = null;
$new_product = null;
}
if (count($newImages))
self::$smarty->assign('images', $newImages);
$this->context->smarty->assign('nb_products', $this->nbProducts);
}
好吧,这似乎是一个非常普遍的问题,新prestashop和处理相同的问题,我发现了很多类似的请求在线和没有工作的解决方案,所以这就是我如何得到它的工作:
第一:覆盖产品类,创建/override/classes/Product.php
class Product extends ProductCore {
/**
* @param string $id_product ID of the product to fetch
* @param bool $exclude_cover Whether to remove or not the cover from the returned list
* @return array List of the product images
* @throws PrestaShopDatabaseException
*/
public static function getProductImagesID($id_product, $exclude_cover = false) {
$id_image = Db::getInstance()->executeS('SELECT `id_image` FROM `' . _DB_PREFIX_ . 'image` WHERE `id_product` = ' . (int)($id_product) . ($exclude_cover ? ' AND `cover` IS NULL' : '') . ' ORDER BY position ASC');
return $id_image;
}
}
二:使用它作为您在任何.tpl文件希望
...
{assign var='productImgs' value=Product::getProductImagesID($product.id_product,true)}
{* now you have an array of product images called $productImgs *}
{* eg. show the first image of the product that is not saved as cover *}
<img class="..." src="{$link->getImageLink($product.link_rewrite, $productImgs[0]['id_image'], 'home_default')|escape:'html':'UTF-8'}" alt="..."/>
完!
奖励,从控制器文件访问数据库应该是有意义的,但覆盖类应该为您节省平台升级。
请记住删除缓存文件/cache/class_index.php并禁用/启用所有prestashop缓存系统。 希望它可以帮助别人。
我编辑了上面的答案,特别是聪明的代码。因为在启用URL重写时它正在创建问题。 得到图像路径代码是'$ link-> getImageLink($ name,$ ids,$ type = NULL)' 最后我得到了图像路径'src =“{$ link-> getImageLink($ product.link_rewrite, $ imageIds,'large_default')}“' – 2013-11-09 07:03:54