php – Magento – 如何运行此自定义产品属性脚本
我有关于magento属性的问题.我创建了一个自定义产品输入文本属性,该属性应该包含整数数据类型,但是,magento将其存储为varchar.我试着在stackoverflow中询问它,他们告诉我没有办法将产品属性类型从字符串更改为整数.
所以我的解决方案是创建一个自定义整数产品属性.我在google上搜索了好几天,然后我发现了一篇文章,它提供了一个创建自定义属性的脚本. http://magentotutorialbeginners.blogspot.com/2014/03/create-product-attribute.html?showComment=1442885130592#c2319234413343201281 问题是我不知道如何运行或使用它. 题: 这个脚本是如何运行的?你能给我一个关于一步一步过程的指南吗? $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(); 我的目标是创建这个属性,以便我在像小于或等于的算术表达式中使用它. 谢谢 解决方法
您的收藏查询如下所示.
$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(); 您在问题中描述的问题存在于小于,大于过滤部分. 在这里,我向您展示了如何在$sql查询中将$metric视为一个整数的实验,从而在您的情况下查询工作. //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() – >其中()包含一个表达式.该表达式将使字符串值在sql查询中被视为整数.我没有测试这段代码.但你可以尝试一下这个. 如果您真的想继续使用新属性,那么您需要做的是创建新的设置资源或使用默认的eav设置资源(Mage_Eav_Model_Entity_Setup).关注如何设置基于EAV的模型的this tutorial (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |