How to display images of categories on the product page to which it belongs Opencart 3

杀马特。学长 韩版系。学妹 提交于 2020-08-08 09:35:19

问题


Please tell me how to display images of the categories to which the product belongs on the product page. I brought out the name and link to the categories, but with the image it does not work. I can't understand what and where to insert to display category images. I did this (in OCMOD file):

<file path="catalog/controller/product/product.php">
<operation error="log">
<search><![CDATA[$product_info = $this->model_catalog_product->getProduct($product_id);]]></search>
<add position="after" index="1"><![CDATA[
$query_linked_categories = $this->model_catalog_product->getCategories($product_id);
$linked_categories = array();
foreach( $query_linked_categories as $linked_category_data ) {
$linked_category = $this->model_catalog_category->getCategory($linked_category_data['category_id']);
$linked_category_info['id'] = $linked_category_data['category_id'];
$linked_category_info['href'] = $this->url->link('product/category', 'path=' . $linked_category_data['category_id']);
$linked_category_info['name'] = $linked_category['name'];
$linked_categories[] = $linked_category_info;
}
]]></add>
</operation>

<operation error="log">
<search><![CDATA[$data['manufacturer'] = $product_info['manufacturer_name'];]]></search>
<add position="before"><![CDATA[
$data['linked_categories'] = $linked_categories;
]]></add>
</operation>

<file path="catalog/view/theme/*/template/product/product.twig">
<operation error="log">
<search><![CDATA[<li>{{ text_model }} {{ model }}</li>]]></search>
<add position="before"><![CDATA[
{% if linked_categories %}
           <div class="uni-module product-linked_categories">
            <div class="category-list row row-flex">
               {% for linked_category in linked_categories %}
               <div>
                  <a href="{{ linked_category.href }}" class="category-list__item image-after">
                            {% if linked_category.thumb %}
                                <img src="{{ linked_category.thumb }}" alt="{{ linked_category.name }}" title="{{ linked_category.name }}" class="category-list__img img-responsive" />
                            {% endif %}
                            {{ linked_category.name }}
                  </a>
               </div>
               {% endfor %}                
               </div>
            </div>
            <script>
                $('.product-linked_categories').uniModules({
                    type:'{{type_view is defined ? type_view : 'carousel'}}',
                    loop: {{linked_categories|length > 4 ? 'true' : 'false'}}
                });
            </script>
        {% endif %}

]]></add>
</operation>
</file>

回答1:


You miss the code to get images in your controller. Now you OCMOD file looks like this:

<file path="catalog/controller/product/product.php">
<operation error="log">
<search><![CDATA[$product_info = $this->model_catalog_product->getProduct($product_id);]]></search>
<add position="after" index="1"><![CDATA[
        $query_linked_categories = $this->model_catalog_product->getCategories($product_id);
        $linked_categories = array();
        foreach( $query_linked_categories as $linked_category_data ) {
            $linked_category = $this->model_catalog_category->getCategory($linked_category_data['category_id']);
            $linked_category_info['id'] = $linked_category_data['category_id'];
            $linked_category_info['href'] = $this->url->link('product/category', 'path=' . $linked_category_data['category_id']);
            $linked_category_info['name'] = $linked_category['name'];
            // added this part for thumbs
            if ($linked_category['image']) {
                $linked_category_info['thumb'] = $this->model_tool_image->resize($linked_category['image'], $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_width'), $this->config->get('theme_' . $this->config->get('config_theme') . '_image_category_height'));
            } else {
                $linked_category_info['thumb'] = '';
            }
            //
            $linked_categories[] = $linked_category_info;
        }
]]></add>
</operation>

<operation error="log">
<search><![CDATA[$data['manufacturer'] = $product_info['manufacturer_name'];]]></search>
<add position="before"><![CDATA[
$data['linked_categories'] = $linked_categories;
]]></add>
</operation>

<file path="catalog/view/theme/*/template/product/product.twig">
<operation error="log">
<search><![CDATA[<li>{{ text_model }} {{ model }}</li>]]></search>
<add position="before"><![CDATA[
{% if linked_categories %}
           <div class="uni-module product-linked_categories">
            <div class="category-list row row-flex">
               {% for linked_category in linked_categories %}
               <div>
                  <a href="{{ linked_category.href }}" class="category-list__item image-after">
                            {% if linked_category.thumb %}
                                <img src="{{ linked_category.thumb }}" alt="{{ linked_category.name }}" title="{{ linked_category.name }}" class="category-list__img img-responsive" />
                            {% endif %}
                            {{ linked_category.name }}
                  </a>
               </div>
               {% endfor %}                
               </div>
            </div>
            <script>
                $('.product-linked_categories').uniModules({
                    type:'{{type_view is defined ? type_view : 'carousel'}}',
                    loop: {{linked_categories|length > 4 ? 'true' : 'false'}}
                });
            </script>
 {% endif %}

]]></add>
</operation>
</file>


来源:https://stackoverflow.com/questions/63097066/how-to-display-images-of-categories-on-the-product-page-to-which-it-belongs-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!