Opencart v3将其他产品图像添加到特色模块中

时间:2018-01-01 21:55:09

标签: php opencart opencart-module opencart-3

我在精选模块中显示的每个产品都有一个弹出窗口,在弹出窗口中我想显示产品页面看起来与产品页面相似的产品的所有图像,即1张大图片,其中包含下面附加图片的缩略图,更新大图片时点击。

为此,我将一些代码从控制器product.php复制到控制器模块featured.php,但这似乎不起作用。这是我的代码:

控制器/扩展/ module / featured.php

if ($product_info) {
                if ($product_info['image']) {
                    $image = $this->model_tool_image->resize($product_info['image'], $setting['width'], $setting['height']);
                } else {
                    $image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
                }

                if ($product_info['image']) {
                    $data['popup'] = $this->model_tool_image->resize($product_info['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_height'));
                } else {
                    $data['popup'] = '';
                }

                $data['images'] = array();

                $results = $this->model_catalog_product->getProductImages($this->model_catalog_product->getProduct($product_id));

                foreach ($results as $result) {
                    $data['images'][] = array(
                        'popup' => $this->model_tool_image->resize($result['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_height')),
                        'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_additional_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_additional_height'))
                    );
                }
                // some other code in between

                $data['products'][] = array(
                    'product_id'  => $product_info['product_id'],
                    'thumb'       => $image,
                    'popup'       => $image,
                    'name'        => $product_info['name'],
                    'description' => utf8_substr(strip_tags(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_product_description_length')) . '..',
                    'price'       => $price,
                    'special'     => $special,
                    'tax'         => $tax,
                    'rating'      => $rating,
                    'href'        => $this->url->link('product/product', 'product_id=' . $product_info['product_id'])
                );

对于view / Theme / extention / module / featured.twig

{% if thumb or images %}
      <ul class="thumbnails">
        {% if thumb %}
        <li><a class="thumbnail" href="{{ popup }}" title="{{ heading_title }}"><img src="{{ thumb }}" title="{{ heading_title }}" alt="{{ heading_title }}" /></a></li>
        {% endif %}
        {% if images %}
        {% for image in images %}
        <li class="image-additional"><a class="thumbnail" href="{{ image.popup }}" title="{{ heading_title }}"> <img src="{{ image.thumb }}" title="{{ heading_title }}" alt="{{ heading_title }}" /></a></li>
        {% endfor %}
        {% endif %}
      </ul>
      {% endif %}

1 个答案:

答案 0 :(得分:0)

您使用的代码适用于单个产品,您需要对其进行编辑以使用多个产品。

文件:

catalog\controller\extension\module\featured.php

<强>查找

$data['products'][] = array(

替换为:

$images = array();
$results = $this->model_catalog_product->getProductImages($product_info['product_id']);
foreach ($results as $result) {
    $images[] = array(
        'popup' => $this->model_tool_image->resize($result['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_popup_height')),
        'thumb' => $this->model_tool_image->resize($result['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_additional_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_additional_height'))
    );
}
$data['products'][] = array(
    'images'      => $images,

文件:

catalog\view\theme\default\template\extension\module\featured.twig

在for循环中,添加:

{% if product.images %}
<ul class="list-inline list-unstyled thumbnails">
{% for image in product.images %}
<li class="image-additional"><a class="thumbnail" href="{{ image.popup }}" title="{{ product.name }}"> <img src="{{ image.thumb }}" title="{{ product.name }}" alt="{{ product.name }}" /></a></li>
{% endfor %}
</ul>
{% endif %}

如果你想在上面的控制器文件中启用gallery(magnific-popup),在foreach循环之外的某个地方,添加:

$this->document->addScript('catalog/view/javascript/jquery/magnific/jquery.magnific-popup.min.js');
$this->document->addStyle('catalog/view/javascript/jquery/magnific/magnific-popup.css');

在视图文件中,在for循环之外的某处,添加:

<script>
$(document).ready(function() {
    $('.thumbnails').magnificPopup({
        type:'image',
        delegate: 'a',
        gallery: {
            enabled: true
        }
    });
});
</script>