我正在创建自定义规则以自动将产品添加到类别中。以下代码来自我的观察者。我遇到的问题是,在运行此代码时,没有属性(例如'shirt_color')的产品被添加到我的类别中。
任何人都可以解释为什么会这样吗?出于某种原因,带有“shirt_color”的产品仍会附加“炭灰色”。比较时('charcoal gray' == 'charcoal gray')它解析为'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']
)
);