Magento-如何运行此自定义产品属性脚本 [英] Magento - How do I run this custom product attribute script
问题描述
我对magento属性有疑问.我创建了一个自定义产品输入文本属性,该属性应该保存一个Integer数据类型,但是magento将其存储为varchar.我在这里试图在stackoverflow中问这个问题,他们告诉我没有办法将产品属性类型从字符串更改为整数.
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.
所以我的解决方案是创建一个自定义整数乘积属性.我在Google上搜索了几天,发现了一篇提供创建自定义属性的脚本的文章. http://magentotutorialbeginners.blogspot.com/2014 /03/create-product-attribute.html?showComment=1442885130592#c2319234413343201281
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.
问题:
此脚本如何运行?您能给我有关逐步过程的指南吗?
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.
谢谢
推荐答案
您的集合查询如下.
$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.
在这里,我向您展示了一个实验,该实验如何在SQL查询中将$metric
视为整数,从而使查询在您的情况下起作用.
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();
此处$collection->getSelect()->where()
保存一个表达式.该表达式将使字符串值在sql查询中视为整数.我没有测试此代码.但是您可以尝试一下.
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.
如果您确实要继续使用新属性,那么您需要做的是创建一个新的设置资源或使用默认的eav设置资源(Mage_Eav_Model_Entity_Setup
).遵循 本教程 ,了解如何设置基于EAV的模型
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
这篇关于Magento-如何运行此自定义产品属性脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!