Web Development. Client Satisfaction. Web Consultancy. Professional. UX. Fast. Responsive Design. Dynamic. Interactive. Innovative. Ecommerce. Wordpress plugins. Prestashop modules. You name it.

How To Create Custom Shortcodes In Prestashop

How To Create Custom Shortcodes In Prestashop

In this tutorial I will shoud you how to create custom shortcodes in prestashop. Prestashop does not come with this type of functionality so we will need to create it from scratch. For this example I will use an admin module created from scratch too, if you don't know how to create a module you can browse this website and you will find a lot of tutorials to understand how to implement one from scratch. Anyway, you do not necessarily need an admin module to achive this functionality, you can just override the CmsController and add this functionlity in that file. I will explain all these things a little later.

All this process will go as follows: you will add in the cms page a shortcode let's say {my_shortcode:1} you don't need to use the number, but if you want to show on the page multiple items by id you will need to provide an ID for the elements. Now, in CMSController at content initialization we will get all content for that page and we will check if the shortcode was inserted in the content, if yes, we will check it and we will return our custom template. 

The following code should be placed in: override -> controllers -> front -> CmsController.php if you don't have all these files created, you will need to create them, in the cms controller file put the following code:

use PrestaShop\PrestaShop\Adapter\ObjectPresenter;
class CmsController extends CmsControllerCore{
public function initContent(){
    parent::initContent(); 
    if($this->assignCase == 1){
      if($this->returnContent($this->cms->content) !== false){
        $this->cms->content = $this->returnContent($this->cms->content);
        $this->context->smarty->assign(array(
                  'cms' => $this->objectPresenter->present($this->cms),
              ));
        $this->setTemplate('cms/page', array(
                    'entity' => 'cms',
                    'id' => $this->cms->id
                ));
      }
    } 
  }
    public function returnContent($contents){
    preg_match_all('/\{my_shortcode\:[(0-9\,)]+\}/i', $contents, $matches);
    if(!empty($matches[0])){
      foreach($matches[0] as $k => $v){
        $explode = explode(":", $v);
        $shortcode_id = str_replace('}', '', $explode[1]);
      }
      if($shortcode_id){
        $contents = $this->context->smarty->fetch('my_file.tpl');
        
        return $contents;
      }
    }else{
      return false;
    }
  }
}

This function will check when you open the page if the shortcode is in the content object, if it's there the returnContent will run and it will return all you have in your file template. By $this->assignCase we are checking if the current page is CMS page. 1 = cms page 2 = category. Using objectPresenter() we will repopulate de CMS object with our details. Now the only thing you need to do is to create your own files for the template and happy coding. Let me know if you have any questions to achive a shortcode in prestashop in the comments section and I will try to help you implementing this functionality.

Leave a Reply

avatar
  Subscribe  
Notify of