1

I created new attribute for products with time input in setup of my module, Setup code is here:

class Parseh_Course_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup {   
     public function getDefaultEntities()   {
            return array(
              'catalog_product' => array(
                'entity_model'                   => 'catalog/product',
                'attribute_model'                => 'catalog/resource_eav_attribute',
                'table'                          => 'catalog/product',
                'additional_attribute_table'     => 'catalog/eav_attribute',
                'entity_attribute_collection'    => 'catalog/product_attribute_collection',
                'attributes'                     => array(
                  'course_end_time' => array(
                    'type'                       => 'varchar',
                    'label'                      => 'End Time',
                    'input'                      => 'time',
                    'backend'                    => 'parseh_course/course_attribute_backend_time',
                    'required'                   => true,
                    'user_defined'               => false,
                    'sort_order'                 => 4,
                    'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                    'used_in_product_listing'    => true,
                    'visible_on_front'           => true,
                    'apply_to'                   => Parseh_Course_Model_Product_Type::TYPE_COURSE,
                    'group'                      => 'Scheduling',
                  ),

                )
              )
            );   
      } 
}

As you see I used "parseh_course/course_attribute_backend_time" model for this input backend.

class Parseh_Course_Model_Course_Attribute_Backend_Time extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
  public function beforeSave($object) {
    $attributeCode = $this->getAttribute()->getName();
    $data = $object->getData($attributeCode);
    if (!is_array($data)) {
      $data = array();
    }
    $object->setData($attributeCode, join(':', $data));
    if (is_null($object->getData($attributeCode))) {
      $object->setData($attributeCode, false);
    }
  }

  public function afterLoad($object) {
    $attributeCode = $this->getAttribute()->getName();
    $data = $object->getData($attributeCode);
    if ($data) {
      $object->setData($attributeCode, explode(':', $data));
    }

    return $this;
  }
}

In beforeSave function I join hour, minute and seconds with ":" and make a string to save in database. In afterLoad function the value of attribute loads correct same as "10:30:00" but when I explode it and create an array of data to fill time inputs in edit form of product, values of hour, minute, second inputs are 00.

Please help me!

  • 1
    what happens if you simply log the results of the explode? – David Manners May 29 '14 at 18:17
1

Try the following:
Instead of

$object->setData($attributeCode, explode(':', $data));

use this:

$object->setData($attributeCode, implode(',',explode(':', $data)));

this will transform the value from 10:30:00 to 10,30,00.
And here is why this should work.
In the class Varien_Data_Form_Element_Time, the one that renders the time type element there is this code in the method getElementHtml.

    if( $value = $this->getValue() ) {
        $values = explode(',', $value);
        if( is_array($values) && count($values) == 3 ) {
            $value_hrs = $values[0];
            $value_min = $values[1];
            $value_sec = $values[2];
        }
    }

So Magento expects a string value with the values for hour, minute and second separated by comma. (See second line in the code above)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.