Jump to content

[SOLVED] Modify blocknewproducts to show products from one category


Recommended Posts

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

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

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

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

  • 2 weeks later...

hi rocky

i 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,46

thx

//$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

  • 1 month later...

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...