doulin1867
2012-06-15 18:17
浏览 19
已采纳

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

I'm creating custom rules to automagically add products to categories. The below code is from my observer. The problem I encounter is that upon running this code, products without an attribute ('shirt_color' for example) get added to my category.

Can anyone shed some light as to why this is happening? For some reason, 'charcoal grey' is still being attached to the product w/o a 'shirt_color'. When compared ('charcoal grey' == 'charcoal grey') it resolves to 'true' and that is why $r == 1;

Thanks.

Example output:

#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

Code:

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();

}

Passed from the controller:

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条回答 默认 最新

  • dongweihuan8610 2012-06-18 16:18
    已采纳

    Figured out the problem. Because I declared $model = Mage::getModel('catalog/product'); earlier, something was messing up later on. Instead of

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

    I now have

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

    I did the same for Mage::getModel('catalog/product')->getCollection().

    打赏 评论

相关推荐 更多相似问题