Question

I have the following csv from which I get the info to update products programmatically:

name1;sku1;description1;shortdescription1
name2;sku2;description2;shortdescription2
name3;sku3;description3;shortdescription3
name4;sku4;description4;shortdescription4
name5;sku5;description5;shortdescription5
name6;sku6;description6;shortdescription6

How this works?

Magento will look first in the catalog collection (in the store view with ID 1) for the products matching the name name1, name2, ...., name6.

The product with name name1, will be assigned the sku sku1, the description description1 and the short description shortdescription1; the same will happen for the products name2, name3, ...., name6. These changes will take effect in the store view with ID 2.

For some reason the script is still not working as intended. The code is as follows:

<?php

    require_once ('abstract.php');
    require_once 'Mage.php';
    umask(0) ;

    function importData($file, $store_id) {
        Mage::app();
        $handle = fopen($file, "r");
        while ($row = fgetcsv($handle, 0, ";")) {
        Mage::getModel('core/store')->load($store_id);
        $storeId='1';
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $productIds[] = $row[0];

        $products = Mage::getModel('catalog/product')->getCollection();
        $products->addStoreFilter();
        $products->addAttributeToSelect('*');
        $products->addAttributeToFilter('entity_id', array('in' => $productIds));
        foreach($products as $product)
            {
                $price = $product->setSku($row[1])->setDescription($row[2])->setShortDescription($row[3]);
                Mage::app()->setCurrentStore(2);
                $product->save();
            }

        }
        fclose($handle);
    }

    importData("test.csv", 3);

?>

I think the issue has to do with the definition of $productIds but I cannot manage to solve it.

Do you have any ideas or recommendations? Thanks

Was it helpful?

Solution

You're trying to add an element to $productIds which is not yet initialized at this point, so PHP will initialize it to an empty array, and then append $row[0] to that array, which gives you your desired result the first loop. The second iteration though, now $productIds becomes ["A", "B"] which is not what you want.

Just set it like below:

$productIds = [$row[0]];

It will initialize it as an array, with the first element being $row[0]. The next time this iterates, $productIds will be a new array with the first element being $row[0].

<?php

    require_once ('abstract.php');
    require_once 'Mage.php';
    umask(0) ;

    function importData($file, $store_id) {
        Mage::app();
        $handle = fopen($file, "r");
        while ($row = fgetcsv($handle, 0, ";")) {
            Mage::getModel('core/store')->load($store_id);
            $storeId='1';
            Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
            $productIds[] = $row[0];

            $products = Mage::getModel('catalog/product')->getCollection();
            $products->addStoreFilter();
            $products->addAttributeToSelect('*');
            $products->addAttributeToFilter('entity_id', array('in' => $productIds));
            foreach($products as $product)
                {
                    $price = $product->setSku($row[1])->setDescription($row[2])->setShortDescription($row[3]);
                    Mage::app()->setCurrentStore(2);
                    $product->save();
                }

            }
            fclose($handle);
        }
    }

    importData("test.csv", 3);

?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top