lewisdonovan Posted October 29, 2010 Share Posted October 29, 2010 Hi, I'm trying to show a block on the homepage that lists all products from a single category, along with image and description (truncated to 60 chars).I have tried to adapt the blocknewproducts module but don't know where to start.Any help would be greatly appreciated. Link to comment Share on other sites More sharing options...
rocky Posted October 30, 2010 Share Posted October 30, 2010 Change line 65 of modules/blocknewproducts/blocknewproducts.php from: $newProducts = Product::getNewProducts(intval($params['cookie']->id_lang), 0, intval(Configuration::get('NEW_PRODUCTS_NBR'))); to: //$newProducts = Product::getNewProducts(intval($params['cookie']->id_lang), 0, intval(Configuration::get('NEW_PRODUCTS_NBR'))); $category = new Category(2, intval($params['cookie']->id_lang)); $newProducts = $category->getProducts(intval($params['cookie']->id_lang), 1, 10); Change 2 to the ID of the category you want to get the products from and 10 to how many products you want to get. Note that the more products you get, the slower your website will become. Link to comment Share on other sites More sharing options...
lewisdonovan Posted October 31, 2010 Author Share Posted October 31, 2010 Hmmm, it's still not working. Below is the code from my .php and .tpl files for this module, including the fix you mentioned.blocknewproducts.php: <?php class BlockNewProducts extends Module { private $_html = ''; private $_postErrors = array(); function __construct() { $this->name = 'blocknewproducts'; $this->tab = 'Blocks'; $this->version = 0.9; parent::__construct(); /* The parent construct is required for translations */ $this->page = basename(__FILE__, '.php'); $this->displayName = $this->l('New products block'); $this->description = $this->l('Displays a block featuring newly added products'); } function install() { if (parent::install() == false OR $this->registerHook('home') == false OR Configuration::updateValue('NEW_PRODUCTS_NBR', 5) == false) return false; return true; } public function getContent() { $output = ''.$this->displayName.''; if (Tools::isSubmit('submitBlockNewProducts')) { if (!$productNbr = Tools::getValue('productNbr') OR empty($productNbr)) $output .= ''.$this->l('You should fill the "products displayed" field').''; elseif (intval($productNbr) == 0) $output .= ''.$this->l('Invalid number.').''; else { Configuration::updateValue('NEW_PRODUCTS_NBR', intval($productNbr)); $output .= 'l('Confirmation').'" />'.$this->l('Settings updated').''; } } return $output.$this->displayForm(); } public function displayForm() { $output = ' <form action="'.$_SERVER['REQUEST_URI'].'" method="post"> _path.'logo.gif" alt="" title="" />'.$this->l('Settings').' '.$this->l('Products displayed').' <input type="text" name="productNbr" value="'.Configuration::get('NEW_PRODUCTS_NBR').'" /> '.$this->l('Set the number of products to be displayed in this block').' <input type="submit" name="submitBlockNewProducts" value="'.$this->l('Save').'" class="button" /> </form>'; return $output; } function hookhome($params) { global $smarty; $currency = new Currency(intval($params['cookie']->id_currency)); //$newProducts = Product::getNewProducts(intval($params['cookie']->id_lang), 0, intval(Configuration::get('NEW_PRODUCTS_NBR'))); $category = new Category(73, intval($params['cookie']->id_lang)); $newProducts = $category->getProducts(intval($params['cookie']->id_lang), 1, 10); $new_products = array(); if ($newProducts) foreach ($newProducts AS $newProduct) $new_products[] = $newProduct; $smarty->assign('new_products', $new_products); return $this->display(__FILE__, 'blocknewproducts.tpl'); } function hookLeftColumn($params) { return $this->hookhome($params); } } ?> Link to comment Share on other sites More sharing options...
lewisdonovan Posted October 31, 2010 Author Share Posted October 31, 2010 blocknewproducts.tpl: <!-- MODULE Block new products --> {l s='New products' mod='blocknewproducts'} {l s='See all' mod='blocknewproducts'} {if $new_products} {foreach from=$new_products item=product name=myLoop} {/foreach} {$new_products.0.name|escape:htmlall:'UTF-8'|truncate:30} {if $product.description_short} {$new_products.0.description_short|strip_tags:htmlall:'UTF-8'|truncate:60} {/if} {$new_products.1.name|escape:htmlall:'UTF-8'|truncate:30} {if $product.description_short} {$new_products.1.description_short|strip_tags:htmlall:'UTF-8'|truncate:60} {/if} {$new_products.2.name|escape:htmlall:'UTF-8'|truncate:30} {if $product.description_short} {$new_products.2.description_short|strip_tags:htmlall:'UTF-8'|truncate:60} {/if} {$new_products.3.name|escape:htmlall:'UTF-8'|truncate:30} {if $product.description_short} {$new_products.3.description_short|strip_tags:htmlall:'UTF-8'|truncate:60} {/if} {else} {l s='No new product at this time' mod='blocknewproducts'} {/if} <!-- /MODULE Block new products --> Link to comment Share on other sites More sharing options...
rocky Posted November 1, 2010 Share Posted November 1, 2010 I forgot the tag in the second block above. I've updated my post. Please try again. Link to comment Share on other sites More sharing options...
lewisdonovan Posted November 1, 2010 Author Share Posted November 1, 2010 Okay, it's now listing products from the category I want but it's only listing 4 products, not 10.Is this something to do with the foreach statement in blocknewproducts.tpl coming BEFORE the UL and LI tags?Thanks Link to comment Share on other sites More sharing options...
rocky Posted November 2, 2010 Share Posted November 2, 2010 Yes, it seems your code is hardcoded to display only 4 products. Try the following code instead: <!-- MODULE Block new products --> {l s='New products' mod='blocknewproducts'} {l s='See all' mod='blocknewproducts'} {if $new_products} {foreach from=$new_products item=new_product name=myLoop} <a href="{$new_product.link}"> <img src="{$img_prod_dir}{$new_product.id_image}-small.jpg" height="40" width="40" alt="{$new_product.legend|escape:htmlall:'UTF-8'}"> <a href="{$new_product.link}" title="{$new_product.name|escape:htmlall:'UTF-8'}">{$new_product.name|escape:htmlall:'UTF-8'|truncate:30} {if $new_product.description_short} <a href="{$new_product.link}">{$new_product.description_short|strip_tags:htmlall:'UTF-8'|truncate:60} <a class="linkview" href="{$new_product.link}" title="{$new_product.name|escape:htmlall:'UTF-8'}"> {/if} {/foreach} {else} {l s='No new product at this time' mod='blocknewproducts'} {/if} <!-- /MODULE Block new products --> Link to comment Share on other sites More sharing options...
kiamaru Posted November 14, 2010 Share Posted November 14, 2010 hi rockyi put this code below in my blocknewsproducts.php. it's OK, but i have the fourth first product from ID 29 and no the 5 last adde product. and how i can to put several id? i want to put 29,6,165,46thx//$newProducts = Product::getNewProducts(intval($params['cookie']->id_lang), 0, intval(Configuration::get('NEW_PRODUCTS_NBR')));$category = new Category(29, intval($params['cookie']->id_lang));$newProducts = $category->getProducts(intval($params['cookie']->id_lang), 1, 4); Link to comment Share on other sites More sharing options...
lewisdonovan Posted November 14, 2010 Author Share Posted November 14, 2010 I forgot to reply to this thread after your last post, rocky.Cheers for all of that, it's running fine now. You're a star. Link to comment Share on other sites More sharing options...
Daniel Ciornei Posted December 15, 2010 Share Posted December 15, 2010 Can this be modified to show only the products that have positive stock? and the number of products still be acording to the number of prod set up in BO. 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