微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 在Magento中按数据属性选择产品

我创建了一个带有自定义数据属性的新产品.

$product = Mage::getModel('catalog/product');
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setName('name');
$product->setDescription('description');
$product->setPrice(1.24);
$product->setData('lid', 123);
$product->save

现在我想使用Mage :: getModel(‘catalog / product’)查询此产品.

如何查询带有’lid’123的产品?

如何查询存在’lid’的产品,又不是null?

================

使用addAttributetoFilter执行查询时的错误日志.

( ! ) Fatal error: Call to a member function getBackend() on boolean in /Users/asdf/Sites/magento19.dev/app/code/core/Mage/Eav/Model/Entity/Abstract.PHP on line 816
Call Stack
#   Time    Memory  Function    Location
1   0.0002  249016  {main}( )   .../index.PHP:0
2   0.0024  514024  Mage::run( )    .../index.PHP:87
3   0.0085  1433456 Mage_Core_Model_App->run( ) .../Mage.PHP:684
4   0.0729  7141088 Mage_Core_Controller_Varien_Front->dispatch( )  .../App.PHP:354
5   0.0776  7404488 Mage_Core_Controller_Varien_Router_Standard->match( )   .../Front.PHP:172
6   0.0825  7941968 Mage_Core_Controller_Varien_Action->dispatch( ) .../Standard.PHP:250
7   0.1205  10859488    Asdf_Sync_Adminhtml_SyncbackendController->indexAction( )   .../Action.PHP:418
8   0.1535  13447744    Asdf_Sync_Adminhtml_SyncbackendController->performSync( )   .../SyncbackendController.PHP:18
9   0.1542  13504568    Asdf_Sync_Helper_Data->pushProducts( )  .../SyncbackendController.PHP:33
10  0.3723  17554640    Mage_Catalog_Model_Resource_Product_Collection->addAttributetoFilter( ) .../Data.PHP:67
11  0.3723  17555008    Mage_Eav_Model_Entity_Collection_Abstract->addAttributetoFilter( )  .../Collection.PHP:1438
12  0.3723  17555360    Mage_Eav_Model_Entity_Collection_Abstract->_getAttributeConditionsql( ) .../Abstract.PHP:321
13  0.3723  17555472    Mage_Eav_Model_Entity_Abstract->isAttributeStatic( )    .../Abstract.PHP:1379

解决方法:

对于create attribute,在magento根目录中运行此脚本(可以根据需要进行编辑)

<?PHP  

require_once('app/Mage.PHP');
 Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();                   

$installer->addAttribute('catalog_product', 'lid', array(
             'label'             => 'LId',
             'type'              => 'int',
             'input'             => 'text',
             'backend'           => '',
             'frontend'          => '',
             'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::ScopE_GLOBAL,
             'visible'           => true,
             'required'          => false,
             'user_defined'      => false,
             'searchable'        => false,
             'filterable'        => false,
             'comparable'        => false,
             'visible_on_front'  => true,
             'visible_in_advanced_search' => false,
             'unique'            => false
));
$installer->endSetup();

?>

现在您的属性已创建,请参阅我有created
然后设置你想要的值see

属性尝试过滤产品.

$product = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributetoFilter('lid', 123);

foreach ($product as $products) { 
    echo '<pre>';
    print_r($products);
}

通过Lid Exist获取产品

$product = Mage::getModel('catalog/product')
    ->getCollection();


foreach ($product as $products) { 
    if($products->getLid()){
       echo $products->getName().'-->'.$products->getLid().'<br>';
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐