在产品页面上显示最新的子类别-OpenCart [英] Show latest Child category on Product Page - Opencart
本文介绍了在产品页面上显示最新的子类别-OpenCart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了这段代码,并为Journal3主题修改了它,它起作用了。 但现在它显示了所有父类别和子类别。我只想看到产品所在的最新儿童类别。 有人能帮我调整一下代码吗?在本例中,我只需要最后一类(筹码)
版本:3.0.3.7 日记账:3.1.8
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<code>Show_Linked_Categories_on_Product_page</code>
<name>Show Linked Categories on Product page</name>
<version>1.0.0</version>
<author></author>
<link></link>
<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/nl-nl/product/product.php">
<operation error="log">
<search><![CDATA[$_['text_manufacturer']]]></search>
<add position="before"><![CDATA[
$_['text_linked_categories'] = 'Categorie:';
]]></add>
</operation>
</file>
<file path="catalog/view/theme/journal3/template/product/product.twig">
<operation error="log">
<search><![CDATA[<li class="product-sku"><b>{{ j3.settings.get(stylePrefix ~ 'ProductSKUText') }}:</b> <span> {{ product_sku }}</span></li>]]></search>
<add position="before"><![CDATA[
{% if linked_categories %}
<li class="product-sku"><b>{{ text_linked_categories }}</b>
{% for linked_category in linked_categories %}
<span><a href="{{ linked_category.href }}">{{ linked_category.name }}</a></span>;
{% endfor %}
</li>
{% endif %}
]]></add>
</operation>
</file>
</modification>
我最终得到了以下工作代码:
<?xml version="1.0" encoding="UTF-8"?>
<modification>
<code></code>
<name></name>
<version></version>
<ocver></ocver>
<author></author>
<date></date>
<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="0">
<![CDATA[
$data['text_linked_category'] = $this->language->get( 'text_linked_category' );
$data['linked_category_info'] = [];
$linked_categories = $this->model_catalog_product->getCategories($product_id);
$last_category = array_pop( $linked_categories );
if( $target_category = $this->model_catalog_category->getCategory( $last_category['category_id'] ) ) {
$data['linked_category_info'] = [
'id' => $target_category['category_id'],
'href' => $this->url->link( 'product/category', 'path=' . $target_category['category_id'], true ),
'name' => $target_category['name']
];
}
]]>
</add>
</operation>
</file>
<file path="catalog/language/nl-nl/product/product.php">
<operation error="log">
<search><![CDATA[$_['text_manufacturer']]]></search>
<add position="before"><![CDATA[$_['text_linked_category'] = 'Categorie:';]]></add>
</operation>
</file>
<file path="catalog/view/theme/journal3/template/product/product.twig">
<operation error="log">
<search><![CDATA[<li class="product-sku"><b>{{ j3.settings.get(stylePrefix ~ 'ProductSKUText') }}:</b> <span> {{ product_sku }}</span></li>]]></search>
<add position="before">
<![CDATA[
{% if linked_category_info %}
<li class="product-sku"><b>{{ text_linked_category }}</b>
<span><a href="{{ linked_category_info.href }}" style="text-decoration: none">{{ linked_category_info.name }}</a></span>
</li>
{% endif %}
]]>
</add>
</operation>
</file>
</modification>
推荐答案
替换您的xml文件中的以下代码:
<modification>
<code>Show_Linked_Categories_on_Product_page</code>
<name>Show Linked Categories on Product page</name>
<version>1.0.0</version>
<author></author>
<link></link>
<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);
//echo '<pre>'; print_r($query_linked_categories); echo '</pre>';
$linked_categories = array();
foreach( $query_linked_categories as $linked_category_data ) {
$linked_category = $this->model_catalog_category->getCategory($linked_category_data['category_id']);
//echo '<pre>'; print_r($linked_category); echo '</pre>';
$linked_category_info['id'] = $linked_category_data['category_id'];
$linked_category_info['href'] = $this->url->link('product/category', 'path=' . $linked_category_data['category_id']);
// CALL custom Function getCategoryName HERE
$linked_category_info['name'] = $this->model_catalog_product->getCategoryName($linked_category_data['category_id']);
$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;
echo '<pre>'; print_r($data['linked_categories']); echo '</pre>';
]]></add>
</operation>
</file>
<file path="catalog/model/catalog/product.php">
<operation error="log">
<search><![CDATA[public function getProducts($data = array()) {]]></search>
<add position="before"><![CDATA[public function getCategoryName($category_id) {
$query = $this->db->query("SELECT GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' > ') AS name FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category c1 ON (cp.category_id = c1.category_id) LEFT JOIN " . DB_PREFIX . "category c2 ON (cp.path_id = c2.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (cp.path_id = cd1.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id) WHERE cp.category_id = '" . (int)$category_id . "'");
return $query->row['name'];}]]></add>
</operation>
</file>
<file path="catalog/language/nl-nl/product/product.php">
<operation error="log">
<search><![CDATA[$_['text_manufacturer']]]></search>
<add position="before"><![CDATA[
$_['text_linked_categories'] = 'Categorie:';
]]></add>
</operation>
</file>
<file path="catalog/view/theme/journal3/template/product/product.twig">
<operation error="log">
<search><![CDATA[<li class="product-sku"><b>{{ j3.settings.get(stylePrefix ~ 'ProductSKUText') }}:</b> <span> {{ product_sku }}</span></li>]]></search>
<add position="before"><![CDATA[
{% if linked_categories %}
<li class=""><b>{{ text_linked_categories }}</b>
{% for linked_category in linked_categories %}
<span><a href="{{ linked_category.href }}">{{ linked_category.name }}</a></span>;
{% endfor %}
</li>
{% endif %}
]]></add>
</operation>
</file>
</modification>
这篇关于在产品页面上显示最新的子类别-OpenCart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文