0

I feel close with my solution (but I may be way off knowing my luck) I am attempting to write a simple php script to update all prices in a specific category within Magento. Below is my code please help me out. The problem is it won't return any results. If I take out the sections that refer specifically to category it runs fine against every product.

    <?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";
}
?>

Latest modifications. Cleaned out the garbage from the above code and it is still missing something. I have searched all over for an answer to this. Again works if the if statement is removed. Thank you

    <?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";
}
?>
4

1 回答 1

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";
    }
?>

这可能会有所帮助,虽然我不能说它是否会正确改变价格,因为我有我的资源在工作,我无法测试它。

编辑

  • 改变了收集方法,我得到了类别并直接从中加载产品。
  • 更新答案以反映 OP 的结果。
于 2013-08-31T03:32:32.297 回答