我觉得我的解决方案很接近(但我可能想知道我的运气)我试图写一个简单的PHP脚本来更新Magento中特定类别的所有价格。以下是我的代码请帮帮我。问题是它不会返回任何结果。如果我拿出专门针对类别的部分,那么它对每个产品都可以正常运行。
<?php
require 'app/Mage.php'; Mage::app();
$products = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter()
->addAttributeToSelect('name')
->addAttributeToSelect('price')
;
$_products = Mage::getModel('catalog/product')->load($productId);
$_category = $_products->getCategoryId();
if($_category == 5){
foreach ($products as $product) {
echo $product->getName() .'<br>';
echo "old: " . $product->getPrice() .'<br>';
$newPrice = ($product->getPrice() * 1.3);
$product->setPrice($newPrice);
$product->save();
echo "new: " . $product->getPrice();
echo '<br><br><br><br>';
}
} else { echo "No Results";
}
?>
最新修改。清除上面代码中的垃圾,它仍然缺少一些东西。我已经搜遍了所有人的答案。如果删除了if语句,则再次起作用。谢谢
<?php
require 'app/Mage.php'; Mage::app();
$products = Mage::getModel('catalog/category')
->load($category_id)
->getProductCollection()
->addAttributeToSelect('*');
if($category_id == 5){
foreach($products as $product) {
echo $product->getName() .'<br>';
echo "old: " . $product->getPrice() .'<br>';
$newPrice = ($product->getPrice() * 1.3);
$product->setPrice($newPrice);
$product->save();
echo "new: " . $product->getPrice();
echo '<br><br><br><br>';
}
} else { echo "No Results";
}
?>
答案 0 :(得分:1)
好吧,让我们看看我能从这里做些什么。
<?php
require 'app/Mage.php';
Mage::app();
//What Magento Version are you running? Try Mage::init(); for later versions
/**
* We're going to modify your second collection here
* Since the last bit didn't work, we're gonna load
* The category, then load the products for that category
**/
$categoryId = 5;
$products = Mage::getModel('catalog/category')
->load($categoryId)
->getProductCollection()
->addStoreFilter()
->addAttributeToSelect('name')
->addAttributeToSelect('value');
if( $products->count() != 0 ) { //We actually want type coercion here
foreach ($products as $product) {
echo $product->getName() .'<br>';
echo "old: " . $product->getPrice() .'<br>';
$newPrice = ($product->getPrice() * 1.3);
$product->setPrice($newPrice);
$product->save();
echo "new: " . $product->getPrice();
echo '<br><br><br><br>';
}
} else {
echo "No Results";
}
?>
这可能会有所帮助,虽然我不能说它是否会正确改变价格,因为我的工作资源无法测试。
修改强>