BazzQc Posted November 28, 2017 Share Posted November 28, 2017 (edited) Hi, I want to display the category img of a product if there is no image for this product. I've managed to do it on product.tpl and product-list.tpl with some <if> but its not working yet in the shopping cart, in searchs and many other places. I want to make sure the product keep no img so I only want to change the display and not the database. Since i only change it on display, im pretty sure its a ref problem in the other pages. For example, this is my code in product-list.tpl : <img class="replace-2x img-responsive" itemprop="image" src="{$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category_default')|escape:'html':'UTF-8'}" alt="{$category->name|escape:'html':'UTF-8'}" {if isset($homeSize)} width="{$homeSize.width}" height="{$homeSize.height}"{/if} } If i select a category before showing the products, the image works. If i'm anywhere else (new-products for example), $category is empty... How can I get the category id from the $product ? Prestashop 1.6.1.15 - Theme : Zero (Zro03) Any suggestions on how to do it, I dont think im in the right direction atm ? Edited November 28, 2017 by BazzQc (see edit history) Link to comment Share on other sites More sharing options...
bellini13 Posted November 29, 2017 Share Posted November 29, 2017 A product can be in more than one Category right? When you are looking at the product outside of the category pages (like with new-products), then which category would you use since you are not inside a category page? Link to comment Share on other sites More sharing options...
BazzQc Posted November 29, 2017 Author Share Posted November 29, 2017 55 minutes ago, bellini13 said: A product can be in more than one Category right? When you are looking at the product outside of the category pages (like with new-products), then which category would you use since you are not inside a category page? In my case, a product can only be in one category. So in new-products, I want to find a way to link a product to his category (to have the category img remplace the product img if there is none) Link to comment Share on other sites More sharing options...
BazzQc Posted November 29, 2017 Author Share Posted November 29, 2017 I thought $product->id_category_default would work but it always return 0.. Looked in the DB and its not set to 0 ... Link to comment Share on other sites More sharing options...
BazzQc Posted November 29, 2017 Author Share Posted November 29, 2017 I found out that $product.category_default works if I want the name. now I want the id. Link to comment Share on other sites More sharing options...
bellini13 Posted November 30, 2017 Share Posted November 30, 2017 22 hours ago, BazzQc said: In my case, a product can only be in one category Are you sure about that? Do you put products in the home category? Quote I thought $product->id_category_default would work but it always return 0.. Looked in the DB and its not set to 0 ... Probably the new products module does not obtain all of the product information, but only what is necessary for the module to function. You'll need to add additional code to query a products category. You can try to use the Product::getProductCategories() function to obtain the id_category for the specified product. //note: this will return an array, so if you are sure you will always have 1 category per product, then you can grab the first index $categories = Product::getProductCategories($id_product); $id_category = $categories[0]; Then using the id_category you can create a new Category object to obtain the name. //note: you need to include the id_lang so that you obtain the correct name in the correct language. If you do not pass in the id_lang, then you will not get the name $category = new Category($id_category, $id_lang); $name = $category->name; Link to comment Share on other sites More sharing options...
BazzQc Posted November 30, 2017 Author Share Posted November 30, 2017 1 hour ago, bellini13 said: Are you sure about that? Do you put products in the home category? Probably the new products module does not obtain all of the product information, but only what is necessary for the module to function. You'll need to add additional code to query a products category. You can try to use the Product::getProductCategories() function to obtain the id_category for the specified product. //note: this will return an array, so if you are sure you will always have 1 category per product, then you can grab the first index $categories = Product::getProductCategories($id_product); $id_category = $categories[0]; Then using the id_category you can create a new Category object to obtain the name. //note: you need to include the id_lang so that you obtain the correct name in the correct language. If you do not pass in the id_lang, then you will not get the name $category = new Category($id_category, $id_lang); $name = $category->name; Thanks for the answer, I tried many things yesterday and finally I found $product['id_category_default'] and ita doing exactly what I want. I'm new to Smarty and Prestashop and i'm not sure if its the right method but its doing exactly what I want! 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