Jump to content

[SOLVED]Display products with a certain discount?


Accounts

Recommended Posts

You can go to prices-drop.php to get a list of all products with a discount. You can't get only the ones with a certain percentage discount without modifying code though.

If you don't mind modifying code, try changing line 1201 of classes/Product.php (in PrestaShop v1.3.1) from:

static public function getPricesDrop($id_lang, $pageNumber = 0, $nbProducts = 10, $count = false, $orderBy = NULL, $orderWay = NULL, $beginning = false, $ending = false)



to:

static public function getPricesDrop($id_lang, $pageNumber = 0, $nbProducts = 10, $count = false, $orderBy = NULL, $orderWay = NULL, $beginning = false, $ending = false, $withDiscount = 0)



and line 1228 from:

($beginning ? 'AND `reduction_from` <= \''.pSQL($beginning).'\'' : '').($ending ? 'AND `reduction_to` >= \''.pSQL($ending).'\'' : '')).'



to:

($beginning ? 'AND `reduction_from` <= \''.pSQL($beginning).'\'' : '').($ending ? 'AND `reduction_to` >= \''.pSQL($ending).'\'' : '')).($withDiscount > 0 ? ' AND reduction_percent = ' . $withDiscount : '').'



and line 1250 from:

($beginning ? 'AND `reduction_from` <= \''.pSQL($beginning).'\'' : '').($ending ? 'AND `reduction_to` >= \''.pSQL($ending).'\'' : '')).'



to:

($beginning ? 'AND `reduction_from` <= \''.pSQL($beginning).'\'' : '').($ending ? 'AND `reduction_to` >= \''.pSQL($ending).'\'' : '')).($withDiscount > 0 ? ' AND reduction_percent = ' . $withDiscount : '').'



Then add the following before the $smarty->assign in prices-drop.php:

$discount = 0;

if (isset($_GET['discount']))
   $discount = $_GET['discount'];



and change line 16 from:

'products' => Product::getPricesDrop(intval($cookie->id_lang), intval($p) - 1, intval($n), false, $orderBy, $orderWay),



to:

'products' => Product::getPricesDrop(intval($cookie->id_lang), intval($p) - 1, intval($n), false, $orderBy, $orderWay, false, false, $discount),



Once you've done that, you should be able to use a URL like prices-drop.php?discount=20 to get a listing of all products with a 20% discount.

Link to comment
Share on other sites

  • 7 months later...
  • 3 months later...

Hello,

I've test your solution but i've a bug with the pagination.

i've the same number of article with or without the filter...

 

I thinks that the pagination comes with this line :

 

($beginning ? 'AND `reduction_from` <= ''.pSQL($beginning).''' : '').($ending ? 'AND `reduction_to` >= ''.pSQL($ending).''' : '')).($withDiscount > 0 ? ' AND reduction_percent = ' . $withDiscount : '').'

 

But i'm not sure ..

 

Someone can help me ?

I'm in prestashop 1.3.2.3

 

Thanks

Fred

Link to comment
Share on other sites

  • 3 years later...

Dear Rocky,

 

I'm trying to modify the files you mean on this post. The think is that i'm using prestashop 1.6 and files look very different.

 

For example, when you mean:

($beginning ? 'AND `reduction_from` <= \''.pSQL($beginning).'\'' : '').($ending ? 'AND `reduction_to` >= \''.pSQL($ending).'\'' : ''))

There is:

(!$beginning ? $current_date : $beginning), (!$ending ? $current_date : $ending), $context);

I know it does exactly the same. But parameters are different. So the way to write its function must be some different. 

 

Can you please write it again to new prestashop versions? *I should need all lines rewrited for 1.6 version.

 

Thank you for everything.

Link to comment
Share on other sites

Hello,

 

I've tryed editing these files and prices_drop doesn't work...

I've tryied what i've explained here:

 

 

1)

controllers/front/PricesDropController.php :: line 41

- Read var "descuento" from URL

- add var $descuento to function getPricesDrop(..., $descuento)

public function initContent()
{
$descuento = 0;
if (isset($_GET['descuento'])) {$descuento = ($_GET['descuento']/100);}
parent::initContent();


$this->productSort();
$nbProducts = Product::getPricesDrop($this->context->language->id, null, null, true, $descuento);
$this->pagination($nbProducts);


$products = Product::getPricesDrop($this->context->language->id, (int)$this->p - 1, (int)$this->n, false, $this->orderBy, $this->orderWay, $descuento);
$this->addColorsToProductList($products);


$this->context->smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'nbProducts' => $nbProducts,
'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
'comparator_max_item' => Configuration::get('PS_COMPARATOR_MAX_ITEM')
));


$this->setTemplate(_PS_THEME_DIR_.'prices-drop.tpl');
}
 
2)

classes/Product.php ::  line 2239

 

- added $descuento to function getPricesDrop(..., $descuento)

- added $descuento to function _getProductIdByDate(..., $descuento)

public static function getPricesDrop($id_lang, $page_number = 0, $nb_products = 10, $count = false,
$order_by = null, $order_way = null, $beginning = false, $ending = false, Context $context = null, $descuento)
{
if (!Validate::isBool($count))
die(Tools::displayError());


if (!$context) $context = Context::getContext();
if ($page_number < 0) $page_number = 0;
if ($nb_products < 1) $nb_products = 10;
if (empty($order_by) || $order_by == 'position') $order_by = 'price';
if (empty($order_way)) $order_way = 'DESC';
if ($order_by == 'id_product' || $order_by == 'price' || $order_by == 'date_add'  || $order_by == 'date_upd')
$order_by_prefix = 'p';
else if ($order_by == 'name')
$order_by_prefix = 'pl';
if (!Validate::isOrderBy($order_by) || !Validate::isOrderWay($order_way))
die (Tools::displayError());
$current_date = date('Y-m-d H:i:s');
$ids_product = Product::_getProductIdByDate((!$beginning ? $current_date : $beginning), (!$ending ? $current_date : $ending), $context, $descuento);

 

3)

classes/SpecificPrice.php :: line 349

- Init function with $descuento var in getProductIdByDate(..., $descuento)

- use $descuento into SQL sentence

public static function getProductIdByDate($id_shop, $id_currency, $id_country, $id_group, $beginning, $ending, $id_customer = 0, $with_combination_id = false, $descuento)
{
if (!SpecificPrice::isFeatureActive())
return array();


$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT `id_product`, `id_product_attribute`
FROM `'._DB_PREFIX_.'specific_price`
WHERE `id_shop` IN(0, '.(int)$id_shop.') AND
`id_currency` IN(0, '.(int)$id_currency.') AND
`id_country` IN(0, '.(int)$id_country.') AND
`id_group` IN(0, '.(int)$id_group.') AND
`id_customer` IN(0, '.(int)$id_customer.') AND
`from_quantity` = 1 AND
(
(`from` = \'0000-00-00 00:00:00\' OR \''.pSQL($beginning).'\' >= `from`)
AND
(`to` = \'0000-00-00 00:00:00\' OR \''.pSQL($ending).'\' <= `to`)
)
AND
`reduction` = '.$descuento, false);
$ids_product = array();
while ($row = Db::getInstance()->nextRow($result))
$ids_product[] = $with_combination_id ? array('id_product' => (int)$row['id_product'], 'id_product_attribute' => (int)$row['id_product_attribute']) : (int)$row['id_product'];
return $ids_product;
}

What am i doing wrong?

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

SOLVED!!

 

I've solved it only with one very very easy think....

All changes before, discarted.

 

All we need to do on 1.6 version is going to classes/SpecificPrice.php :: line 349

 

And do this:

 

public static function getProductIdByDate($id_shop, $id_currency, $id_country, $id_group, $beginning, $ending, $id_customer = 0, $with_combination_id = false)
{
if (!SpecificPrice::isFeatureActive())
return array();


if(isset($_GET['descuento'])) {
$descuento = $_GET['descuento']/100;
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT `id_product`, `id_product_attribute`
FROM `'._DB_PREFIX_.'specific_price`
WHERE `id_shop` IN(0, '.(int)$id_shop.') AND
`id_currency` IN(0, '.(int)$id_currency.') AND
`id_country` IN(0, '.(int)$id_country.') AND
`id_group` IN(0, '.(int)$id_group.') AND
`id_customer` IN(0, '.(int)$id_customer.') AND
`from_quantity` = 1 AND
(
(`from` = \'0000-00-00 00:00:00\' OR \''.pSQL($beginning).'\' >= `from`)
AND
(`to` = \'0000-00-00 00:00:00\' OR \''.pSQL($ending).'\' <= `to`)
)
AND
`reduction` = '.$descuento, false);
$ids_product = array();
while ($row = Db::getInstance()->nextRow($result))
$ids_product[] = $with_combination_id ? array('id_product' => (int)$row['id_product'], 'id_product_attribute' => (int)$row['id_product_attribute']) : (int)$row['id_product'];
return $ids_product;
}
if(!isset($_GET['descuento'])){
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT `id_product`, `id_product_attribute`
FROM `'._DB_PREFIX_.'specific_price`
WHERE `id_shop` IN(0, '.(int)$id_shop.') AND
`id_currency` IN(0, '.(int)$id_currency.') AND
`id_country` IN(0, '.(int)$id_country.') AND
`id_group` IN(0, '.(int)$id_group.') AND
`id_customer` IN(0, '.(int)$id_customer.') AND
`from_quantity` = 1 AND
(
(`from` = \'0000-00-00 00:00:00\' OR \''.pSQL($beginning).'\' >= `from`)
AND
(`to` = \'0000-00-00 00:00:00\' OR \''.pSQL($ending).'\' <= `to`)
)
AND
`reduction` > 0
', false);
$ids_product = array();
while ($row = Db::getInstance()->nextRow($result))
$ids_product[] = $with_combination_id ? array('id_product' => (int)$row['id_product'], 'id_product_attribute' => (int)$row['id_product_attribute']) : (int)$row['id_product'];
return $ids_product; 
}
}

What does it do?

 

If there is a variable on URL called "descuento" the SQL sentence SELECTS WHERE reduction = descuento

else, if there is no var called "descuento" it does what always did...

 

 

Link to comment
Share on other sites

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