Jordan
Jordan

Reputation: 399

Magento - Update all prices in category

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

Upvotes: 0

Views: 1427

Answers (1)

Jason
Jason

Reputation: 1962

Well, let's see what I can do from here.

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

That might help a little, although I can not say whether or not it will change the price correctly, since I have my resources at work I couldn't test it.

Edit

  • Changed the collection method, I get the category and load the products directly from it.
  • Updated answer to reflect OP's results.

Upvotes: 1

Related Questions