Jump to content

Edit History

Holoweb

Holoweb


I figured out the solution

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;
}

 

Holoweb

Holoweb

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!

×
×
  • Create New...