Search code examples
opencartopencart-moduleopencart-3

Adding Tags to an extension/module


im trying to get the product tags to show on the featured module, and in fact, anywhere on the site! I've managed to get the tags to work, but for some reason, its only showing 1 tag on all products (the same tag) rather than using individual tags per item.

Each product i have has its own tag, only, the same tag is showing where i have entered this!

I have this in my featured.php controller :

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

            if ($product_info['tag']) {
                $tags = explode(',', $product_info['tag']);

                foreach ($tags as $tag) {
                    $data['tags'][] = array(
                        'tag'  => trim($tag),
                        'href' => $this->url->link('product/search', 'tag=' . trim($tag))
                    );
                }
        }

and in my featured.twig file :

{% if tags %}
    <p>{{ text_tags }}
    {% for i in 0..tags|length %}
    {% if i < (tags|length - 1) %} <a href="{{ tags[i].href }}">{{ tags[i].tag }}</a>,
    {% else %} <a href="{{ tags[i].href }}">{{ tags[i].tag }}</a> {% endif %}
    {% endfor %} </p>
    {% endif %}

Now as i said, its simply repeating the same tag for each product, rather than using each products own tags....where have i gone wrong?

Many thanks!


Solution

  • In featured.php controller, find:

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

    Replace with:

    $product_tags = array();
    if ($product_info['tag']) {
        $tags = explode(',', $product_info['tag']);
        foreach ($tags as $tag) {
            $product_tags[] = array(
                'tag'  => trim($tag),
                'href' => $this->url->link('product/search', 'tag=' . trim($tag))
            );
        }
    }
    $data['products'][] = array(
        'tags' => $product_tags,
    

    And use it in featured.twig:

    {% if product.tags %}
    <p>{% for i in 0..product.tags|length %}
    {% if i < (product.tags|length - 1) %} <a href="{{ product.tags[i].href }}">{{ product.tags[i].tag }}</a>,
    {% else %} <a href="{{ product.tags[i].href }}">{{ product.tags[i].tag }}</a> {% endif %}
    {% endfor %} </p>
    {% endif %}