Magento - How to run this custom product attribute script

I have a problem with the magento attribute. I created a custom text input attribute for the product that should contain the Integer Data Type, however magento saves it as varchar. I tried asking about it here on stackoverflow, and they told me that there is no way to change the type of a product attribute to an integer from a string.

So my solution is to create a custom integer product attribute. I have been browsing 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 do not know how to run or use it.

Question:

How to do this script? Can you give me a step-by-step guide?

$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 so that it can be used in an arithmetic expression that is less than or equal to.

thanks

+4
source share
1 answer

Your collection request is as follows.

$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 described in the question is in a section smaller than filtering.

Here I will show you how you can consider it $metricas an integer in a sql query and, therefore, the query works 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();

$collection->getSelect()->where() . sql. . .

, eav (Mage_Eav_Model_Entity_Setup). EAV

+1

Source: https://habr.com/ru/post/1608416/


All Articles