【问题标题】:Opencart 3 - display category(linked to) on product pageOpencart 3 - 在产品页面上显示类别(链接到)
【发布时间】:2019-02-17 21:15:35
【问题描述】:

我需要在产品页面上显示类别链接。

我在forum.opencart.com找到了以下xml:

<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>Show Linked Categories on Product page</id>
<version>1</version>
<vqmver>2.5.1</vqmver>
<author>http://www.opencart-extensions.co.uk</author>

<file name="catalog/controller/product/product.php">
<operation error="log">
<search position="after" index="1"><![CDATA[$product_info = $this-    >model_catalog_product->getProduct($product_id);]]></search>
<add><![CDATA[
$data['text_linked_categories'] = $this->language->get('text_linked_categories');
$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 position="before"><![CDATA[$data['manufacturer'] =     $product_info['manufacturer'];]]></search>
<add><![CDATA[
$data['linked_categories'] = $linked_categories;
]]></add>
</operation>
</file>

<file name="catalog/language/*/product/product.php">
<operation error="log">
<search position="before"><![CDATA[$_['text_manufacturer']]]></search>
<add><![CDATA[
$_['text_linked_categories']        = 'Categories:';
]]></add>
</operation>
</file>

<file name="catalog/view/theme/*/template/product/product.tpl">
<operation error="log">
<search position="before"><![CDATA[<li><?php echo $text_model; ?> <?php     echo $model; ?></li>]]></search>
<add><![CDATA[
<?php if( $linked_categories ){ ?>
<li><?php echo $text_linked_categories; ?> 
<?php foreach( $linked_categories as $linked_category ){ ?>
<a href="<?php echo $linked_category['href']; ?>"><?php echo         $linked_category['name']; ?></a>&nbsp;
<?php } ?>
</li>
<?php } ?>
]]></add>
</operation>
</file>
</modification>

问题是,这个 xml vqmod 只适用于版本 2。

如何将此解决方案应用于 Opencart 3?

【问题讨论】:

  • 是的,因为 opencat 3 与 2.0 相比有不同的模板语言,你需要修改它以使其工作

标签: php twig opencart


【解决方案1】:

所以它非常简单.. 你只需要记住,在 OC 版本 3 中,tpl 必须是 twig 文件。 所以我在 OCMOD 中重写了 OC 版本 3.0.2 的代码。请勿在 VQMOD 中使用此代码。

<?xml version="1.0" encoding="UTF-8"?>
<modification>
<id>Show Linked Categories on Product page OC 3.0.x</id>
<code>00002</code>
<name>Show Linked Categories on Product page OC 3.0.x</name>
<version>1.0.0</version>
<vqmver>2.6.2</vqmver>
<author>K.B.</author>

<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[
$data['text_linked_categories'] = $this->language->get('text_linked_categories');
$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'];]]></search>
<add position="before"><![CDATA[
$data['linked_categories'] = $linked_categories;
]]></add>
</operation>
</file>

<file path="catalog/language/*/product/product.php">
<operation error="log">
<search><![CDATA[$_['text_manufacturer']]]></search>
<add position="before"><![CDATA[
$_['text_linked_categories']        = 'Categories:';
]]></add>
</operation>
</file>

<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 %}
  <li>{{ text_linked_categories }} 
  {% for linked_category in linked_categories %}
            <a href="{{ linked_category.href }}">{{ linked_category.name }}</a>&nbsp;
            {% endfor %}
            </li>
            {% endif %}

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

创建名称为install.xml 的新文本文件,将上面的代码复制到其中。使用新名称categories_to_product.ocmod.zip 将其压缩并使用 OC 安装程序进行安装。之后不要忘记刷新修改...

祝你好运

【讨论】:

  • 谢谢你完美的工作!! K.B. 拯救了这一天
  • 嗨,K.B.我只想显示产品所在的最后一个子类别。能否请您提供建议/帮助。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多