php – Magento – 如何运行此自定义产品属性脚本

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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

脚本宝典总结

以上是脚本宝典为你收集整理的php – Magento – 如何运行此自定义产品属性脚本全部内容,希望文章能够帮你解决php – Magento – 如何运行此自定义产品属性脚本所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。