1

I've some code to import some products: if the prodocut is new, I've insert it in the catalog, otherwise, I've to update the product if there are some changes. Code is the following:

<?php

    include_once('C:/xampp/htdocs/magento/app/Mage.php');

    Mage::app();

    $rootMagento = mage::getBaseDir();
    $files = scandir($rootMagento);
    $count = sizeof($files);
    for ($i =0; $i< $count; $i++) {
        if ($files[$i]=='productData.txt'){   
            //$fileAperto = fopen($files[$i], "r");
            $righe = file('productData.txt');
            $countRighe = sizeof($righe);
            for ($ii = 0; $ii < $countRighe; $ii++) {
                list($sku, $name, $description, $shortDescription) = split("[';']", $righe[$ii]);
                if ($sku == '' || $name == '' || $description == '' 
                    || $shortDescription == '' ) {
                    mage::log('esportazione non corretta: manca uno dei dati');
                } 
                $product_model = mage::getModel('catalog/product');
                $productId = $product_model->getIdBySku($sku);
                if (!$productId){
                    //inserimento
                    $productData = array ('sku' => $sku,
                                          'name' => $name,
                                          'description' => $description,
                                          'short_description' => $shortDescription,
                                          'status' => 1,
                                          'visibility' => 4,
                                          'attribute_set_id' => 10
                                          );

                    $product_model->setData($productData);
                    $qty = 1;
                    try{
                        $productId = $product_model->save()->getId();
                        $product = $product_model->load($productId);
                        $product->setQty($qty);
                        $product->setIsInStock(1); //->isInStock(1)
                        $product->save();

                    }catch(Exception $e){
                        mage::log($e->getMessage());
                    }

                }else{
                    //aggiornamento
                    $product = $product_model->load($productId);
                    $productData = array ('sku' => $sku,
                                          'name' => $name,
                                          'description' => $description,
                                          'short_description' => $shortDescription,
                                          'status' => 1,
                                          'visibility' => 4,
                                          'attribute_set_id' => 10
                                          );
                    $product->setData($productData); // + codice x stock
                    $qty = 1;
                    try{
                        $productId = $product_model->save()->getId();
                        $product = $product_model->load($productId);
                        $product->setQty($qty);
                        $product->setIsInStock(1); //->isInStock(1)
                        $product->save();

                    }catch(Exception $e){
                        mage::log($e->getMessage());
                    }
                }
            }
        }
    }

For the insert it's ok, but for update it doesn't work. Isn't this code correct?

0

There's indeed a small issue in your else.

Your code looks like this:

$product = $product_model->load($productId);
...
$productId = $product_model->save()->getId();

But the second line is wrong. If I get rid of the $product_model variable it looks like this:

$productId = mage::getModel('catalog/product')->save()->getId();

So it does not work obviously as you need to save the product you loaded.

So you need to replace this line with:

$productId = $product->save()->getId();
  • 1
    @Federica feel free to mark your question as answered so it will benefit for others ;) – Raphael at Digital Pianism Aug 31 '16 at 9:01
  • I replaced the row and I used variable $product instead $product_model but the product didn't update. In my example I changed name of an existing product.. – Federica Aug 31 '16 at 9:39
  • @Federica well I don't see any other errors in your code sorry I'm out of ideas here – Raphael at Digital Pianism Aug 31 '16 at 9:41
0

channge below line in your code

$product_model = mage::getModel('catalog/product');

to

$product_model = Mage::getModel('catalog/product') ->loadByAttribute('sku', trim($sku) );

and then you can get product id by $product_model->getId(); and name $product_model->getName();

and so on please let me know if you still find issue

Your product model is not loaded at update time that is why its not being save and replace above code it loads your model via sku and then you can update your product.

  • thank you for the answer, I tryied in this way but I've this error – Federica Aug 31 '16 at 9:50
  • change this line in update . from $productId = $product_model->save()->getId(); to $productId = $product->save()->getId(); – Murtuza Zabuawala Aug 31 '16 at 9:54
  • Please check and let me know – Murtuza Zabuawala Aug 31 '16 at 9:54
  • I had already modified this row.. – Federica Aug 31 '16 at 9:58
  • change this line from $product = $product_model->load($productId); to $product = Mage::getModel('catalog/product')->load($productId); – Murtuza Zabuawala Aug 31 '16 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.