Jump to content

[SOLVED] How to use variables in CMS pages


ledaanbieder

Recommended Posts

Does anybody know how to insert variables (non-html) code into the CMS pages? We are running a multishop with shared CMS-pages, but we want our sitename to be variable in these pages. One of the the variables should be $SHOP_NAME in a standaard CMS page with the privacy policy. Pages like these often use the shop name many times. <?php .... ?> will not work because of the HTML output... Who can help us? Arjan 't Hart webmaster LEDaanbieder.nl (http://www.ledaanbieder.nl)

cms.tiff

Link to comment
Share on other sites

How if using jquery ?
inside the CMS content you just have to write an unique html element e.g :

... cms content ... <span class="store_name"></span> ... continue cms content

Then with jquery, you can change the html element with the store name.

var storeName = '{$shop_name}';
$('.store_name').text(storeName);

You can also use jquery html(), append(), prepend(), after(), before(), etc :)

But remember, you should create an unique html element with unique css class and make sure no other html element using this css class.

Link to comment
Share on other sites

As far as I know, it's not possible, nor is it sensible, to do this all from the CMS page.


You're best bet would be to create widget. Widgets are reusable template tags that are very similar to the blocks used in the Magento layout system.


Once your widget is made, you can then call the widget in the CMS page with a product option,[spam-filter]widget type="mywidgets/productname" product_id="1"[spam-filter], and modify the widget's output based on the product id entered.


The tutorial I've linked to is very good, and should be a great starting point.


Link to comment
Share on other sites

well

in my opinion gonebdg solution will work, but it is a bit "tricky". 

if i were you i will create some modification, for example:

 

in cms controller add function to replace cms contents.

 

$cms->content=str_replace("{MY_VARIABLE}",SHOP_NAME,"$cms->content");

 

 

then in cms editor just use {MY_VARIABLE} and it will be changed automatically to value defined in str_replace function

 

 

you can define as many aliases as you want 

Link to comment
Share on other sites

Thanks Vekia!

 

Do you mean change CmsController.php and add this function?

 

 

/**
* Add function to use {my_variable} in CMS content
*/
public function replaceContent()
{
$cms->content=str_replace("{MY_VARIABLE}",SHOP_NAME,"$cms->content");
}
 
And where do I have to define this function to work like you described?
 
Thanks in advance!
Edited by ledaanbieder (see edit history)
Link to comment
Share on other sites

hello

 

open CmsController.php file, you've got there function: public function initContent()

 

at the end of this function you've got:

$this->setTemplate(_PS_THEME_DIR_.'cms.tpl');

right before this code add this:

if (isset($this->cms)){
    $this->cms->content=str_replace("{MY_VARIABLE}",configuration::get('PS_SHOP_NAME'),"{$this->cms->content}");
}

then in cms edit page use {MY_VARIABLE} like i show below:

MiOJX42.png

 

 

 

effect in front office:

NhY9q9I.png

 

*my shop is named "Test Shop"

  • Like 3
Link to comment
Share on other sites

  • 4 weeks later...

I don't want to be a pain in the ass Vekia,

But why suggest changing the core controller instead of replacing the core controller's function with an override controller?

E.g. placing the complete initContent function in root/override/controllers/front/CmsController.php and include the extra str_replace code there.

 

That way, an update of the PS software won't  :ph34r: up the changes made.

 

P.S.

Great solution by the way.

Link to comment
Share on other sites

  • 3 months later...

I don't want to be a pain in the ass Vekia,

But why suggest changing the core controller instead of replacing the core controller's function with an override controller?

E.g. placing the complete initContent function in root/override/controllers/front/CmsController.php and include the extra str_replace code there.

 

That way, an update of the PS software won't  :ph34r: up the changes made.

 

P.S.

Great solution by the way.

 

Could you share an override file for prestashop 1.4.11 ?

Link to comment
Share on other sites

Well done Vekia. This is exactly what I'm looking for, with one difference.

 

I want to replace links in my CMS pages with a path to the base folder. I'm having a small issue in that the link is ending up like this:

 

http://www.mysite.com/gb/content/{MY_VARIABLE}/6-food#/categories-king_edward_potatoes

 

My code looks like this:

 

            $this->cms->content=str_replace("{MY_VARIABLE}",configuration::get('PS_BASE_DIR'),"{$this->cms->content}");
 

Any ideas?

 

Thank you.

Edited by prestatent (see edit history)
Link to comment
Share on other sites

Thanks Vekia

 

Do I not have to use MY_VARIABLE in the code?

 

What I'm looking to achieve is a link like this:

 

http://www.mysite.com/gb/6-food#/categories-king_edward_potatoes

 

but what is appearing is a link like this:

 

http://www.mysite.com/gb/content/{MY_VARIABLE}/6-food#/categories-king_edward_potatoes

Edited by prestatent (see edit history)
Link to comment
Share on other sites

Prestashop 1.5.4.1

 

Hi Vekia

 

I created an override for the CMS Controller file, based on your code, as follows:

 

class CmsController extends CmsControllerCore
{
    public function initContent()
    {
        parent::initContent();

        if (isset($this->cms))
        {
            $this->cms->content = str_replace("{MY_VARIABLE}", '../', "{$this->cms->content}");
        }

        $this->setTemplate(_PS_THEME_DIR_.'cms.tpl');
    }
}
 

 

This fix resolved linking from CMS pages to product pages.

 

Thank you once again for your help.

Link to comment
Share on other sites

Prestashop 1.5.4.1

 

Hi Vekia

 

I created an override for the CMS Controller file, based on your code, as follows:

 

class CmsController extends CmsControllerCore

{

    public function initContent()

    {

        parent::initContent();

 

        if (isset($this->cms))

        {

            $this->cms->content = str_replace("{MY_VARIABLE}", '../', "{$this->cms->content}");

        }

 

        $this->setTemplate(_PS_THEME_DIR_.'cms.tpl');

    }

}

 

 

This fix resolved linking from CMS pages to product pages.

 

Thank you once again for your help.

Will this override work in PS 1.6 ?

Link to comment
Share on other sites

  • 11 months later...
  • 2 months later...

Hi,
I'm using multistore. If I use the code from Vekia with variable PS_SHOP_NAME, I see the name of multistore, but I would like to see the name of one of my store. I would like to use the name which is in ps_shop in DB, not the name which is in ps_configuration in DB. Is that possible?

Link to comment
Share on other sites

  • 7 months later...

Hi guys! I used Vekia instruction in this thread to use my variable in product description. Thanks very much for this code! At this moment I can put my {MY_VARIABLE} to product description (via html editor) and it will show default product category. I used this code in ProductController.php and it work good for me!
 

if (isset($this->product)){
    $this->product->description=str_replace("{MY_VARIABLE}",($this->category->name),"{$this->product->description}");
}

Can you please help me to do the same thing but with certain product Feature Value?

In used code from this this topic, but it only help to use feature value variable which I can put only to .tpl file. But I need to use certain features value variables via html editor in product description.

I'm not a coder and do not know how it can be done correctly, but I'm believe it possible. I need create same product descriptions where different words are features values. I want only set needed feature values for this product and in description will be shown these words automatically. I have big quantity of products and all time I need to add new products. Copy / paste work make me crazy )) Please help me to get correct code, if you can).
Thank you in advance! )

Edited by ssneck (see edit history)
Link to comment
Share on other sites

Answer to my question :)

If you want to use certain feature as variable in you product description you need to put code to ProductController.php as described here by Adolphemattos in post #6 and then put this code:

if (isset($this->product)){
$feature_var = $features_by_key[1]['value'];
$this->product->description=str_replace("{FEATURE_VAR}","{$feature_var}","{$this->product->description}");
}

right before this line:

$this->setTemplate(_PS_THEME_DIR_.'product.tpl');

now you can write {FEATURE_VAR} in product description and in fron office it will show feature with id1.

Link to comment
Share on other sites

  • 1 year later...

Hi guys! I used Vekia instruction in this thread to use my variable in product description. Thanks very much for this code! At this moment I can put my {MY_VARIABLE} to product description (via html editor) and it will show default product category. I used this code in ProductController.php and it work good for me!

 

if (isset($this->product)){
    $this->product->description=str_replace("{MY_VARIABLE}",($this->category->name),"{$this->product->description}");
}

Can you please help me to do the same thing but with certain product Feature Value?

 

In used code from this this topic, but it only help to use feature value variable which I can put only to .tpl file. But I need to use certain features value variables via html editor in product description.

 

I'm not a coder and do not know how it can be done correctly, but I'm believe it possible. I need create same product descriptions where different words are features values. I want only set needed feature values for this product and in description will be shown these words automatically. I have big quantity of products and all time I need to add new products. Copy / paste work make me crazy )) Please help me to get correct code, if you can).

Thank you in advance! )

 

Hi, it works for me.

But i would like to insert 2-3 variables.

 

price

meta_title

category name

 

how can I do it? thanks

  • Like 1
Link to comment
Share on other sites

  • 6 years later...
On 3/11/2014 at 2:19 PM, prestatent said:

Prestashop 1.5.4.1

 

Hi Vekia

 

I created an override for the CMS Controller file, based on your code, as follows:

 

class CmsController extends CmsControllerCore
{
    public function initContent()
    {
        parent::initContent();

        if (isset($this->cms))
        {
            $this->cms->content = str_replace("{MY_VARIABLE}", '../', "{$this->cms->content}");
        }

        $this->setTemplate(_PS_THEME_DIR_.'cms.tpl');
    }
}
 

 

This fix resolved linking from CMS pages to product pages.

 

Thank you once again for your help.

can I use the code for prestashop 1.7.8 pls ?

Link to comment
Share on other sites

On 3/11/2014 at 2:19 PM, prestatent said:

Prestashop 1.5.4.1

 

Hi Vekia

 

I created an override for the CMS Controller file, based on your code, as follows:

 

class CmsController extends CmsControllerCore
{
    public function initContent()
    {
        parent::initContent();

        if (isset($this->cms))
        {
            $this->cms->content = str_replace("{MY_VARIABLE}", '../', "{$this->cms->content}");
        }

        $this->setTemplate(_PS_THEME_DIR_.'cms.tpl');
    }
}
 

 

This fix resolved linking from CMS pages to product pages.

 

Thank you once again for your help.

does not work for 1.7.8 version , anybody knows how can I use that for prestashop 1.7.8 pls ?

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...