fortuner Posted October 8, 2015 Share Posted October 8, 2015 Hello, everyone! I'm trying get product data on product page i.e. name, price, short desc, discount (if applied) and category - name, description, lowest and highest price. How do I implement this? I assume, there is some built-in Prestashop functionality that makes it possible without SQL queries? Here is the code: $page_name = Dispatcher::getInstance()->getController(); // page_name var doesn't work without without this? if ($page_name == 'product') { // What do I do here? } elseif ($page_name == 'category') { // and here? } Link to comment Share on other sites More sharing options...
fortuner Posted October 9, 2015 Author Share Posted October 9, 2015 I am now stuck here: $page_name = Dispatcher::getInstance()->getController(); // page_name var doesn't work without without this if ($page_name == 'product') { $product = new Product((int)Tools::getValue('id_product')); if (Validate::isLoadedObject($product)) { var_dump($product); // This shows all $product data, but I need very little from that. } } How do I get values I need from $product array and assign them to smarty array? I.e product name, price etc? I'm sorry if I'm doing something wrong, I'm novice in php. 1 Link to comment Share on other sites More sharing options...
jgamio Posted October 9, 2015 Share Posted October 9, 2015 1) To get a product data just use check the Product class some info is there other maybe need to load example $product->name to get the product name 2) to assign to you smarty just use some these $this->context->smarty->assign(array( 'product_name' => $product->name, 'product' => $product )); you can send just the data or the product the on template just <p > {$product_name}</p> or <p > {$product.name}</p> 2 Link to comment Share on other sites More sharing options...
fortuner Posted October 10, 2015 Author Share Posted October 10, 2015 1) To get a product data just use check the Product class some info is there other maybe need to load example $product->name to get the product name 2) to assign to you smarty just use some these $this->context->smarty->assign(array( 'product_name' => $product->name, 'product' => $product )); you can send just the data or the product the on template just <p > {$product_name}</p> or <p > {$product.name}</p> Sorry, this doesn't work:( Notice: Undefined variable: product Notice: Trying to get property of non-object in line bla-bla This is module, not a theme, so i guess it cannot access data like that: $product->name Link to comment Share on other sites More sharing options...
yaniv14 Posted October 10, 2015 Share Posted October 10, 2015 Make sure you have the 'id_product' of a real product ID in your url parameters. Also I am assuming that you have a template assign to the display hook. 1 Link to comment Share on other sites More sharing options...
fortuner Posted October 10, 2015 Author Share Posted October 10, 2015 (edited) Make sure you have the 'id_product' of a real product ID in your url parameters. Also I am assuming that you have a template assign to the display hook. You mean like this? And how to use it in tpl then? {$product_name} shows an error array to string conversion. $product = new Product((int)Tools::getValue('id_product')); $this->context->smarty->assign(array( 'product_name' => $product->name, //and other product stuff )); Edited October 10, 2015 by fortuner (see edit history) 1 Link to comment Share on other sites More sharing options...
yaniv14 Posted October 10, 2015 Share Posted October 10, 2015 Can you explain again what exactly are you trying to do. On your first post you mentioned that you are trying to get product data on product page. On product page all the data already assigned to smarty variables thru ProductController which located in controllers/front folder. You can try print the data in the product tpl just to see what product variable gives you. like: {$product|@print_r} You can see sample of product values in the same tpl like {$product->name} If you are talking about something different please explain. Link to comment Share on other sites More sharing options...
jgamio Posted October 10, 2015 Share Posted October 10, 2015 You need to see these $this->context->smarty->assign(array( 'name_to_use_on_tpl' => $data_on_your_module )); now you pass just a string or a object depen on your values you need change how get the info on the tpl Link to comment Share on other sites More sharing options...
fortuner Posted October 12, 2015 Author Share Posted October 12, 2015 Can you explain again what exactly are you trying to do. On your first post you mentioned that you are trying to get product data on product page. On product page all the data already assigned to smarty variables thru ProductController which located in controllers/front folder. You can try print the data in the product tpl just to see what product variable gives you. like: {$product|@print_r} You can see sample of product values in the same tpl like {$product->name} If you are talking about something different please explain. Hello! I'm trying to make little module, that shows additional info on product page. Something like that: "you can buy product X on address Y for price Z" I've already done SQL query to show store addresses and other contacts and succeeded to show them on the page I need. But I'm new to PHP, smarty and Prestashop classes coding, so I don't know how to show product name (price, etc) on product page in module $product = new Product((int)Tools::getValue('id_product')); $this->context->smarty->assign(array( 'product_name' => $product->name, 'product_price' => $product->price, )); This looks like should work, isn't it? If it is wrong, correct me please. And please tell me how to show these two in smarty. Thanks in advance! Link to comment Share on other sites More sharing options...
yaniv14 Posted October 12, 2015 Share Posted October 12, 2015 Well, everything looks fine but I'll try to explain better. this line: $product = new Product((int)Tools::getValue('id_product')); will get product with ID number from post or get 'id_product' so if you are in product page and your module loads on that page and for example the url is like that: http://domain.com/index.php?controller=product&id_product=5 so product with id 5 will be instanced. Tools:getValue its a build in function of prestashop to replace php $_POST or $_GET. $this->context->smarty->assign(array( 'product_name' (this is the variable to be accessed in the template) => $product->name (this is the data implanted in that variable), )); after you assign the smarty variables array you will need to assign a template for it and return it if thats the end of the function (in display hooks most likely yes) like: return $this->display(__FILE__, 'location_of_template/template_name.tpl'); finally in the template you will only need to call the variable like {$product_name} Now I assume that you are hooking that entire function to an existing display hook? because the fact that the module is loaded doesnt mean that the template will show. product.tpl is the file that in charge of the main display of product page and that file is setup with calls for all related hooks like: hookDisplayLeftColumnProduct/hookDisplayRightColumnProduct/etc..... in your case you need to see where is your problem exactly. 1. are you getting the $product it self from DB 2. are you hooking it to a real hook try to echo $product (echo print_r($product) and see if you get that first. maybe you can post the entire content of your module main file to see if i can better help. Link to comment Share on other sites More sharing options...
fortuner Posted October 12, 2015 Author Share Posted October 12, 2015 Well, everything looks fine but I'll try to explain better. this line: $product = new Product((int)Tools::getValue('id_product')); will get product with ID number from post or get 'id_product' so if you are in product page and your module loads on that page and for example the url is like that: http://domain.com/index.php?controller=product&id_product=5 so product with id 5 will be instanced. Tools:getValue its a build in function of prestashop to replace php $_POST or $_GET. $this->context->smarty->assign(array( 'product_name' (this is the variable to be accessed in the template) => $product->name (this is the data implanted in that variable), )); after you assign the smarty variables array you will need to assign a template for it and return it if thats the end of the function (in display hooks most likely yes) like: return $this->display(__FILE__, 'location_of_template/template_name.tpl'); finally in the template you will only need to call the variable like {$product_name} Now I assume that you are hooking that entire function to an existing display hook? because the fact that the module is loaded doesnt mean that the template will show. product.tpl is the file that in charge of the main display of product page and that file is setup with calls for all related hooks like: hookDisplayLeftColumnProduct/hookDisplayRightColumnProduct/etc..... in your case you need to see where is your problem exactly. 1. are you getting the $product it self from DB 2. are you hooking it to a real hook try to echo $product (echo print_r($product) and see if you get that first. maybe you can post the entire content of your module main file to see if i can better help. Here is my code (except addresses part, it's working well) public function hookHeader() { $product = new Product((int)Tools::getValue('id_product')); $this->context->smarty->assign(array( 'name' => $product->name, 'price' => $product->price, )); return $this->display(__FILE__, 'mod_blockcontactinfos_header.tpl'); $this->context->controller->addCSS(($this->_path).'mod_blockcontactinfos.css', 'all'); } Then in tpl file I try this code: {$name} Which causes an error: Notice: Array to string conversion in C:\OpenServer\domains\... Link to comment Share on other sites More sharing options...
yaniv14 Posted October 12, 2015 Share Posted October 12, 2015 hookHeader is attach to the <header> tag and not the body tag so you wont see anything if you attach a template to it. Prestashop use that hook to add CSS or JS mainly. Also the last line of adding a css won't happen because its after the return. Try to split it into and see if you get something (check that the module hooked to the footer first): public function hookHeader(){ $this->context->controller->addCSS(($this->_path).'mod_blockcontactinfos.css', 'all');} public function hookFooter($params) { $product = new Product((int)Tools::getValue('id_product')); $this->context->smarty->assign(array( 'name' => $product->name, 'price' => $product->price, )); return $this->display(__FILE__, 'mod_blockcontactinfos_header.tpl');} Link to comment Share on other sites More sharing options...
fortuner Posted October 12, 2015 Author Share Posted October 12, 2015 hookHeader is attach to the <header> tag and not the body tag so you wont see anything if you attach a template to it. Prestashop use that hook to add CSS or JS mainly. Also the last line of adding a css won't happen because its after the return. Try to split it into and see if you get something (check that the module hooked to the footer first): public function hookHeader() { $this->context->controller->addCSS(($this->_path).'mod_blockcontactinfos.css', 'all'); } public function hookFooter($params) { $product = new Product((int)Tools::getValue('id_product')); $this->context->smarty->assign(array( 'name' => $product->name, 'price' => $product->price, )); return $this->display(__FILE__, 'mod_blockcontactinfos_header.tpl'); } Thanks, I'll fix that. What about displaying $name and $price in smarty mod_blockcontactinfos_header.tpl? Link to comment Share on other sites More sharing options...
yaniv14 Posted October 12, 2015 Share Posted October 12, 2015 Change it first and see if you still get error. If you still got an error try to print $product to see what you get after $product = new Product((int)Tools::getValue('id_product')); add: var_dump($product); 1 Link to comment Share on other sites More sharing options...
ventura Posted October 12, 2015 Share Posted October 12, 2015 Try it this way take the product object $product = new Product((int)Tools::getValue('id_product'), true, $this->context->language->id, $this->context->shop->id); assign the variable in template $this->context->smarty->assign(array( 'product' => $product, )); now you can access to all object variables in template {$product->name} 1 Link to comment Share on other sites More sharing options...
fortuner Posted October 12, 2015 Author Share Posted October 12, 2015 Change it first and see if you still get error. If you still got an error try to print $product to see what you get after $product = new Product((int)Tools::getValue('id_product')); add: var_dump($product); Thank you very much, You are helping me so much! I added {debug} in tpl to see what does smarty has and it has shown me a $name var which i added like this $this->context->smarty->assign('name', $product->name); I finally found where the problem is: Smarty_Variable Object (3) ->value = Array (2) 1 => "Faded Short Sleeve T-shirts" 2 => "Faded Short Sleeve T-shirts" ->nocache = 1 ->scope = "file:C:\OpenServer\domains\y1.loc\www..." The product name is an array because in my test shop there are two languages active. How do I get product name with current language? Thank you again! Link to comment Share on other sites More sharing options...
yaniv14 Posted October 12, 2015 Share Posted October 12, 2015 Use Ventura example. $product = new Product((int)Tools::getValue('id_product'), true, $this->context->language->id, $this->context->shop->id); Link to comment Share on other sites More sharing options...
fortuner Posted October 12, 2015 Author Share Posted October 12, 2015 Try it this way take the product object $product = new Product((int)Tools::getValue('id_product'), true, $this->context->language->id, $this->context->shop->id); assign the variable in template $this->context->smarty->assign(array( 'product' => $product, )); now you can access to all object variables in template {$product->name} A blank page without errors (error reporting is on). I totally do not understand this. Link to comment Share on other sites More sharing options...
fortuner Posted October 12, 2015 Author Share Posted October 12, 2015 A blank page without errors (error reporting is on). I totally do not understand this. Ok, this one was a problem $this->context->smarty->assign(array( 'product' => $product, )); This works instead: $this->context->smarty->assign('product_name', $product->name); Thank you! Link to comment Share on other sites More sharing options...
fortuner Posted October 15, 2015 Author Share Posted October 15, 2015 I've run on some problem with price display. It is displayed witout taxes despite I use convertPrice. Here is the code: PHP $product = new Product((int)Tools::getValue('id_product'), true, $this->context->language->id, $this->context->shop->id); $this->context->smarty->assign('product', $product); Smarty {if !$priceDisplay}{convertPrice price=$product->price}{else}{convertPrice price=$product->price_tax_exc}{/if} Where is my mistake? Link to comment Share on other sites More sharing options...
yaniv14 Posted October 15, 2015 Share Posted October 15, 2015 try to use this to get product price. {convertPrice price=$product->getPrice(true, $smarty.const.NULL, 6)} 1 Link to comment Share on other sites More sharing options...
fortuner Posted October 15, 2015 Author Share Posted October 15, 2015 try to use this to get product price. {convertPrice price=$product->getPrice(true, $smarty.const.NULL, 6)} It works, thanks a lot! Could you please explain what does the expression in backets? This one (true, $smarty.const.NULL, 6) Link to comment Share on other sites More sharing options...
yaniv14 Posted October 15, 2015 Share Posted October 15, 2015 (edited) This is the full function from classes/Product.php public function getPrice($tax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1) all parametes are optional, you are using the first 3 only. basically this function inherit from the main function that is getPriceStatic that look like this. public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1, $force_associated_tax = false, $id_customer = null, $id_cart = null, $id_address = null, &$specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, Context $context = null, $use_customer_price = true) Edited October 15, 2015 by yaniv14 (see edit history) 2 Link to comment Share on other sites More sharing options...
fortuner Posted October 15, 2015 Author Share Posted October 15, 2015 This is the full function from classes/Product.php public function getPrice($tax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1) all parametes are optional, you are using the first 3 only. basically this function inherit from the main function that is getPriceStatic that look like this. public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = null, $decimals = 6, $divisor = null, $only_reduc = false, $usereduc = true, $quantity = 1, $force_associated_tax = false, $id_customer = null, $id_cart = null, $id_address = null, &$specific_price_output = null, $with_ecotax = true, $use_group_reduction = true, Context $context = null, $use_customer_price = true) According to you words this getPrice(false, $smarty.const.NULL, 6) should get a price without taxes right? Tell me please, how do I get the date for special price valid until? Is there any function or I should get it with MySQL query? Link to comment Share on other sites More sharing options...
yaniv14 Posted October 15, 2015 Share Posted October 15, 2015 For your first question 'YES' For your second question I am not sure because I never needed to use it, but you can try getSpecificPrice() function form classes/SpecificPrice.php 1 Link to comment Share on other sites More sharing options...
fortuner Posted October 15, 2015 Author Share Posted October 15, 2015 Here is solution to get specific price date: {if !empty ($product->specificPrice)} {foreach from $product->specificPrice item=to key="id"} {$product->specificPrice.to|date_format:"%Y-%m-%d"} // Date format without hours and minutess {/foreach} {/if} Link to comment Share on other sites More sharing options...
fortuner Posted October 15, 2015 Author Share Posted October 15, 2015 I'm now trying to get products from current category. But it returns only one product. What am I doing wrong? PHP $products = $category->getProducts($this->context->language->id, $get_total = true, $active = true); Smarty {$products|print_r} // Returns first product {$products|@count} // Returns 1 instead product count How do I fix this? Link to comment Share on other sites More sharing options...
yaniv14 Posted October 15, 2015 Share Posted October 15, 2015 The function has 3 required params - lang, page number & products per page. So I guess you can try: $products = $category->getProducts($this->context->language->id, 1, 1000); 1 Link to comment Share on other sites More sharing options...
fortuner Posted October 15, 2015 Author Share Posted October 15, 2015 Thank you so much! You are saving me How do I know which parameters are required for some other functions? Link to comment Share on other sites More sharing options...
yaniv14 Posted October 15, 2015 Share Posted October 15, 2015 Usually its documented above the function Link to comment Share on other sites More sharing options...
fortuner Posted October 23, 2015 Author Share Posted October 23, 2015 How do I get if product available for order when out of stock? In product-list.tpl it is $allow_oops variable, but it doesn't work in module. I was looking through Product class and didn't find anything matching Thanks inadvance! Link to comment Share on other sites More sharing options...
yaniv14 Posted October 23, 2015 Share Posted October 23, 2015 (edited) This is probably smarty assigned variable, so you should check in the controllers instead of the classes. Edited October 23, 2015 by yaniv14 (see edit history) Link to comment Share on other sites More sharing options...
fortuner Posted October 27, 2015 Author Share Posted October 27, 2015 I've found the solution to get info if product is available, when out of stock Here is the code: $allow_oosp = (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'); Returns 1 when product is available and 0 when it isn't. I want to admit that developing for Prestashop is easy to understand. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now