Jump to content

Dynamic links on CMS pages and in product descriptions


Holoweb

Recommended Posts

Hello. PS 1.7.8.1.

Im searching for a way how to use dynamic links over my website. Im adding links on CMS pages and in product description for other products within text and i have lot of that in CMS pages too. I have 7 languages on my website and its impossible to keep track of every link, so whenever i rename some page, or change the structure a little, lot of these links all over my website breaks.

I need a way to use {$link->getCategoryLink('6')}, or simial stuff that are used in TPL file on CMS pages and category descriptions. I was not able to find any solution for this, even tho some threads with this question already exists.

I have some experience, so i can move around files and code a little. Any help will be greatly appreciated. I would be happy if you know a modul that do that, im prety sure im not the onlůy one who needs this.

Thank you!

Hugo

Link to comment
Share on other sites

20 hours ago, Olivier CLEMENCE said:

Hi Holoweb,

where did you find other topic about this feature please ?

I just googled but its all 10+ years old and not relatable.

 

20 hours ago, ps8moduly.cz said:
{url entity='category' id=6 id_lang=Context::getContext()->language->id}

{url entity='cms' id=5 id_lang=Context::getContext()->language->id}

 

Well thanks, but this is not working. This would work in .TPL files, but i search for something what will work in CMS pages, which are populated with HTML

Link to comment
Share on other sites

2 hours ago, ps8moduly.cz said:

And will you show me how you now put it in the CMS pages?

It works, but you have to put it in the source code, the <> icon. Furthermore, HTML Purifier must be disabled.

The code is HTML. Its feeded via Elementor to the CMS pages and i need that to stay, since my employees know nothing about coding. I need the to fill "URL" fields with somethig what takes ID of the product / cms / category etc. and populate with link in certain language. Adding anything to source code is not a solution for us, that i know how to do, but that is not what im looking for.

Elementor fills only the "" part of the link, so when i add the code, i get:

<a href="{url entity='category' id=6 id_lang=Context::getContext()->language->id}" class="elementor-button-link elementor-button btn elementor-size-large btn-secondary btn-block">CONTENT</a>

Front end generates 404 obv with this. I tried add <> but i generated also 404.

Thank you very much for your time!

 

Link to comment
Share on other sites

I'm sorry, if you wrote that it was an elementor, I wouldn't have responded at all.

A lot of people get ripped off by buying warehouse, leo or tjemeforesz sabl8ny with elementor or other page builders and they think they have everything solved by doing click click click and they think they are on top of everything.

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

9 minutes ago, ps8moduly.cz said:

I'm sorry, if you wrote that it was an elementor, I wouldn't have responded at all.

A lot of people get ripped off by buying warehouse, leo or tjemeforesz sabl8ny with elementor or other page builders and they think they have everything solved by doing click click click and they think they are on top of everything.

Thank you i guess. Not sure where i said im on top of everything tho. I was using classic PS editor for CMS pages from 2016 to late 2020, writing all HTML myself. Now i have Elementor and it actually saves lot of time and i can make other people do it, bcs its easy. Dont feel the ripoff here, dynamic links were not possible in the basic HTML editor for CMS pages even before as far as i know and i dont need to have employees who know any sort of coding. Looks like win win to me.

Link to comment
Share on other sites

hi there, i have a solution could help you. in your text editor (Cms or product description) write links like that <a href="{$link->getCategoryLink('6')}">category</a> and go to ProductController.php and CmsController.php and before the controller return the data to the tpl file, you will do some script that will get the id of the category, in our example its 6 and when you get the id then get the category link by php then replace {$link->getCategoryLink('6')} with the link you got and probleme solved.

you just need the script to search and replace the links

Link to comment
Share on other sites

Using dynamic links for CMS pages is very simple and has been possible for a long time.
for Prestashop 1.7,
just write an override for ./override/controllers/front/CmsController.php.
In the HTML editor, you just need to write, for example,

to insert the product [product:5:detail], or to insert the product name and URL link [product:5:link] and overwrite the content in the controller.

E.g.: sample for [product:5:link]

class CmsController extends CmsControllerCore
{
    public function initContent()
    {
        $this->cms->content = $this->returnShortcodeContent($this->cms->content);
        parent::initContent();
    }

    public function getBetween($content, $start, $end) {
        $n = explode($start, $content);
        $result = Array();
        foreach ($n as $val) {
            $pos = strpos($val, $end);
            if ($pos !== false) {
                $result[] = substr($val, 0, $pos);
            }
        }
        return $result;
    }

    public function returnShortcodeContent($contents)
    {
        $db = Db::getInstance();
        $productWithLink = $this->getBetween($contents,'[product:', ':link]');

        foreach ($productWithLink as $match) {
            $productId = $match;
            $product = new Product((int)$productId, false, $this->context->language->id);
            $productName = $product->name;
            $productLink =  $this->context->link->getProductLink($product);
            $replace = '<a href="'.$productLink.'">'.$productName.'</a>';
            $contents = str_replace('[product:'.$productId.':link]', $replace, $contents);
        }

        return $contents;

    }
}

 

Admin CMS page:

obrazek.png.480b83f5c37a350d4e69a9a2d2e756c4.png

Front:

obrazek.thumb.png.4b994c636db168eb076c2f4a346acfb0.png

  • Thanks 1
Link to comment
Share on other sites

Thank you! That worked! So, can i kindly ask how to modify the override to be able to do the same with categories? From CMS pages, i mostly link categories. Thank you very much!

 

EDIT: I firured out. This code will add this functionality of dynamic links for both product, and categories by ID for CMS pages. Big thanks to @ps8moduly.cz

public function initContent()
{
    $this->cms->content = $this->returnShortcodeContent($this->cms->content);
    parent::initContent();
}

public function getBetween($content, $start, $end) {
    $n = explode($start, $content);
    $result = Array();
    foreach ($n as $val) {
        $pos = strpos($val, $end);
        if ($pos !== false) {
            $result[] = substr($val, 0, $pos);
        }
    }
    return $result;
}

public function returnShortcodeContent($contents)
{
    $db = Db::getInstance();
    $productWithLink = $this->getBetween($contents,'[product:', ':link]');
    $categoryWithLink = $this->getBetween($contents,'[category:', ':link]');

    foreach ($productWithLink as $match) {
        $productId = $match;
        $product = new Product((int)$productId, false, $this->context->language->id);
        $productName = $product->name;
        $productLink =  $this->context->link->getProductLink($product);
        $replace = '<a href="'.$productLink.'">'.$productName.'</a>';
        $contents = str_replace('[product:'.$productId.':link]', $replace, $contents);
    }

    foreach ($categoryWithLink as $match) {
        $categoryId = $match;
        $category = new Category((int)$categoryId, $this->context->language->id);
        $categoryName = $category->name;
        $categoryLink =  $this->context->link->getCategoryLink($category);
        $replace = '<a href="'.$categoryLink.'">'.$categoryName.'</a>';
        $contents = str_replace('[category:'.$categoryId.':link]', $replace, $contents);
    }

    return $contents;
}

 

Edited by Holoweb
I figured out the solution (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...