编程语言
首页 > 编程语言> > php – 在Magento中按数据属性选择产品

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>';
    }
}

标签:php,magento,magento-1-9
来源: https://codeday.me/bug/20190701/1352262.html