1

I created a custom source model for two attribute. But my Layered navigation can not find that value of that attribute. So i want to replicate my custom_attribute_value from my data base to eav_attribute_option...i need any idea how to do this. Basically i created a custom source type model...and want to replicate those value from my database to magento . i created attribute by this code..

$installer = $this;
$setup = new Mage_Catalog_Model_Resource_Setup('core_setup');
$installer->startSetup();

$attr1 = array(
    'attribute_model' => NULL,
    'backend' => '',
    'type' => 'int',
    'table' => '',
    'frontend' => '',
    'input' => 'select',
    'label' => 'Brands',
    'frontend_class' => '',
    'source' => 'designers/attribute_source_brands',
    'required' => '1',
    'user_defined' => '1',
    'default' => '',
    'unique' => '0',
    'note' => '',
    'input_renderer' => NULL,
    'global' => '1',
    'visible' => '1',
    'searchable' => '1',
    'filterable' => '1',
    'comparable' => '1',
    'visible_on_front' => '1',
    'is_html_allowed_on_front' => '1',
    'is_used_for_price_rules' => '1',
    'filterable_in_search' => '1',
    'used_in_product_listing' => '1',
    'used_for_sort_by' => '0',
    'is_configurable' => '0',
    'visible_in_advanced_search' => '1',
    'position' => '1',
    'group' => 'General',
    'wysiwyg_enabled' => '0',
    'used_for_promo_rules' => '1');
$setup->addAttribute('catalog_product', 'brands', $attr1);

$attr2 = array(
    'attribute_model' => NULL,
    'backend' => '',
    'type' => 'int',
    'table' => '',
    'frontend' => '',
    'input' => 'select',
    'label' => 'Designers',
    'frontend_class' => '',
    'source' => 'designers/attribute_source_designers',
    'required' => '1',
    'user_defined' => '1',
    'default' => '',
    'unique' => '0',
    'note' => '',
    'input_renderer' => NULL,
    'global' => '1',
    'visible' => '1',
    'searchable' => '1',
    'filterable' => '1',
    'comparable' => '1',
    'visible_on_front' => '1',
    'is_html_allowed_on_front' => '1',
    'is_used_for_price_rules' => '1',
    'filterable_in_search' => '1',
    'used_in_product_listing' => '1',
    'used_for_sort_by' => '0',
    'is_configurable' => '0',
    'visible_in_advanced_search' => '1',
    'position' => '1',
    'group' => 'General',
    'wysiwyg_enabled' => '0',
    'used_for_promo_rules' => '1');
$setup->addAttribute('catalog_product', 'designers', $attr2);

My source model for brand attribute

class Csipl_Designers_Model_Attribute_Source_Brands extends Mage_Eav_Model_Entity_Attribute_Source_Abstract {

    protected $_options = null;

    public function getAllOptions($withEmpty1 = false) {
        $collection = Mage::getModel('designers/brands')->getCollection()

                ->addFieldToSelect('*')
                ->addFieldToFilter('status', array('eq' => 'ENABLED'));
        $collection->load();
        if (is_null($this->_options)) {
            $this->_options = array();
            $result = array();
            foreach ($collection as $brand) {
                $this->_options[] = array(
                    'value' => $brand->getBrandId(),
                    'label' => $brand->getBrandName()
                );
            }
        }
        $options = $this->_options;
        if (!empty($options)) {
            if ($withEmpty1) {
                array_unshift($options, array('value' => '', 'label' => 'Please Select Brands'));
            }
        } else {

            if ($withEmpty1) {
                array_unshift($options, array('value' => '', 'label' => 'Please Add Brands In Brand Management '));
            }
        }
       //print_r($options);exit;
        return $options;
    }

    public function getOptionText($value) {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }

    public function getFlatColums() {
        $attributeCode = $this->getAttribute()->getAttributeCode();
        $column = array(
            'unsigned' => false,
            'default' => null,
            'extra' => null
        );

        if (Mage::helper('core')->useDbCompatibleMode()) {
            $column['type'] = 'int(10)';
            $column['is_null'] = true;
        } else {
            $column['type'] = Varien_Db_Ddl_Table::TYPE_SMALLINT;
            $column['length'] = 10;
            $column['nullable'] = true;
            $column['comment'] = $attributeCode . ' column';
        }

        return array($attributeCode => $column);
    }

    public function getFlatUpdateSelect($store) {
        return Mage::getResourceModel('eav/entity_attribute')
                        ->getFlatUpdateSelect($this->getAttribute(), $store);
    }

}

My source model for designers attribute

class Csipl_Designers_Model_Attribute_Source_Designers extends Mage_Eav_Model_Entity_Attribute_Source_Abstract {

    protected $_options = null;

  public function getAllOptions($withEmpty1 = false) {
        $collection = Mage::getModel('designers/designers')->getCollection()
                ->addFieldToSelect('*')
                ->addFieldToFilter('status', array('eq' => 'ENABLED'));
        $collection->load();
        if (is_null($this->_options)) {
            $this->_options = array();
            $result = array();
            foreach ($collection as $designer) {
                $this->_options[] = array(
                    'value' => $designer->getDesignerId(),
                    'label' => $designer->getDesignerName()
                );
            }
        }
        $options = $this->_options;
        if (!empty($options)) {
            if ($withEmpty1) {
                array_unshift($options, array('value' => '', 'label' => 'Please Select Designers'));
            }
        } else {

            if ($withEmpty1) {
                array_unshift($options, array('value' => '', 'label' => 'Please Add Designers In Designer Management '));
            }
        }
      // print_r($options);exit;
        return $options;
    }

    public function getOptionText($value) {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }

    public function getFlatColums() {
        $attributeCode = $this->getAttribute()->getAttributeCode();
        $column = array(
            'unsigned' => false,
            'default' => null,
            'extra' => null
        );

        if (Mage::helper('core')->useDbCompatibleMode()) {
            $column['type'] = 'int(10)';
            $column['is_null'] = true;
        } else {
            $column['type'] = Varien_Db_Ddl_Table::TYPE_SMALLINT;
            $column['length'] = 10;
            $column['nullable'] = true;
            $column['comment'] = $attributeCode . ' column';
        }

        return array($attributeCode => $column);
    }

    public function getFlatUpdateSelect($store) {
        return Mage::getResourceModel('eav/entity_attribute')
                        ->getFlatUpdateSelect($this->getAttribute(), $store);
    }

}
4
  • have you created source models ? – Minesh Patel Dec 7 '15 at 6:14
  • @MineshPatel yes...but my layered navigation not listing value of attribute from my source model...so i want to replicate that in eav_attribute_option – Deepak Rai Dec 7 '15 at 6:19
  • can you place source model code here ? it should work if you have assigned attribute value to product – Minesh Patel Dec 7 '15 at 6:30
  • @MineshPatel I updated my question with source model – Deepak Rai Dec 7 '15 at 6:40

Your Answer

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

Browse other questions tagged or ask your own question.