我有一千种产品,希望找到没有图像的所有产品。我试图在管理产品网格中搜索(无图像),但没有结果。我怎样才能做一个SQL查询来禁用所有这些产品?如何在Magento中找到没有图像的所有产品?
回答
我写了一篇博客文章,然后用sql查询找到丢失的图像。它不会禁用产品,但至少是一个开始:http://prattski.com/2010/06/29/magento-sql-to-find-missing-images/。从这一点开始应该很容易做到。如果您的配置不符合我的要求,您可能需要更改属性ID。
停止思考SQL。从Magento的模型开始思考。 Magento的模型恰好使用SQL作为后端。通过原始SQL查询事情是可能的,但是会根据Magento的版本而有所不同,并且可能会因您使用的后端而有所不同。
从测试控制器操作或其他可以执行Magento代码的地方运行以下命令。它查询产品型号无图像
//this builds a collection that's analagous to
//select * from products where image = 'no_selection'
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('image', 'no_selection');
foreach($products as $product)
{
echo $product->getSku() . " has no image \n<br />\n";
//var_dump($product->getData()); //uncomment to see all product attributes
//remove ->addAttributeToFilter('image', 'no_selection');
//from above to see all images and get an idea of
//the things you may query for
}
如果您想要所有带有图像的产品,该怎么办? – swl1020 2013-02-26 20:12:21
@ swl1020我还没有测试过这个,但是一个标准的不等于过滤器('neq')应该可以工作。 '' - > addAttributeToFilter('image',array(“neq”=>'no_selection'));'本文中提供了完整的过滤选项列表:http://alanstorm.com/magento_collections – 2013-02-27 01:14:42
集合远胜于调试一周中任何一天处理EAV所需的自我强化SQL语句。这项工作已经为您完成,您只需要注意您使用的逻辑来处理结果。 – 2013-03-09 20:16:13
也得到了查询艾伦描述了幕后运行的SQL:
echo (string) $products->getSelect();
有两种方法可以做到:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('small_image',array('notnull'=>'','neq'=>'no_selection'));
上面的代码应该工作,但在我的情况下,它不工作。所以我尝试以下:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('small_image',array('neq'=>'no_selection'));
祝你好运!
我知道这是超旧的,但我发现它很有帮助,所以我想我会发布更新。
作为Alan上述答案的补充,我发现除了'no_selection'之外,还有其他场景可能是由于插件或系统中的一般错误?最后的nlike会真的找到所有的东西,但我只是为了好玩而留下其他人。
改变集合查询,如下所示:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(array(
array (
'attribute' => 'image',
'like' => 'no_selection'
),
array (
'attribute' => 'image', // null fields
'null' => true
),
array (
'attribute' => 'image', // empty, but not null
'eq' => ''
),
array (
'attribute' => 'image', // check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
));
对于没有小图像产品试图在eav_attribute表这个
select * from catalog_product_entity_varchar WHERE attribute_id = 86 AND value = 'no_selection'
发现attribute_id
我只想补充一点的答案肖恩米肖是正确的,除了我们不需要使用那个
array (
'attribute' => 'image', // null fields
'null' => true
),
使用该网页:http://bytes.com/topic/sql-server/answers/83267-wildcard-doesnt-match-using-like-varchar-field-wierd
“NULL值是不一样零长度的字符串。NULL 表示没有任何价值的,而SQL标准说,一个 NULL值永远不会与任何其他价值,包括另一个NULL”
所以%/%/%
不会得到NULL值,但添加的代码从上面我们会修正这个错误,并得到与NULL值图像领域。这是结果
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(array(
array (
'attribute' => 'image', // null fields
'null' => true
),
array (
'attribute' => 'image', // check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
));
如果你想工作的所有图像属性,代码可能是这样的
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(array(
array (
'attribute' => 'image', //Check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
array (
'attribute' => 'small_image', //Check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
array (
'attribute' => 'thumbnail', //Check for information that doesn't conform to Magento's formatting
'nlike' => '%/%/%'
),
array (
'attribute' => 'image', //Check for null fields
'null' => true
),
array (
'attribute' => 'small_image', //Check for null fields
'null' => true
),
array (
'attribute' => 'thumbnail', //Check for null fields
'null' => true
),
));
我以各种组合尝试了所有这些答案,只返回了我的目录的一小部分。原因:我最初使用定制的产品图像导入脚本导入我的产品。
如果我在导入期间没有为某些行指定图像,那么脚本不会为这些图像创建NULL
或空属性值。它根本没有创建属性行。
由于addAttributeToFilter
默认使用INNER
连接,并且没有要加入的图像属性值,所以此处发布的查询未收到这些SKU。
以下代码返回该图像,small_image 或缩略图为空的所有产品,格式不正确,或该行被完全丢失。
addAttributeToFilter
的第三个参数允许您指定要与WHERE
语句的OR
子句一起使用的连接类型。
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array(
'attribute' => 'image',
'null' => '1'
),
array(
'attribute' => 'small_image',
'null' => '1'
),
array(
'attribute' => 'thumbnail',
'null' => '1'
),
array(
'attribute' => 'image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'small_image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'thumbnail',
'nlike' => '%/%/%'
)
),
null,
'left'
);
如果像我一样,你想将其转换为一个SQL查询来为CSV从SQL客户端出口,只需打印从PHP脚本查询:
echo $products->getSelect();
我已经看到StackOverflow上发布了一些SQL,它硬编码了attribute_id
整数,这些整数涉及image
,small_image
和thumbnail
属性,但这些属性可能因安装而异。在Magento中,使用ORM查询要比使用SQL好得多。
我尝试了所有,但是当平板目录是使
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
array(
array(
'attribute' => 'image',
'null' => '1'
),
array(
'attribute' => 'small_image',
'null' => '1'
),
array(
'attribute' => 'thumbnail',
'null' => '1'
),
array(
'attribute' => 'image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'small_image',
'nlike' => '%/%/%'
),
array(
'attribute' => 'thumbnail',
'nlike' => '%/%/%'
)
),
null,
'left'
);
您可以使用此SQL只是看哪个产品不具备图片此为我的作品:
SELECT * FROM catalog_product_entity_media_gallery RIGHT OUTER JOIN catalog_product_entity ON catalog_product_entity.entity_id = catalog_product_entity_media_gallery.entity_id WHERE catalog_product_entity_media_gallery.value is NULL
祝您好运!
- 1. Magento - 如何隐藏没有图像的产品
- 2. 导出magento中没有图像的产品
- 3. Magento:显示产品页面上的所有图像(view.phtml)
- 4. 获取产品的所有Magento图像(原始尺寸)
- 5. Magento:防止没有图像显示的产品
- 6. 如何仅使用mysql在magento中拉出所有产品ID,skus,产品名称,描述,图像,类别?
- 7. 如何从Magento的1.4.2所有产品数据导入到1.9.0
- 8. Magento Pager没有显示在产品list.phtml
- 9. 如何获得随机产品,如果我没有在magento的相关产品
- 10. Magento的1.4 - 显示所有产品
- 11. 删除Magento上的所有产品
- 12. 给Magento的所有产品打折
- 13. Magento - 显示所有产品的总价
- 14. 在magento产品页面中没有得到正确的输出
- 15. 如何在Magento的图像产品上重叠2张图像?
- 16. magento产品图像样本
- 17. Magento简单产品图像
- 18. 我如何获得所有Magento产品品牌
- 19. 如何使用Magento中没有websiteID的产品进行产品收藏?
- 20. Magento PHP:没有父母的产品
- 21. Magento - 没有注册的产品提示
- 22. Magento的1.7产品图像导入未分配图像产品
- 23. 如何删除Magento中的所有目录产品
- 24. 如何删除/清除magento中的所有产品评论?
- 25. 如何将附加图像添加到Magento中的产品
- 26. Magento找到孤儿产品
- 27. Magento - 获取所有产品的产品集合
- 28. Magento - 获取分组产品的所有关联产品
- 29. Magento - 导入所有产品/进口产品库存的区别?
- 30. 删除所有产品属性magento
@Michael Myers:你为什么在发布后几乎整整一年编辑它? – 2011-07-15 17:46:48
@ Zyychin:对编辑没有任何限制。任何时候你看到一些可以改进的东西,请随时继续并做到这一点。在这种情况下,我在这里是因为有人发布了一个我未删除的答案。 – 2011-07-15 17:53:31
哦,是的,这很公平。 我只看到:由...编辑,并且从放置的方式来看,似乎你显然编辑了这个问题,而没有删除(错误)答案! 这样做更有意义。我向这个社区致敬。 – 2011-07-15 17:58:58