Magento 2函数每次都返回相同的值

时间:2017-12-14 12:51:33

标签: php magento magento2

我正在为我的产品页面编写一些代码来获取两个单独属性组中的一些是/否属性,如果属性值为1(是),我可以使用它来显示图像。 / p>

以下是块(Product.php)中的代码:

/**
 * Gets the attribute group of a product via its ID
 *
 * @param $groupID int: the attribute group ID
 * @return collection of product attributes
 */
public function getAttributeGroup($groupId) {
    return $this->attributeCollection->setAttributeGroupFilter($groupId);
}

/**
 * Gets the information of an attribute in an array
 *
 * @param $label string: the label of the attribute
 * @param $code string: the code of the attribute
 * @param $loadOption boolean: whether to load the options
 * @return array: the array with the info
 */
public function getAttributeArray($label, $code, $loadOption = false) {
    $value = (
        $loadOption ?
        $this->product->getResource()->getAttribute($code)->getFrontend()->getValue($this->product) :
        $this->product->getData($code)
    );
    if($value) {
        $res = array();
        $res["label"] = $label;
        $res["value"] = $value;
        return $res;
    }
    return false;
}

/**
 * Displays key stage and other icons on product page
 *
 * @param $groupId int: the attribute group ID
 * @return array/boolean: the array with the info or false
 */
public function displayIcons($groupId) {
    $icons = $this->getAttributeGroup($groupId);
    if (!$icons) {
        throw new Exception('Could not get attribute group.');
    }
    $iconAttributes = array();
    foreach($icons as $icon) {
        $data = $icon->getData();
        if (!$data) {
            throw new Exception('Could not get attribute data.');
        }
        $iconAttribute = $this->getAttributeArray($data["frontend_label"], $data["attribute_code"]);
        if ($iconAttribute["value"] == 1) {
            $iconAttribute["code"] = $data["attribute_code"];
            array_push($iconAttributes, $iconAttribute);
        }
    }
    return $iconAttributes;
}

在product.phtml中:

try {
    $icons = $block->displayIcons(34);
    $keyStageIcons = $block->displayIcons(35);
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
}

print_r($icons);
print_r($keyStageIcons);

问题是它无法正确获得第二组的结果。当我最后打印时,我得到第一组两次:

Array
(
    [0] => Array
        (
            [label] => Competitive Play
            [value] => 1
            [code] => competitive
        )

    [1] => Array
        (
            [label] => Co-Operative Play
            [value] => 1
            [code] => coopplay
        )

    [2] => Array
        (
            [label] => Hand-eye Co-ordination
            [value] => 1
            [code] => handeye
        )

    [3] => Array
        (
            [label] => Physical Fitness
            [value] => 1
            [code] => physical
        )

)
Array
(
    [0] => Array
        (
            [label] => Competitive Play
            [value] => 1
            [code] => competitive
        )

    [1] => Array
        (
            [label] => Co-Operative Play
            [value] => 1
            [code] => coopplay
        )

    [2] => Array
        (
            [label] => Hand-eye Co-ordination
            [value] => 1
            [code] => handeye
        )

    [3] => Array
        (
            [label] => Physical Fitness
            [value] => 1
            [code] => physical
        )

)

如果我翻转函数调用并先放入$ keyStageIcons,那么我会得到两次:

Array
(
    [0] => Array
        (
            [label] => Key Stage 1
            [value] => 1
            [code] => ks1
        )

    [1] => Array
        (
            [label] => Key Stage 2
            [value] => 1
            [code] => ks2
        )

)
Array
(
    [0] => Array
        (
            [label] => Key Stage 1
            [value] => 1
            [code] => ks1
        )

    [1] => Array
        (
            [label] => Key Stage 2
            [value] => 1
            [code] => ks2
        )

)

为什么当我再次调用该函数时,它不会让我成为第二组?就像我的displayIcons函数的变量每次被调用时都没有被重置一样。

0 个答案:

没有答案