Jump to content

block new products


joaocoelho

Recommended Posts

Hi,

I need to adapt my code to show random products.

 

I have this code into blocknewproducts.php:

<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2013 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

if (!defined('_PS_VERSION_'))
	exit;

class BlockNewProducts extends Module
{
	public function __construct()
	{
		$this->name = 'blocknewproducts';
		$this->tab = 'front_office_features';
		$this->version = '1.5';
		$this->author = 'PrestaShop';
		$this->need_instance = 0;

		parent::__construct();

		$this->displayName = $this->l('New products block');
		$this->description = $this->l('Displays a block featuring your store\'s newest products.');
	}

	public function install()
	{
		if (!parent::install()
			|| !$this->registerHook('rightColumn')
			|| !$this->registerHook('header')
			|| !$this->registerHook('addproduct')
			|| !$this->registerHook('updateproduct')
			|| !$this->registerHook('deleteproduct')
			|| !Configuration::updateValue('NEW_PRODUCTS_NBR', 5)
		)
			return false;
		$this->_clearCache('blocknewproducts.tpl');
		return true;
	}
	
	public function uninstall()
	{
		$this->_clearCache('blocknewproducts.tpl');
		return parent::uninstall();
	}

	public function getContent()
	{
		$output = '<h2>'.$this->displayName.'</h2>';
		if (Tools::isSubmit('submitBlockNewProducts'))
		{
			if (!($productNbr = Tools::getValue('productNbr')) || empty($productNbr))
				$output .= '<div class="alert error">'.$this->l('Please complete the "products to display" field.').'</div>';
			elseif ((int)($productNbr) == 0)
				$output .= '<div class="alert error">'.$this->l('Invalid number.').'</div>';
			else
			{
				Configuration::updateValue('PS_BLOCK_NEWPRODUCTS_DISPLAY', (int)(Tools::getValue('always_display')));
				Configuration::updateValue('NEW_PRODUCTS_NBR', (int)($productNbr));
				$output .= '<div class="conf confirm">'.$this->l('Settings updated').'</div>';
			}
		}
		return $output.$this->displayForm();
	}

	public function displayForm()
	{
		$output = '
		<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
		<fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
				<label>'.$this->l('Products to display.').'</label>
					<div class="margin-form">
						<input type="text" name="productNbr" value="'.(int)(Configuration::get('NEW_PRODUCTS_NBR')).'" />
						<p class="clear">'.$this->l('Define the number of products to be displayed in this block.').'</p>
					</div>
					<label>'.$this->l('Always display this block.').'</label>
					<div class="margin-form">
						<input type="radio" name="always_display" id="display_on" value="1" '.(Tools::getValue('always_display', Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY')) ? 'checked="checked" ' : '').'/>
						<label class="t" for="display_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
						<input type="radio" name="always_display" id="display_off" value="0" '.(!Tools::getValue('always_display', Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY')) ? 'checked="checked" ' : '').'/>
						<label class="t" for="display_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
						<p class="clear">'.$this->l('Show the block even if no products are available.').'</p>
					</div>
					<center><input type="submit" name="submitBlockNewProducts" value="'.$this->l('Save').'" class="button" /></center>
				</fieldset>
			</form>';
		return $output;
	}

	public function hookRightColumn($params)
	{
		if (!$this->isCached('blocknewproducts.tpl', $this->getCacheId()))
		{
			if (!Configuration::get('NEW_PRODUCTS_NBR'))
				return;
			$newProducts = false;
			if (Configuration::get('PS_NB_DAYS_NEW_PRODUCT'))
				$newProducts = Product::getNewProducts((int) $params['cookie']->id_lang, 0, (int)Configuration::get('NEW_PRODUCTS_NBR'));
			if (!$newProducts && !Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY'))
				return;
			$this->smarty->assign(array(
				'new_products' => $newProducts,
				'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
			));
		}
		return $this->display(__FILE__, 'blocknewproducts.tpl', $this->getCacheId());
	}

	protected function getCacheId($name = null)
	{
		return parent::getCacheId('blocknewproducts|'.date('Ymd'));
	}

	public function hookLeftColumn($params)
	{
		return $this->hookRightColumn($params);
	}
	
	public function hookHome($params)
	{
		return $this->hookRightColumn($params);
	}

	public function hookHeader($params)
	{
		$this->context->controller->addCSS(($this->_path).'blocknewproducts.css', 'all');
	}

	public function hookAddProduct($params)
	{
		$this->_clearCache('blocknewproducts.tpl');
	}

	public function hookUpdateProduct($params)
	{
		$this->_clearCache('blocknewproducts.tpl');
	}

	public function hookDeleteProduct($params)
	{
		$this->_clearCache('blocknewproducts.tpl');
	}
}

I would like to adapt the module to show RANDOM PRODUCTS, not only NEW PRODUCT. I don't wan't to have a message like "THERE AREN'T NEW PRODUCTS AVAILABLE".

 

How can i do that?

Link to comment
Share on other sites

The function Category::getProducts() can be used to get random products from a category. I can't find a function to get random products from all categories. If you have all your products in the "Home" category, you could replace:

            if (!Configuration::get('NEW_PRODUCTS_NBR'))
                return;
            $newProducts = false;
            if (Configuration::get('PS_NB_DAYS_NEW_PRODUCT'))
                $newProducts = Product::getNewProducts((int) $params['cookie']->id_lang, 0, (int)Configuration::get('NEW_PRODUCTS_NBR'));
            if (!$newProducts && !Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY'))
                return;

with:

            $category = new Category(Configuration::get('PS_HOME_CATEGORY'));
            $newProducts = $category->getProducts($this->context->language->id, 0, 0, null, null, false, true, true, 2);

Change the last parameter 2 to the number of random products you want. Also, if your using an earlier version of PrestaShop that doesn't have the PS_HOME_CATEGORY settings, you'll have to change that to 2 or even 1 for really old versions.

Link to comment
Share on other sites

  • 3 months later...

Vekia Thanks for quick response, where should I place    shuffle($newProducts);  in

 

$category = new Category(Configuration::get('PS_HOME_CATEGORY'));
            $newProducts = $category->getProducts($this->context->language->id, 0, 0, null, null, false, true, true,   2);  shuffle($newProducts);

 

please can you tell me, thanks

Edited by jjryeste (see edit history)
Link to comment
Share on other sites

  • 4 weeks later...

I'd like to display in the newproducts page not only the new products , but also old products that have been modified lastly.

My shop treats unique products, and once a product is soldout , it may return in a slightly different version. Thus the need to appear as a NEW Product.

In order to achieve this I neeed to mofify the getNewProducts function to do my bidding.

But I am stuck coz I don't know where to find it.

Any ideeas ?

EDIT:

Found it , it is a public function in classes/Product.php 

Now I need to update the SQL to add OR my condition :))

will update.

ok done ... a bit too long to post here , so if anyone need it , just message me.

Edited by George.Gall (see edit history)
Link to comment
Share on other sites

  • 8 months later...

Vekia Thanks for quick response, where should I place    shuffle($newProducts);  in

 

$category = new Category(Configuration::get('PS_HOME_CATEGORY'));

            $newProducts = $category->getProducts($this->context->language->id, 0, 0, null, null, false, true, true,   2);  shuffle($newProducts);

 

please can you tell me, thanks

 

Hi did you end up working out where the Shuffle went?

Link to comment
Share on other sites

  • 2 years later...

Tested in 1.6.1.17 and New Products Module v1.10.1

WILL NOT WORK WHEN CACHE IS ENABLED

Create file in directory override/modules/blocknewproducts/blocknewproducts.php and place the code below inside file.

 

<?php

class BlockNewProductsOverride extends BlockNewProducts
{
	protected function getNewProducts()
	{
		if (!Configuration::get('NEW_PRODUCTS_NBR'))
			return;
		$newProducts = false;
		if (Configuration::get('PS_NB_DAYS_NEW_PRODUCT'))
		
		if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'index') {
			$newProducts = Product::getNewProducts((int) $this->context->language->id, 0, 100);shuffle($newProducts);array_splice($newProducts, Configuration::get('NEW_PRODUCTS_NBR') );
		} else {
			
			$newProducts = Product::getNewProducts((int) $this->context->language->id, 0, 100);shuffle($newProducts);array_splice($newProducts, Configuration::get('NEW_PRODUCTS_NBR') );
		}

		if (!$newProducts && Configuration::get('PS_BLOCK_NEWPRODUCTS_DISPLAY'))
			return;
		return $newProducts;
	}
} 
Edited by dirtrider118 (see edit history)
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...