Kelsier Posted May 18, 2022 Share Posted May 18, 2022 (edited) Hello: I am using version 1.7.8.5 of PrestaShop. I have created a CMS page, and I have created a template in the path /themes/classic/templates/cms/ to be able to display fully personalized content. So far, so good. In a section of the page, I want to display products from a certain category. I have achieved this without problems using the following code: {assign var="products" value=Product::getProducts(1, 0, 6, 'id_product', 'DESC', 11, true)} {foreach $products as $product} <div class="product-description"> <h3 class="h3 product-title"> <a href="{Context::getContext()->link->getProductLink( $product.id_product )}" content="{Context::getContext()->link->getProductLink($product.id_product )}">{$product.name} </a> </h3> </div> {/foreach} The loop works and the products are displayed. My problem is that I can't show the discounts of the products. I have tried various solutions based on other system TPLs, but I can't get the product discount. {if $product.has_discount} {if $product.discount_type === 'percentage'} <span class="discount-percentage discount-product">{l s='Save %percentage%' d='Shop.Theme.Catalog' sprintf=['%percentage%' => $product.discount_percentage_absolute]}</span> {else} <span class="discount discount-amount"> {l s='Save %amount%' d='Shop.Theme.Catalog' sprintf=['%amount%' => $product.discount_to_display]}</span> {/if} {else} <span class="price" aria-label="Precio">{$product.price|ceil} €</span> {/if} But the non-discounted price is always displayed, even on products that have it. How can I check if a product has a discount or specific prices, and then show it if so? Thank you very much in advance. Edited May 19, 2022 by Kelsier (see edit history) Link to comment Share on other sites More sharing options...
Kelsier Posted May 20, 2022 Author Share Posted May 20, 2022 (edited) Update: I have ended up making a module, with a hook, which I call from a tpl. The hook works perfectly, it collects the products of the category, with all the information, including the specific prices. But I still have a problem, I can't get the variable to be passed to the tpl. I am using the smarty->assing method but it doesn't work, in the tpl the variable returns null. This is the code of my hook. if (array_key_exists('max', $params)) { $max = $params['max']; } else{ $max = 1000; } $category = new Category( $params['id_category'], //id de la categoría (int)Context::getContext()->language->id // id del idioma ); $caronteProducts = $category->getProducts( (int)Context::getContext()->language->id, //id lenguaje 1, //número de páginas $max, //productos por páginas 'date_add', //order by 'DESC', //order way false, //get total true, //mostrar activos false, // random 1, // random_number_products true, //check access Context::getContext() //context ); $this->context->smarty->assign('caronteProducts', $caronteProducts); return $caronteProducts; And so, that's how I call it from the tpl. {hook h="displayCaronteCategories" id_category=11} {$caronteProducts|var_dump} However, when doing the var_dump from the tpl, the variable returns Null. From the hook the variable shows all the information. Any help please? Edited May 20, 2022 by Kelsier (see edit history) Link to comment Share on other sites More sharing options...
Luis C Posted May 23, 2022 Share Posted May 23, 2022 Can you please create a template file inside your module's folder and call it instead of returning the object you just created? (return $caronteProducts) return $this->display(__FILE__, 'file.tpl'); Then try to var_dump the object from your newly created template file. If you hooked your module to "displayCaronteCategories" and it's correctly registered you should get a result, unless your object does not contain data. Let me know 1 Link to comment Share on other sites More sharing options...
Kelsier Posted May 25, 2022 Author Share Posted May 25, 2022 Hello, Thank you very much for your answer, I have tried registering the TPL from the hook. My problem is that I call the hook from a tpl assigned to a CMS. Like this: {hook h="displayCaronteCategories" id_category=11} If I call the hook from the tpl, and then from the hook I tell it to show the tpl again, it will show the content incorrectly, it can even loop with itself right? Link to comment Share on other sites More sharing options...
Luis C Posted May 25, 2022 Share Posted May 25, 2022 (edited) That's how modules work after all. You call your hook from a theme template file, then your module calls its own template file. At any rate, correct me if I'm wrong, but you can't assign a smarty variable inside a module, and then use it in a theme template file outside your module, because you've assigned that variable inside a method/function, and you can only use it if you make an instance of the class (yourModule class). If storing a loop inside your object/variable is all your module does, you're safe removing your "Return $caronteProducts" line and adding return $this->display(__FILE__, 'file.tpl'); it should not make an infinite loop. If you feel safe doing so, please share your module main php file and I'll take a look Edited May 25, 2022 by Luis C (see edit history) Link to comment Share on other sites More sharing options...
ventura Posted May 25, 2022 Share Posted May 25, 2022 I think you are not using the most appropriate way. There are modules of this type available 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