脚本宝典收集整理的这篇文章主要介绍了php – Magento – 如何运行此自定义产品属性脚本,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我的解决方案是创建一个自定义整数产品属性.我在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
以上是脚本宝典为你收集整理的php – Magento – 如何运行此自定义产品属性脚本全部内容,希望文章能够帮你解决php – Magento – 如何运行此自定义产品属性脚本所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。