Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Magento中找到没有图像的所有产品?_Magento - Fatal编程技术网

如何在Magento中找到没有图像的所有产品?

如何在Magento中找到没有图像的所有产品?,magento,Magento,我有上千种产品,希望找到所有没有图像的产品。我试图在管理产品网格中搜索(没有图像),但没有结果。我怎样才能使用SQL查询来禁用所有这些产品?不久前,我写了一篇博客文章,用SQL查询来查找丢失的图像。它不会禁用产品,但至少是一个开始:。从这一点上来说应该很容易做到。如果您的属性ID与我的不匹配,您可能必须更改属性ID。停止使用SQL。从Magento的模型开始思考。Magento的模型恰好使用SQL作为后端。通过原始SQL查询东西是可能的,但不同版本的Magento会有所不同,并且可能会因您使用的

我有上千种产品,希望找到所有没有图像的产品。我试图在管理产品网格中搜索(没有图像),但没有结果。我怎样才能使用SQL查询来禁用所有这些产品?

不久前,我写了一篇博客文章,用SQL查询来查找丢失的图像。它不会禁用产品,但至少是一个开始:。从这一点上来说应该很容易做到。如果您的属性ID与我的不匹配,您可能必须更改属性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
}       
//这构建了一个对
//从图像为“无选择”的产品中选择*
$products=Mage::getModel('目录/产品')
->getCollection()
->addAttributeToSelect(“*”)
->addAttributeToFilter('image','no_selection');
foreach($products as$product)
{
echo$product->getSku()“没有图像\n
\n”; //var_dump($product->getData());//取消注释以查看所有产品属性 //删除->添加属性过滤器('image','no_selection'); //从上面看所有的图片,并得到一个想法 //你可以查询的东西 }
另外,要获取Alan描述的查询在后台运行的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'));

祝你好运

我知道这是非常古老的,但我发现它很有用,所以我想我应该发布一个更新

作为以上艾伦回答的补充,我发现除了“无选择”之外,还有其他情况。。可能是由于插件或系统中的一般错误?最终的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' => '%/%/%'
        ),
    ));

对于没有小图像的产品,请尝试此选项

select * from catalog_product_entity_varchar WHERE attribute_id = 86 AND value = 'no_selection'

在eav_属性表中查找属性id我只想补充一点,Sean Michaud的答案是正确的,除非我们不需要使用它

array (
        'attribute' => 'image', // null fields
        'null' => true
    ),
使用该页面:

“空值与长度为零的字符串不同。空 表示没有任何值,SQL标准表示 空值永远不等于任何其他值,包括另一个空值“

因此,
%/%/%
将不会获得空值,但通过添加上面的代码,我们将修复错误并获得具有空值的图像字段。这就是结果

$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
使用
内部
连接,并且没有要连接的图像属性值,因此此处发布的查询无法捕获这些SKU

下面的代码返回图像、小图像或缩略图为空、格式不正确或完全缺少行的所有产品

addAttributeToFilter
的第三个参数允许您指定要与
WHERE
语句的
子句一起使用的联接类型

$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查询以从SQL客户端导出为CSV,只需从PHP脚本打印查询:

echo $products->getSelect();

我在StackOverflow上看到过一些SQL,它们对引用
图像
小图像
缩略图
属性的
属性id
整数进行了硬编码,但不同的安装可能会有所不同。在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

祝你好运

@迈克尔·迈尔斯:这篇文章发布将近一年后,你到底为什么要编辑它?@Zéychin:编辑没有法律限制。任何时候你看到一些可以改进的东西,请随时去做。在这种情况下,我在这里是因为有人发布了一个非答案,我来删除。哦,是的,这是公平的。我刚刚看到:编辑人…,从它的放置方式来看,似乎你显然编辑了问题,而不是删除了(错误的)答案!这更有意义。我向你为这个社区所做的工作致敬。如果你想要所有带有图像的产品呢?@swl1020我还没有测试过这个,但是一个标准的不平等过滤器(
neq
)应该可以工作。`->addAttributeToFilter('image',数组(“neq”=>'no_selection'));`本文提供了完整的筛选选项列表:集合比调试一周中任何一天使用EAV所需的自模糊SQL语句要好得多。这项工作已经为你完成了,你只需要注意你用来处理结果的逻辑。谢谢@AlanStorm,你是天才