Skip to main content

Custom Field needs proper formatting

More
11 years 8 months ago #188470 by brentwilliams2
-- HikaShop version -- : 2.3.4
-- Joomla version -- : 2.5.27

I am trying to insert a custom field within a product page. I found that I can show the custom field with this code:
Code:
echo $this->row->eventdate;

However, it does not format the date correctly. (I am using the Advanced Date Picker)

Can you tell me what code I should use to show the field, "eventdate" properly formatted?

Thanks!

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188504 by Xavier
Hi,

The returned information is a timestamp, so you need the php date() function to format it ;)
php.net/manual/fr/function.date.php

Something like:
Code:
echo date('d-M-Y',$this->row->eventdate);

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188599 by brentwilliams2
Thanks for the response, Xavier! But it didn't seem to parse correctly...

On a test product, the date in the database shows "20150226000000". I had originally entered February 26, 2015, so the database appears to show correctly.

But when I implement the code you gave, it shows "30-Mar-640505". So something isn't translating right...

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188607 by Jerome
Hi,

While the simple date picker is using a timestamp, the advanced date picker store the date in an internal format.
The internal format has been choose to let everybody select a date before 1 January 1970 (otherwise we can't be sure that the saved time-stamp will work right on every servers) and also to simplify some algorithms of date restriction.

That's why it's better to use the HikaShop field class to display the custom field, so the advanced date picker plugin will process itself the value and display the date with an appropriate format.

Otherwise, you can use the code of the function "getDate" (convert text into an array) and "getTimestamp" (convert an array into a timestamp) of the plugin in order to convert the value.
Code:
function getDate($value, $format = 'm/d/Y') { $ret = array( 'y' => 0, 'm' => 0, 'd' => 0, 'h' => 0, 'i' => 0, 's' => 0 ); if(empty($value)) return $ret; $dateValue = $value; if(preg_match('#^([0-9]+)$#', $value)) { if(strlen($value) == 14) { $dateValue = substr($value,0,4) . '/' . substr($value,4,2) . '/' . substr($value,6,2); } else { $dateValue = hikashop_getDate($value, '%Y/%m/%d'); } list($y,$m,$d) = explode('/', $dateValue, 3); } else { $y = 0; $m = 0; $d = 0; $timestamp = strtotime(str_replace('/', '-', $value)); if($timestamp !== false && $timestamp !== -1 && $timestamp > 0) { $dateValue = date('Y/m/d', $timestamp); list($y,$m,$d) = explode('/', $dateValue, 3); } else { list($y,$m,$d) = explode('/', $value, 3); } } $ret['y'] = (int)$y; $ret['m'] = (int)$m; $ret['d'] = (int)$d; return $ret; } function getTimestamp($value) { if(is_array($value)) { $value = $value['y'] . '/' . $value['m'] . '/' . $value['d']; } $ret = hikashop_getTime($value); return $ret; }

Regards,

Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188645 by brentwilliams2
On further research, I found this thread which appears to be exactly what I am looking for. However, when I tried to implement the code using my field name, I get a 500 error.
Code:
$fieldsClass = hikashop_get('class.field'); $fields = $fieldsClass->getFields('frontcomp',$this->row,'product','checkout&task=state'); echo $fieldsClass->show($fields['eventdate'],$this->row->eventdate);

Am I implementing incorrectly? It is in the show_default.php.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188647 by brentwilliams2
And Xavier, your way works IF the proper timestamp is in the database. If I manually put in "1424822400" into the database, then your way works perfectly. But that isn't the timestamp format that works with this system. Hikashop produces a different type of timestamp, like this "20150226000000".

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188756 by Jerome
Hi,

Am I implementing incorrectly? It is in the show_default.php.

Yes, it is exactly what I was talking about in my answer.

That's why it's better to use the HikaShop field class to display the custom field, so the advanced date picker plugin will process itself the value and display the date with an appropriate format.


Regards,

Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago - 11 years 8 months ago #188769 by brentwilliams2
Jerome, I appreciate the help, but I just don't know what you mean by this:

That's why it's better to use the HikaShop field class to display the custom field, so the advanced date picker plugin will process itself the value and display the date with an appropriate format.


Can you point me to the instructions on how to do this or show me an example?

(By the way, I wanted to say how much I appreciate your help, and I apologize that I'm not "getting it". Sorry for not understanding your instructions.)
Last edit: 11 years 8 months ago by brentwilliams2.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188923 by Jerome
Hi,

Please check your PHP error log.
Without more details bout the fatal error you got it will be difficult to tell you more.
But the approach is for me the right one.

Regards,

Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #188962 by brentwilliams2
Ok, thanks. Got this error:

Fatal error: Call to undefined method hikashop::show() in C:\xampp\htdocs\administrator\components\com_hikashop\classes\field.php on line 1298

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #189002 by Jerome
Hi,

What does the variable "$fields" contains ?
As you see in the "show" function in the HikaShop field class ; it asks for specific data.
Code:
function show(&$field,$value,$className='') { $field_type = $field->field_type; if(substr($field->field_type,0,4) == 'plg.') { $field_type = substr($field->field_type,4); JPluginHelper::importPlugin('hikashop', $field_type); } $classType = 'hikashop'.ucfirst($field_type); if(!class_exists($classType)) return $value; $class = new $classType($this); $html = $class->show($field,$value,$className); return $html; }
If the parameter you give does not contains the information "field_type" ; the function can't call correctly the advanced date picker function.

Regards,

Jerome - Obsidev.com
HikaMarket & HikaSerial developer / HikaShop core dev team.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #189011 by brentwilliams2
Jerome, I am sorry, but I really don't know how this code works. I used this code:
Code:
$fieldsClass = hikashop_get('class.field'); $fields = $fieldsClass->getFields('frontcomp',$this->row,'product','checkout&task=state'); echo $fieldsClass->show($fields['eventdate'],$this->row->eventdate);

All I know is that my custom field is "eventdate" in the database and is a product field, and based upon the example provided in the other link, it looked like this was the format of the code I was supposed to use, although I don't know if that is true.

Beyond that, I simply don't know how this bit of code works. If there is another piece of code that will allow me to use a custom field elsewhere in a template, I would be happy to use it. I just don't know of any other option, and I don't know how to answer your question.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #189043 by nicolas
Hi,

The example code I gave in the other thread is correct and the code you're using is correct too.
So it must come from something else.
First, try to update HikaShop to the latest version. Make sure that the Hikashop Date Picker Plugin of the "hikashop" group is enabled via the joomla plugins manager.
If that doesn't help, please provide a screenshot of the options of the custom field so that we can see how you configured it.

Please Log in or Create an account to join the conversation.

More
11 years 8 months ago #189601 by brentwilliams2
Thanks for the response, Nicolas. I have run out of time on this project, so I will have to try to figure it out later.

Please Log in or Create an account to join the conversation.

Time to create page: 0.263 seconds
Powered by Kunena Forum