Luis Masuelli
Luis Masuelli

Reputation: 12333

Magento - API not being updated

The previous provider killed several kittens by changing the core many times of a Magento 1.6.2.0 distribution.

So, in order to solve an issue in record time, we had to screw and hit the kittens' corspses: we still maintain the modified API: catalog_category.assignProduct.

So far, we have this API for the method:

public function assignProduct($categoryId, $productId, $position = null, $identifierType = null)
{
    $datos = explode('|', $categoryId);
    $productId = array_shift($datos);
    $categorias_actuales = Mage::getModel('catalog/category')
                           ->getCollection()
                           ->addIsActiveFilter()
                           ->addIdFilter($datos)
                           ->addLevelFilter(3)
                           ->getAllIds();
    $incat = function($cat) use ($categorias_actuales) {
        return in_array($cat, $categorias_actuales);
    };

    $result = array_combine($datos, array_map($incat, $datos));

    try {
        $product = Mage::helper('catalog/product')->getProduct($productId, null, $identifierType);
        $product->setCategoryIds($categorias_actuales);
        $product->save();
    } catch (Mage_Core_Exception $e) {
        $this->_fault('data_invalid', $e->getMessage());
    } catch (Exception $e) {
        $this->_fault('internal_error', $e->getMessage());
    }

    return $result;
}

The intention of the API was to assign many categories at once for a product. However this is a modified version because the previous was prohibitively slow. And the curious fact is that this worked until recently. It's intention was to:

  1. only the first parameter is used, and instead of an integer value, a kitten was killed here and the API now expects a string like "productId|catgId1|catgId2|cargId3|..." where the first value is a product id and the second to last values are categories ids; each of them being integer values. The parameter is broken by pipe character ("|") and the product id is popped. In this way, we have the product id in one var, and the categories array in another var.
  2. get the categories collection keeping only the active ones in level 3 (i.e. depth for the category in the tree) whose ids are among the specified ones. This means: if provided categories are array(3, 5, 7, 8) (e.g. from a "245|3|5|7|8" parameter, being "245" the product id) and one of them does not exist or is not active (e.g. 7 is not active and 8 does not exist), the returned value in $categorias_actuales is [3, 5].
  3. as for debugging porpuse, map each input category to existence and validity. this means: existent&&active&&level=3 categories will have "true" as value, while non-existent or non-active categories will have "false" as value. For the given example, the returned array would be: array(3 => true, 5 => true, 7 => false, 8 => false), since 7 is not active and 8 does not exist.
  4. in the try-catch block: a. retrieve the product; b. set the filtered ids (in the example: [3, 5]) as product categories in the just-fetched product; c. save the product.

However, I have the following issues:

  1. The function returns true, so returning $result would not give me the array $results as return value (my intention was to know in which categories was the product actually inserted).
  2. Changing to false instead of $result in the return statement had no effect at all: the obtained/displayed value from the API call was still true.
  3. Throwing a new Exception("something's going on here") had no effect at all. Still true at output.
  4. dying (die('something's going on here')) neither had effect. Still seeing (guess what?) true at the output.
  5. Edit: Also tried a syntax error!! (guess what? nothing happens).

It's not only that I tried these steps, but also refreshed the cache (System > Cache Management > select all and refresh, and even clicking the "Flush Magento Cache" button).

Questions: 1. given the issues before: how can I debug that API call? 2. what could be causing the product not being saved with their categories? By calling category.level I can see that the given ids (i.e. given in the first parameter to category.assignProduct) exist.

I'm a n00b @ magento API and perhaps I'm missing something usually obvious. Any help would be appreciated.

Upvotes: 0

Views: 148

Answers (1)

Seth Malaki
Seth Malaki

Reputation: 4486

Did you disable compilation? You can do it in one of these two ways.

  1. System -> Tools -> Compilation in admin

  2. Use SSH/shell and navigate to your magento root. Do the following commands:

    • cd shell
    • php -f compiler.php disable

Also, if your web server runs PHP APC with apc.stat set to 0 (you can check this by running php -i | grep apc.stat or looking for it at any phpinfo() output), please restart your apache2 web server or if you use php5-fpm -- restart that one, too.

Then, if all else fails, try looking for an equivalent file at app/code/local.

e.g. if your file is somewhere in app/code/core/Mage/Path/ToApiFile.php, look for the local version at app/code/local/Mage/Path/ToApiFile.php.

Finally, a very useful shell command for searching code instances:

$ cd app/code/ 
$ grep -rin "function assignProduct" *

Upvotes: 1

Related Questions