2

I got following error when tried to save product from collection:

Invalid method Varien_Object::save(Array())

And here is full code (I know, that it is ugly, but now I try refactor it):

public function postAction()
{
    $sku = Mage::app()->getRequest()->getParam('sku');
    $file = Mage::app()->getRequest()->getParam('file');

    if (empty($sku) or empty($file)) {
        echo "FALSE";
        return;
    }

    $path = Mage::getBaseDir() . "/media/photosimport/";
    $filepath = $path . $file;
    $img_save_mode = array('image', 'small_image', 'thumbnail');

    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    if (!$product->getId()) {
        echo "FALSE";
        return;
    }

    Mage::log($sku . ' =>' . $product->getName(), null, 'photosimport.log');

    $filename = pathinfo($file, PATHINFO_FILENAME);
    $filenameArray = explode("_",$filename); //0 - SKU, 1 - image name
    $imagename = (isset($filenameArray[1]) && !empty($filenameArray[1])) ? $filenameArray[1] : null;
    $prod = Mage::getModel('catalog/product')->load($product->getId());
    $galleryData = $prod->getData('media_gallery');

    foreach ($galleryData['images'] as $galleryImage)
    {
        $gf = explode("/", $galleryImage['file']);
        if ($gf[count($gf) - 1] == $file) {
            echo "UPDATE";
            unlink(Mage::getBaseDir() . '/media/catalog/product' . $galleryImage['file']);
            rename($filepath, Mage::getBaseDir() . '/media/catalog/product' . $galleryImage['file']);
            @unlink($filepath);
        }
    }
    if (file_exists($filepath)) {
        try {
            if (empty($imagename)) {
                $product->addImageToMediaGallery($filepath, $img_save_mode, true, false);
            } else {
                $product->addImageToMediaGallery($filepath, false, true, false);
            }
            Mage::log($sku . ' ok', null, 'photosimport.log');
        } catch (Exception $e) {
            Mage::log('error: ' . $e->getMessage(), null, 'photosimport.log');
            echo "FALSE";
            echo $e->getMessage();
            return;
        }
    } else {
        Mage::log(
            'Product does not have an image or the path is incorrect. Path was: '.$filepath,
            null,
            'photosimport.log'
        );
        echo "FALSE";
        return;
    }

    try {
        $product->save();
    } catch (Exception $e) {
       echo $e->getMessage();
    }

    @unlink($filepath);
    echo "OK";
}

Please, help me to fix that! I tried to use saveAttribute via resource model, but it didn't work too.

2

Main issue at product exits or not.When you have use Mage::getModel('catalog/product')->loadByAttribute('sku', $sku) then as per asOOP concept , $product was gave an object that means' !$product goes to false ` .

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
    if (!$product) {
        echo "FALSE";
        return;
    }

In this case,you need to use getId().when a product is not exits then it getId() getter function give the value blank

 if (!$product->getId()) {
                echo "FALSE";
                return;
            }
// load the product object properly

    $id=$product->getId();
    $product=Mage::getModel('catalog/product')->load($id)
  • thank you! Fixed and updated code, but this doesn't fix main problem, still get exception on save() – Rakshazi May 8 '15 at 9:44
  • update.....please check.ed.. – Amit Bera May 8 '15 at 9:51
  • still doesn't work, I updated code, please, check it. – Rakshazi May 8 '15 at 10:20
  • please check compiler and it may compiler issue.Admin>Tool>Compiler – Amit Bera May 8 '15 at 10:23
1

Short answer

You can't save a product that has been loaded via loadByAttribute. You have to execute load($id) on that product as suggested in first answer. So use this code to load and save the product:

$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if ($product === false) {
    return;
}
$product->load($product->getId());
//...
$product->save();

Problem

Mage_Catalog_Model_Abstract->loadByAttribute uses collection-loading on collection catalog/product_collection with a limit of 1.

Stock items of products in that collection are loaded via the event observer Mage_CatalogInventory_Model_Observer->addStockStatusToCollection which listens to event catalog_product_collection_load_after. addStockStatusToCollection loads stock items of products only if flag require_stock_items is set true.

In case of Mage_Catalog_Model_Abstract->loadByAttribute this flag isn't set so we end up with $product->_data['stock_item'] being of type Varien_Object (see Mage_CatalogInventory_Model_Stock_Status lines 488-491).

This causes error

Invalid method Varien_Object::save...

on $product->save.

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.