Page 1 of 1
Viewing discounts on the main page
Posted: Fri Dec 14, 2007 3:14 am
by quetza
This is conceptually a slightly tricky issue as which discount do you display if there are more than one? I've decided to just display the discount on the lowest quantity.
The first thing to do is get the discount pulled from the database. In catalog/controller/home.php, there's loop to iterate over all the objects: foreach ($results as $result) {. On the line after that, add the following code:
Code: Select all
$product_discount = $database->getRow("select * from product_discount where product_id = '" . (int)$result['product_id'] . "' ORDER BY quantity ASC LIMIT 1");
if (!$product_discount) {
$discount = 0;
} else {
$discount = $product_discount['discount'];
}
This puts the discount with the lowest quantity into $discount.
Inside the $product_data[] = array() definition, add:
Code: Select all
'discount' => $discount,
'discount_price' => $currency->format($tax->calculate($result['price'] - $discount, $result['tax_class_id']))
This makes discount and discount_price available in the template.
In your template directory, edit content/home.tpl
Change this line that displays each item:
Code: Select all
<div class="products"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a><br /><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a><br /><?php echo $product['price']; ?></div>
to
Code: Select all
<div class="products"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a><br /><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a><br />
<?php if (!$product['discount']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price_old"><?php echo $product['price']; ?></span>
<span class="price_new"><?php echo $product['discount_price']; ?></span>
<?php } ?> </div>
Let me know if this works for you.
Re: Viewing discounts on the main page
Posted: Fri Dec 14, 2007 6:17 am
by Bebe
Thank you for your help regarding this issue.
I have tried to do the changes per your instructions and am still having the same problem of the discount showing only on the cart page upon adding to cart and not on the main page. Am I perhaps missing something?
Your time and assistance is greatly appreciated.
Thank you, Bebe
Opencart v 0.7.7
Re: Viewing discounts on the main page
Posted: Fri Dec 14, 2007 9:40 am
by quetza
You've definitely configured a discount on the Discount tab of the product concerned?
You can try running the SQL statement directly. Find out what your productID is (product_description table will probably be the easiest place to view this) and run:
select * from product_discount where product_id = '"{productID}"' ORDER BY quantity ASC LIMIT 1")
replacing {productID} with the actual productID you found from the database.
Does this return the discount row correctly?
Re: Viewing discounts on the main page
Posted: Sat Dec 15, 2007 11:49 am
by Bebe
Thank you for your help.
I have configured the discount in Admin-Product-Discount Tab. The discount does show properly ONLY in the 'New Items' section of the shop AND ONLY after adding a product to the cart for purchase. The discount does not show on the main product page where the description of the item is written.
ie: New Bike listed with sale/discount shows under 'New Items' on front page with original price lined out and the new sale price next to it. Upon clicking to view this New Bike listed at a discount, it takes me to the page showing the complete description. However, on this page, ONLY the original price is showing (index.php?controller=product&product_id=xx) and no indication of the sale/discount price.
If however, you click buy and add this New Bike to cart/basket, the cart/basket page does show the discount.
It seems as if I am only missing a piece of code somewhere.
I am not at all familiar with backend or programming and was/am/is clueless with the SQL instructions. Sorry.
I hope I have been able to explain my problem to your understanding in hopes that you are able to help with a simple fix. Again, thank you for your assistance as I certainly appreciate all your efforts.
Bebe
Re: Viewing discounts on the main page
Posted: Wed Dec 19, 2007 6:18 am
by jakvem
I have tried the modifications as well, but it didn't change anything. The product discount still doesnt show on the main product page and not on the new items display either
Jakob
Re: Viewing discounts on the main page
Posted: Wed Jan 23, 2008 12:21 pm
by bruce
Make similar changes to those described, to controller/product.php and content/product.tpl
Re: Viewing discounts on the main page
Posted: Tue Feb 05, 2008 5:45 am
by ironman
hi,
thank you for contribution, i want to implement this modification on my site, but when i try to view my home page i receive this message:
Code: Select all
Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\wamp\www\opencart\catalog\controller\home.php on line 45
Re: Viewing discounts on the main page
Posted: Tue Feb 05, 2008 7:10 am
by bruce
If you post the file, I will look at it.
Re: Viewing discounts on the main page
Posted: Sat Feb 09, 2008 2:44 am
by ironman
sorry for delay, i was away from my computer.
now... the 2 file where i made changes.
Re: Viewing discounts on the main page
Posted: Sat Feb 09, 2008 1:29 pm
by bruce
It is always the simplest things that bite us. There was a comma missing in the following (shown corrected) from home.php
Other than that, your modification works, I made a discount and it was displayed.
Code: Select all
$product_data[] = array(
'discount' => $discount,
'discount_price' => $currency->format($tax->calculate($result['price'] - $discount, $result['tax_class_id'])),
'name' => $result['name'],
'href' => $url->href('product', FALSE, array('product_id' => $result['product_id'])),
'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id']))
);
Re: Viewing discounts on the main page
Posted: Sat Feb 09, 2008 5:54 pm
by ironman
thank you...
its working.
Re: Viewing discounts on the main page
Posted: Tue Feb 12, 2008 9:16 pm
by ironman
can i use this code on category page?
Re: Viewing discounts on the main page
Posted: Thu Feb 28, 2008 3:21 pm
by silverpenhouse
For the category page I did the following:
For catalog\controller\category.php
I went from:
Code: Select all
foreach ($results as $result) {
$query = array(
'path' => $request->get('path'),
'product_id' => $result['product_id']
);
$product_data[] = array(
'name' => $result['name'],
'href' => $url->href('product', FALSE, $query),
'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id'], $config->get('config_tax')))
);
}
to:
Code: Select all
foreach ($results as $result) {
$product_discount = $database->getRow("select * from product_discount where product_id = '" . (int)$result['product_id'] . "' ORDER BY quantity ASC LIMIT 1");
if (!$product_discount) {
$discount = 0;
} else {
$discount = $product_discount['discount'];
}
$query = array(
'path' => $request->get('path'),
'product_id' => $result['product_id']
);
$product_data[] = array(
'name' => $result['name'],
'href' => $url->href('product', FALSE, $query),
'thumb' => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
'price' => $currency->format($tax->calculate($result['price'], $result['tax_class_id'], $config->get('config_tax'))),
'discount' => $discount,
'discount_price' => $currency->format($tax->calculate($result['price'] - $discount, $result['tax_class_id']))
);
}
and for: For catalog\template\default\content\category.tpl
I changed the:
to:
Code: Select all
<?php if (!$product['discount']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price_old"><?php echo $product['price']; ?></span>
<span class="price_new"><?php echo $product['discount_price']; ?></span>
<?php } ?>
I am still trying to figure out how to do it for the product page, once figured out, I will post it.
Re: Viewing discounts on the main page
Posted: Thu Feb 28, 2008 3:36 pm
by silverpenhouse
Bruce (or anyone), can you expand on how to apply it directly to the product's page.
It's a bit different because of the whole "$view->set(...)" thing and I don't know how to work around it.
I will continue to try but if anyone knows, it would be great.
Thanks,
Amin
Re: Viewing discounts on the main page
Posted: Thu Feb 28, 2008 4:40 pm
by bruce
Here is a working example based on the established premise that we catch the first discount.
This example also styles the Recommended Retail Price in the template a little just for fun.
add the following (some extra code shown to help with location) to \catalog\controller\product.php
Code: Select all
$view->set('thumb', $image->resize($product_info['filename'], $config->get('config_image_width'), $config->get('config_image_height')));
//
// show discounts here
//
$product_discount = $database->getRow("select * from product_discount where product_id = '" . (int)$product_info['product_id'] . "' ORDER BY quantity ASC LIMIT 1");
if ($product_discount)
{
$discount = $product_discount['discount'];
$view->set('discount_price', $currency->format($tax->calculate($discount, $product_info['tax_class_id'], $config->get('config_tax'))));
}
$view->set('price', $currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'], $config->get('config_tax'))));
also make the following change to \catalog\template\default\content\product.tpl
( again, some extra code shown to help with location )
Code: Select all
<div class="left"><?php echo $heading_title; ?></div>
<?php if (isset($discount_price)) { ?>
<div class="right"><?php echo $discount_price; ?><span style="font-size: 75%;" ><?php echo ' RRP:' . $price; ?></span></div>
<?php } else { ?>
<div class="right"><?php echo $price; ?></div>
<?php } ?>
</div>
<div id="product">
hth

Re: Viewing discounts on the main page
Posted: Thu Feb 28, 2008 8:48 pm
by bruce
Just updating, because I made a mistake in the code given above. I displayed the discount amount as the price.
Code: Select all
$view->set('discount_price', $currency->format($tax->calculate($discount, $product_info['tax_class_id'], $config->get('config_tax'))));
should be
Code: Select all
$view->set('discount_price', $currency->format($tax->calculate($product_info['price'] - $discount, $product_info['tax_class_id'], $config->get('config_tax'))));
Re: Viewing discounts on the main page
Posted: Fri Feb 29, 2008 3:40 pm
by silverpenhouse
Bruce, you are the man.
Through your solution, I was able to understand how to apply discounts.
Here is my version of it, and the final result is that it displays the old price as well as the new price. (You can see it being used here:
http://www.juicefringues.com/index.php? ... duct_id=56)
Here is my code:
catalog\controller\product.php:
Code: Select all
$view->set('thumb', $image->resize($product_info['filename'], $config->get('config_image_width'), $config->get('config_image_height')));
//
// show discounts here
//
$product_discount = $database->getRow("select * from product_discount where product_id = '" . (int)$product_info['product_id'] . "' ORDER BY quantity ASC LIMIT 1");
if (!$product_discount)
{
$discount = 0;
$view->set('discount', $discount);
} else {
$discount = $product_discount['discount'];
$view->set('discount', $discount);
$view->set('discount_price', $currency->format($tax->calculate($product_info['price']-$discount, $product_info['tax_class_id'], $config->get('config_tax'))));
}
$view->set('price', $currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'], $config->get('config_tax'))));
catalog\template\default\content\product.tpl :
Code: Select all
<div class="heading">
<div class="left"><?php echo $heading_title; ?></div>
<div class="right">
<?php if (!$discount) { ?>
<?php echo $price; ?>
<?php } else { ?>
<span class="price_old"><?php echo $price; ?></span>
<span class="price_new"><?php echo $discount_price; ?></span>
<?php } ?>
</div>
</div>
<div id="product">
Thanks again Bruce,
Amin
Re: Viewing discounts on the main page
Posted: Fri Feb 29, 2008 6:21 pm
by bruce
My pleasure and well done.