Magento - 加载产品时属性不好?

时间:2012-06-15 18:17:30

标签: php magento

我正在创建自定义规则,以自动将产品添加到类别中。以下代码来自我的观察者。我遇到的问题是,在运行此代码时,没有属性的产品(例如'shirt_color')会​​被添加到我的类别中。

有人能说清楚为什么会这样吗?出于某种原因,“炭灰色”仍然附着在没有'shirt_color'的产品上。比较时('charcoal grey'=='charcoal grey')它会解析为'true',这就是为什么$ r == 1;

感谢。

示例输出:

#product w/ a 'shirt_color' attribute
productEntityId=628
productName=Rogue GRAY International Shirt XXL
productSKU=HW0003-XXL
string ==
product attributeSTRTOLOWER='charcoal grey'
value STRTOLOWER='charcoal grey'
r=1

#product w/o a 'shirt_color' attribute
productEntityId=629
productName=O'Neill Hyperfreak White
productSKU=
string ==
product attributeSTRTOLOWER='charcoal grey'
value STRTOLOWER='charcoal grey'
r=1

代码:

public function onCategoryRuleSave($observe)
{
    $model = Mage::getModel('catalog/product');
    $collection =
        $model->getCollection()
            ->addAttributeToSelect('entity_id');
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');

    foreach ($collection as $product) {
echo "productEntityId=" . $product->getId() . "<br>";
        $product = $product->load($product->getId());
        $productAttributeValue =
            $model->load($product->getId())
                ->getAttributeText( $observe['attribute_code'] );
        $r = 0;

echo "productName=" . $product->getName() . "<br>";
echo "productSKU=" . $product->getSku() . "<br>";
        if ( is_numeric($observe['value']) ) {
echo "operator= " . $observe['operator'] . "<br>";
            switch($observe['operator']) {
                case "<":
echo "numeric <<br>";
                    $r = ($productAttributeValue < $observe['value']) ? 1 : 0;
                    break;
                case ">":
echo "numeric ><br>";
                    $r = ($productAttributeValue > $observe['value']) ? 1 : 0;
                    break;
                case "<=":
echo "numeric <=<br>";
                    $r = ($productAttributeValue <= $observe['value']) ? 1 : 0;
                    break;
                case ">=":
echo "numeric >=<br>";
                    $r = ($productAttributeValue >= $observe['value']) ? 1 : 0;
                    break;
                case "==":
echo "numeric ==<br>";
                    $r = ($productAttributeValue == $observe['value']) ? 1 : 0;
                    break;
                case "!=":
echo "numeric !=<br>";
                    $r = ($productAttributeValue != $observe['value']) ? 1 : 0;
                    break;
            }
        }
        else {
            switch($observe['operator']) {
                case "==":
echo "string ==<br>";
echo "product attributeSTRTOLOWER='" . strtolower($productAttributeValue) . "'<br>";
echo "value STRTOLOWER='" . strtolower($observe['value']) . "'<br>";
                    $r = (
                        strtolower($productAttributeValue) == strtolower($observe['value'])
                    ) ? 1 : 0;
echo "r=" . $r . "<br>";
                    break;
                case "!=":
echo "string !=<br>";
echo "product attributeSTRTOLOWER='" . strtolower($productAttributeValue) . "'<br>";
echo "value STRTOLOWER='" . strtolower($observe['value']) . "'<br>";
                    $r = (
                        strtolower($productAttributeValue) != strtolower($observe['value'])
                    ) ? 1 : 0;
echo "r=" . $r . "<br>";
                    break;
            }
        }
echo "<br>";
        if ($r==1) {
            $write->query(
                "REPLACE INTO `catalog_category_product` (`category_id`, `product_id`)
                    VALUES (" . $observe['category_id'] . "," . $product->getId() . ")"
            );
        }
    }
die();

}

从控制器传递:

Mage::dispatchEvent(
    'category_rule_save',
    array(
        'rule_id'        => $id,
        'attribute_code' => $data['attribute_code'],
        'operator'       => $data['operator'],
        'value'          => $data['value'],
        'category_id'    => $data['category'],
        'store_id'       => $data['store_id']
    )
);

1 个答案:

答案 0 :(得分:0)

找出问题所在。因为我之前宣布过$model = Mage::getModel('catalog/product');,后来有些东西搞乱了。而不是

$model->load($product->getId())

我现在有

Mage::getModel('catalog/product')->load($product->getId())

我为Mage::getModel('catalog/product')->getCollection()做了同样的事。