Jump to content

Assigning variable to smarty


mariusz_k

Recommended Posts

I am trying to use sql query in php, assign the value result to smarty and then call it in .tpl file.
 
code added to product.php
public function sendIdLangBySmarty($query_results)


{


$sql = 'SELECT id_country FROM '._DB_PREFIX_.'ps_adress WHERE id_customer = $cookie->id_customer';


$result = Db::getInstance()->getValue($sql);


$this->context->smarty->assign('query_results', Db::getInstance()->getValue($result));


}

code added to product.tpl for display or not "add to cart" button

{if $is_logged && $query_results == 2}
.....
{/if}

What I want to do now is to disable possibility "add to cart" for users from some countries.

I know this can be done easier. So why am i doing such a think? When this part will work correctly 
I want to add another query to restrict this for only some products.
 
The question is what am I doing wrong? For now it's just doasn't working and i'am sure it's coused
by php part becuse when I restrict only by $is_logged in .tpl it works like a charm.
 
I found some topics similar to my problem but still cannot do it correctly by myself. 
Edited by mariusz_k (see edit history)
Link to comment
Share on other sites

Hi there the common practice to do is to add the smarty variables in the controller of the page not in the class file

 

so you can keep the function to the Product.php but the smarty assign code should be moved to the ProductController.php inside the initContent function and changed to something like 

$this->context->smarty->assign('query_results', $this->product->sendIdLangBySmarty());
Link to comment
Share on other sites

 

I am trying to use sql query in php, assign the value result to smarty and then call it in .tpl file.
 
code added to product.php
public function sendIdLangBySmarty($query_results)


{


$sql = 'SELECT id_country FROM '._DB_PREFIX_.'ps_adress WHERE id_customer = $cookie->id_customer';


$result = Db::getInstance()->getValue($sql);


$this->context->smarty->assign('query_results', Db::getInstance()->getValue($result));


}

code added to product.tpl for display or not "add to cart" button

{if $is_logged && $query_results == 2}
.....
{/if}

What I want to do now is to disable possibility "add to cart" for users from some countries.

I know this can be done easier. So why am i doing such a think? When this part will work correctly 
I want to add another query to restrict this for only some products.
 
The question is what am I doing wrong? For now it's just doasn't working and i'am sure it's coused
by php part becuse when I restrict only by $is_logged in .tpl it works like a charm.
 
I found some topics similar to my problem but still cannot do it correctly by myself. 

 

 

If you want to use a module... https://addons.prestashop.com/en/order-management/26993-hide-price-and-disallow-purchase-of-products.html

Link to comment
Share on other sites

 

BalzoT - Thank you very much for the valuable advice about using controllers

 

joseantgv - I have seen this module already, but due to self-development I would like to try not to use ready-made solutions ;) But I am also grateful for your commitment

 

After weekend when I get back to work at the store, I will share my progress.

Edited by mariusz_k (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...