Page 1 of 1

Display date available at product page [Solved]

Posted: Tue Oct 14, 2014 8:04 pm
by htwp
Hi, can someone tell me how to show in frond end the date of availability? If i choose a future date in admin the product is auto disabled, so i need some code to display the product and show also the date in order to make the pre-order available.

Thank you

Re: Display date available at product page

Posted: Tue Oct 14, 2014 11:52 pm
by tacobandito
So I went ahead and worked this out for you. This was tested to work on 1.5.6.4, so may need tweaking for 2.0 or other versions.

Search for this code in catalog\controller\product\product.php

Code: Select all

$this->data['description'] = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
Add this code directly below on a new line.

Code: Select all

$this->data['date_available'] = $product_info['date_available'];
Now we've added this as a variable to the controller (this information is already pulled in the model, no need to adjust that), we just need to call it from the view (theme).

To do that, you'll need to edit catalog\view\theme\YOUR_THEME\template\product\product.tpl

So, you'll need to figure out where you'd like this new info to show up on your theme...and then paste this code in that spot...

Code: Select all

<?php if ( isset($date_available) && !empty($date_available) ) { echo $date_available; } ?>
The isset & empty check probably isn't necessary...but hey, why not. Hope this was helpful!

Re: Display date available at product page

Posted: Wed Oct 15, 2014 2:04 am
by htwp
It worked very well, thank you very much...

now i am trying to show this (availability date message) only when the current date is not the same as availability date...when the date comes the message will automatically stop to show up...any idea?

product A will be available on 25/10/2014
current date 14/10/2014 so display the date
if current date is same as available date then don't display it!!!

Re: Display date available at product page

Posted: Wed Oct 15, 2014 2:33 am
by tacobandito
This should work...

Code: Select all

<?php 
$now = new DateTime("now");
$then = new DateTime($date_available);

if ($now > $then) {
     echo "<p>Today is after the date_available</p>";
} else {
     echo "<p>Today is before the date_available</p>";
}
?>

Re: Display date available at product page

Posted: Wed Oct 15, 2014 4:07 am
by htwp
Hi again thank you again for your help i appreciate it. i have added the following code

<?php
$now = new DateTime("now");
$then = new DateTime($date_available);
$date_new = $date_available;

if ($now < $then && !empty($date_available) ) { echo "<h2>Διαθέσιμη ημερομηνία παραλαβής: "; echo date('d/m/Y',strtotime($date_new));


} else {
echo "";
}
?>

is it possible to make the date red color?

Re: Display date available at product page

Posted: Wed Oct 15, 2014 4:24 am
by tacobandito
Copy + Paste of your code with red colored date.

Code: Select all

<?php
$now = new DateTime("now");
$then = new DateTime($date_available);
$date_new = $date_available;

if ($now < $then && !empty($date_available) ) { echo "<h2>Διαθέσιμη ημερομηνία παραλαβής: "; echo "<span style='color:red;'>".date('d/m/Y',strtotime($date_new))."</span>"; 


} else {
echo "";
}
?>
Why have the $date_new variable? It's the same as $date_available. Also, you have an unclosed <h2> tag, which might cause issues if you haven't closed that after the fact...Here's a tidier version of the code if you'd like to use it, now that "dev work" is wrapped.

Code: Select all

<?php
$now = new DateTime("now");
$then = new DateTime($date_available);

if ($now < $then && !empty($date_available) ) { echo "<h2>Διαθέσιμη ημερομηνία παραλαβής: "; echo "<span style='color:red;'>".date('d/m/Y',strtotime($date_available))."</span></h2>"; }
?>

Re: Display date available at product page

Posted: Wed Oct 15, 2014 4:27 am
by htwp
I wish i had your knowledge.... Thank you so much...

Re: Display date available at product page

Posted: Wed Oct 15, 2014 4:36 am
by tacobandito
Happy to help! Be sure to mark the topic [Solved] by editing the first post in the thread and adding that to the title. If you need something more complicated developed in the future, feel free to PM me! :D

Re: Display date available at product page [Solved]

Posted: Wed Oct 15, 2014 9:19 pm
by htwp
I have made an extension to this free...

http://www.opencart.com/index.php?route ... n_id=19072

Re: Display date available at product page [Solved]

Posted: Thu Oct 16, 2014 12:59 am
by tacobandito
Neat. I downloaded and took a look...it looks like you adjusted the cart functionality as well....I'm assuming so you could still checkout with a product that's not yet available?

Re: Display date available at product page [Solved]

Posted: Thu Oct 16, 2014 2:18 am
by htwp
You may still checkout with no problem.... i already using it with no problems at all....