Question

My locale is set to France but the advanced search only works in US format. I've made a custom attribute (date type) and add it to the advanced search. And there are 3 problems:

  • The datepicker (default) works in US format no matter which locale I set (the Datepicker translation is ok)
  • The advanced search only works in US format. If I modify the format (url values) from mm/dd/yy to dd/mm/yy I get no results
  • The date validation seems to pick the locale from browser. My default locale is de_DE, so a US date "03/13/14" is not valid and I cannot run the search. If I change the browser locale to en_US everything works. (but with US locale)

Questions: How to force Magento to work with the specified locale settings (fr_FR) in the advanced search and validator and to ignore the browser locale?

I work with the community edition. The problem is the same for version 1.7 and 1.8.

UPDATE:

I found the place where the browser locale is detected: app/code/core/Mage/CatalogSearch/Model/Resource/Advanced/Collection.php

if (!Zend_Date::isDate($conditionValue['from'])) {
    Mage::throwException($invalidDateMessage);
}

app/code/core/Zend/Date.php

$locale = Zend_Locale::findLocale($locale);

If I change it to the following I can force the locale to en_US for example:

if (!Zend_Date::isDate($conditionValue['from'], null, 'en_US')) {                                    
    Mage::throwException($invalidDateMessage);
}

The next problematic code where the date format is hardcoded is in: app/code/core/Mage/CatalogSearch/Block/Advanced/Form.php

->setFormat('%m/%d/%y')

The only thing I cannot find is where the search query is build for the date type. It seems that the date format is harcoded to en_US in multiple places throughout the code or the setted locale is totally ignored which really sucks!

Was it helpful?

Solution

I have had similar issue with Date filter :)

The way date time instantiated in the form like that

Step 1:-

In Block Form class Mage_CatalogSearch_Block_Advanced_Form

public function getDateInput($attribute, $part = 'from')
{
    $name = $attribute->getAttributeCode() . '[' . $part . ']';
    $value = $this->getAttributeValue($attribute, $part);

    return $this->_getDateBlock()
        ->setName($name)
        ->setId($attribute->getAttributeCode() . ($part == 'from' ? '' : '_' . $part))
        ->setTitle($this->getAttributeLabel($attribute))
        ->setValue($value)
        ->setImage($this->getSkinUrl('images/calendar.gif'))
        ->setFormat('%m/%d/%y') //So you need change the Format here !!!!!
        ->setClass('input-text')
        ->getHtml();
}

So you need to change the Date format for the calendar in the line ->setFormat('%m/%d/%y')

! Take look about the supported datetime format here http://www.dynarch.com/jscal/#sec34

Then its used in the method below to generate the block

$block = $this->getLayout()->createBlock('core/html_date');
$this->setData('_select_block', $block);

and in that block class Mage_Core_Block_Html_Date

Line 40: $displayFormat = Varien_Date::convertZendToStrFtime($this->getFormat(), true, (bool)$this->getTime());
.......
Line53: ifFormat    : "' . $displayFormat . '",

Step 2:-

In the advanced search collection class Mage_CatalogSearch_Model_Resource_Advanced_Collection

You need to modify this method to allow the new locale or the new datatime format you needed !

Line43: public function addFieldsToFilter($fields) {
.......
.......
.......
.......
Line96: if (!Zend_Date::isDate($conditionValue['from'])) {
.......
Line109:if (!Zend_Date::isDate($conditionValue['to'])) {
.......
}

Now You need to modify those 2 lines with the new datetime format you change the search block to allow this method to be validated

For instance i modified the Javascript calendar format to be %d-%m-%Y which will produce Date in the calendar like ( 25-12-2014 )

Then i modified The method in the above collection class to be like this

if (!Zend_Date::isDate($conditionValue['from'], 'd-m-Y' )) { // I added the format for the validation 
.......
.......
if (!Zend_Date::isDate($conditionValue['to'], 'd-m-Y' )) { // same as above one 

step 3:-

Change those value of date inputs in the class Mage_CatalogSearch_Model_Resource_Advanced_Collection to whatever format or locale.

Everything works fine with that.

I have made small module that rewrite the 2 classes you need to modify based on the locale you need

Check it out https://github.com/Meabed/magento-advanced-search-datetime-field

Regards !

OTHER TIPS

Have you tried swithcing locale in System > Configuration > Change the scope to your store view > General > General > Locale Options, rather than writting into the core files (which is a bad practice). It might solve the first part of your problem.

As for the other problem, you might want to take a look at the JS files such as calendar.js

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top