/** * Tests that product upsert API requests can be made. */ public function testSendingProductUpsert() { $account = new NostoAccount('platform-00000000'); $product = new NostoProduct(); $token = new NostoApiToken('products', '01098d0fc84ded7c4226820d5d1207c69243cbb3637dc4bc2a216dafcf09d783'); $account->addApiToken($token); $op = new NostoOperationProduct($account); $op->addProduct($product); $result = $op->upsert(); $this->specify('successful product upsert', function () use($result) { $this->assertTrue($result); }); }
/** * Event handler for the "catalog_product_save_after" event. * Sends a product update API call to Nosto. * * @param Varien_Event_Observer $observer the event observer. * * @return Nosto_Tagging_Model_Observer */ public function sendProductUpdate(Varien_Event_Observer $observer) { if (Mage::helper('nosto_tagging')->isModuleEnabled()) { /** @var Mage_Catalog_Model_Product $product */ $product = $observer->getEvent()->getProduct(); // Always "upsert" the product for all stores it is available in. // This is done to avoid data inconsistencies as even if a product // is edited for only one store, the updated data can reflect in // other stores as well. foreach ($product->getStoreIds() as $storeId) { $store = Mage::app()->getStore($storeId); /** @var NostoAccount $account */ $account = Mage::helper('nosto_tagging/account')->find($store); if ($account === null || !$account->isConnectedToNosto()) { continue; } // Load the product model for this particular store view. $product = Mage::getModel('catalog/product')->setStoreId($store->getId())->load($product->getId()); if (is_null($product)) { continue; } if (!$product->isVisibleInSiteVisibility()) { continue; } /** @var Nosto_Tagging_Model_Meta_Product $model */ $model = Mage::getModel('nosto_tagging/meta_product'); $model->loadData($product, $store); // Only send product update if we have all required // data for the product model. $validator = new NostoValidator($model); if ($validator->validate()) { try { $op = new NostoOperationProduct($account); $op->addProduct($model); $op->upsert(); } catch (NostoException $e) { Mage::log("\n" . $e, Zend_Log::ERR, 'nostotagging.log'); } } } } return $this; }