php - Magento - Update all prices in category

742

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

Answer

Solution:

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.

People are also looking for solutions to the problem: php - File path not being found

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.