简体   繁体   中英

Magento - How do I run this custom product attribute script

I have a problem regarding magento attribute. I have created a custom product input text attribute that supposed to hold an Integer Data Type, however, magento stores it as a varchar. I tried to ask about it here in stackoverflow and they told me that there is no way to change product attribute type to integer from string.

So my solution is to create a custom integer product attribute. I search it on google for several days and I found an article that gives a script that creates a custom attribute. http://magentotutorialbeginners.blogspot.com/2014/03/create-product-attribute.html?showComment=1442885130592#c2319234413343201281

The problem is that I don't know how to run this or use it.

QUESTION:

How do this script run? Can you give me a guide about the step by step process?

$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_product', 'custom_mprice', array( 
        'input' => 'text',
        'type' => 'int',
        'label' => 'Enter Max Price',
        'backend' => '',
        'visible' => 1,
        'required' => 0,
        'user_defined' => 1,
        'searchable' => 0,
        'filterable' => 0,
        'sort_order' => 30,
        'comparable' => 0,
        'visible_on_front' => 0,
        'visible_in_advanced_search' => 0,
        'is_html_allowed_on_front' => 0,
        'is_configurable' => 1,
        'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, )); 
$installer->endSetup();

My goal is to create this attribute in order for me to use it in an arithmetic expression like lesser than or equal to.

Thanks

Your collection query look like this.

$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addFieldTofilter($attr_name, array(
        'eq' => Mage::getResourceModel('catalog/product')
            ->getAttribute($attr_name)
            ->getSource()
            ->getOptionId($attr_val)
    ))
    ->addAttributeToSelect('*')
    ->addFieldTofilter($metric, array('gteq' => $metric_val_min))
    ->addFieldTofilter($metric, array('lteq' => $metric_val_max))
   ->load();

The problem that you have described in the question reside in the less than, greater than filtering section.

Here I am showing you an experiment of how you can treat $metric as an integer in sql query and thus the query work in your case.

//create sql expression
$expr1 = 'CAST (' . $metric . ' AS UNSIGNED) >= ?';
$expr2 = 'CAST (' . $metric . ' AS UNSIGNED) <= ?';
$greaterEqCondtion = new Zend_Db_Expr($expr1);
$lesserEqCondition = new Zend_Db_Expr($expr2);

//applying filters which are relatively simple to express.
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addFieldTofilter($attr_name, array(
        'eq' => Mage::getResourceModel('catalog/product')
            ->getAttribute($attr_name)
            ->getSource()
            ->getOptionId($attr_val)
));

//now going to apply filters which is relatively complex in this case.
$collection->getSelect()
    ->where($greaterEqCondtion, $metric_val_min)
    ->where($lesserEqCondition, $metric_val_max);

//we are set, let us load collection
$collection->load();

Here $collection->getSelect()->where() holds an expression. The expression will make the string value to treat as an integer in the sql query. I didn't test this code. But you can make a try on this.

If you really want to go on with new attribute, then what you need to do is create a new setup resource or use default eav setup resource ( Mage_Eav_Model_Entity_Setup ). Follow this tutorial for how to setup an EAV based model

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM