Php – Magento – Update Prices Script – Multiple Stores

magentoPHP

I am trying to update some (but not all ) prices from one store to another. For example one line of shirts should be 1.2 times the price in the second store

I would like to update some groups of items only, based on entity label but am struggling to pull all the data from magento (asside what i can get out in the code below). The bits I am missing are price and entity label. I know what tables they live in but not sure the correct magento syntax to access them eg Mage::getModel('catalog/product') and I am trying to achieve this using Magento friendly code and not queries

Using enterprise 1.11, not looking at buying a plugin at this stage, any advice greatly appreciated

$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); #important
$product_id = '8782';
$collection = Mage::getModel('catalog/product')
         ->load($product_id);
        echo "Collection: " . $collection->getName();
        echo "<br>";
        echo "Attribute Set: " . $collection->getAttributeSetId() . " SKU: " . $collection->getSku();
        echo "<br>"; 
        echo "Entity ID: " . $collection->getEntity_id() . " Product Type (Attribute Label): " . $collection->getProducttype();

Best Answer

Just clarifying:

In the sample you've shown, the $collection object isn't really a "collection". It's an instance of one 'catalog/product' object.

To modify the price of one catalog/product object, you'd be looking at doing something like this:

$product = Mage::getModel('catalog/product')->load($product_id);
$product->setPrice($product->getPrice() * 1.2)
        ->save();

If you want to do this over a bunch of products, you'll probably want to use a collection of 'catalog/product' objects, and apply some attribute filters (which boils down to adding WHERE clauses to the eventually generated SQL). (Here's one summary of the magento collection query syntax.)

I'm not sure what you mean by "entity label". Is that a custom attribute you've attached to your products?

A general example, applying this price change to all products with a certain SKU:

$product_collection = Mage::getModel('catalog/product')->getCollection()
                                                       ->addAttributeToFilter('sku', array('like' => 'FOO01-%'));

foreach($product_collection as $product) {

    $new_price = calcNewPrice($product->getPrice());
    $product->setPrice($new_price)
            ->save(); 

}     

Where, if you're going across stores for price calcs, "calcNewPrice" might look something like this:

function calcNewPrice($product) {
    $other_store_product = Mage::getModel('catalog/product')->setStoreId($other_store_id)
                                                            ->load($product->getId());
    if ($other_store_product) {
        return $other_store_product->getPrice() * 1.2;
    }
    // else ???
}

Hope this helps.

Related Topic