Magento如果产品有属性,请获取它的ID [英] Magento If product has attribute, get it's ID

查看:97
本文介绍了Magento如果产品有属性,请获取它的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取具有特定值属性的产品的产品ID。

I need to get the product ID for product that have an attribute with a certain value.

这是我到目前为止所拥有的:

Here's what I have so far:

$found = false;

foreach ($_productCollection as $_product) {
    $attribute = $_product->getResource()->getAttribute('attribute');
    $id = $_product->getId();

    if ($attribute = NULL) {
        // Do Nothing
    }
    if ($attribute = "value1") {
        echo $attribute . "<br>";
        $value1 = $id;
    }
    if ($attribute = "value2") {
        echo $attribute . "<br>";
        $value2 = $id;
    }
    if ($attribute = "value3") {
        echo $attribute . "<br>";
        $value3 = $id;
    }

    if ($value1 && $value2 && $value3) {
        $found = true;
        echo "All IDs Found" ."<br>";
    }
    if ($found = true) {
        break;
    }
}

echo "Value 1 ID = " . $value1 ."<br>". "Value 2 ID = " . $value2 ."<br>". "Value 3 ID = " . $value3;

这主要是工作,它正确地循环产品,并且可以获得每个产品的属性值。但它显示了该类别中第一个产品的ID,对于每个产品,无论它们具有什么属性。

This mostly work, it loops through the products correctly, and can get the attribute values for each product. But it is showing the ID for the first product in the category, for every product regardless of what attribute they have.

喜欢这样:

value1
value2
value3
All IDs Found
Value 1 ID = 17024
Value 2 ID = 17024 (should be 17025)
Value 3 ID = 17024 (should be 17026)

如何在类别中包含具有该属性的产品,以及具体的产品之一值,它会列出产品的ID吗?

How can I get it so that if there is a product in the category with the attribute, and one of the specific values, it will list that products' ID?

编辑:

使用==修复了我的部分内容问题。

Using == has fixed part of my issue.

另一个问题是使用:

$_product->getResource()->getAttribute('attribute');

没有以允许我使用它的值的方式获取属性。改为使用它:

Was not getting the attribute in a way that allowed me to use it's values. By using this instead:

$_product->getAttributeText('attribute');

并将属性设置为在产品列表中使用。这允许我为每个值使用if语句。

and setting the attribute to 'Use in product Listing'. That allowed me to use if statements, for each value.

推荐答案

您每次都要分配值:

if ($attribute = "value1") {

使用double进行比较:

Use double to compare:

if ($attribute == "value1") {

这篇关于Magento如果产品有属性,请获取它的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆