Пример #1
0
 /**
  * @inheritdoc
  */
 public function initContent()
 {
     $collection = new NostoExportOrderCollection();
     foreach ($this->getOrderIds() as $id_order) {
         $order = new Order($id_order);
         if (!Validate::isLoadedObject($order)) {
             continue;
         }
         $nosto_order = new NostoTaggingOrder();
         $nosto_order->include_special_items = false;
         $nosto_order->loadData($this->module->getContext(), $order);
         $validator = new NostoValidator($nosto_order);
         if ($validator->validate()) {
             $collection[] = $nosto_order;
         }
         $order = null;
     }
     $this->encryptOutput($collection);
 }
Пример #2
0
 /**
  * @inheritdoc
  */
 public function initContent()
 {
     $context = $this->module->getContext();
     $collection = new NostoExportProductCollection();
     foreach ($this->getProductIds() as $id_product) {
         $product = new Product($id_product, true, $context->language->id, $context->shop->id);
         if (!Validate::isLoadedObject($product)) {
             continue;
         }
         $nosto_product = new NostoTaggingProduct();
         $nosto_product->loadData($context, $product);
         $validator = new NostoValidator($nosto_product);
         if ($validator->validate()) {
             $collection[] = $nosto_product;
         }
         $product = null;
     }
     $this->encryptOutput($collection);
 }
Пример #3
0
 /**
  * Validates the account attributes.
  *
  * @throws NostoException if any attribute is invalid.
  */
 protected function validate()
 {
     $validator = new NostoValidator($this);
     if (!$validator->validate()) {
         foreach ($validator->getErrors() as $errors) {
             throw new NostoException(sprintf('Invalid Nosto account. %s', $errors[0]));
         }
     }
 }
Пример #4
0
 /**
  * Exports visible products from the current store.
  * Result can be limited by the `limit` and `offset` GET parameters.
  */
 public function productAction()
 {
     if (Mage::helper('nosto_tagging')->isModuleEnabled()) {
         $pageSize = (int) $this->getRequest()->getParam('limit', 100);
         $currentOffset = (int) $this->getRequest()->getParam('offset', 0);
         $currentPage = $currentOffset / $pageSize + 1;
         // We use our own collection object to avoid issues with the product
         // flat collection. It's missing required data by default.
         /** @var Nosto_Tagging_Model_Resource_Product_Collection $products */
         $products = Mage::getModel('nosto_tagging/product')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId())->addAttributeToSelect('*')->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)->setPageSize($pageSize)->setCurPage($currentPage);
         if ($currentPage > $products->getLastPageNumber()) {
             $products = array();
         }
         $collection = new NostoExportProductCollection();
         foreach ($products as $product) {
             /** @var Mage_Catalog_Model_Product $product */
             /** @var Nosto_Tagging_Model_Meta_Product $meta */
             $meta = Mage::getModel('nosto_tagging/meta_product');
             $meta->loadData($product);
             $validator = new NostoValidator($meta);
             if ($validator->validate()) {
                 $collection[] = $meta;
             }
         }
         $this->export($collection);
     }
 }
Пример #5
0
 /**
  * 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;
 }
Пример #6
0
 /**
  * Returns the whole collection in JSON format.
  *
  * @return string the json.
  * @throws NostoException if the collection is empty.
  */
 protected function getCollectionAsJson()
 {
     $data = array();
     foreach ($this->collection->getArrayCopy() as $item) {
         /** @var NostoProductInterface|NostoValidatableInterface $item */
         $validator = new NostoValidator($item);
         if ($validator->validate()) {
             $data[] = $this->getProductAsArray($item);
         }
     }
     if (empty($data)) {
         throw new NostoException('No products found in collection.');
     }
     return json_encode($data);
 }
 /**
  * Loads a Nosto product model for given PS product ID, language ID and shop ID.
  *
  * @param int $id_product the PS product ID.
  * @param int $id_lang the language ID.
  * @param int $id_shop the shop ID.
  * @return NostoTaggingProduct|null the product or null if could not be loaded.
  */
 protected function loadNostoProduct($id_product, $id_lang, $id_shop)
 {
     $product = new Product($id_product, false, $id_lang, $id_shop);
     if (!Validate::isLoadedObject($product)) {
         return null;
     }
     if (isset($product->visibility) && $product->visibility === 'none') {
         return null;
     }
     $this->makeContextSnapshot();
     $nosto_product = new NostoTaggingProduct();
     $nosto_product->loadData($this->makeContext($id_lang, $id_shop), $product);
     $this->restoreContextSnapshot();
     $validator = new NostoValidator($nosto_product);
     if (!$validator->validate()) {
         return null;
     }
     return $nosto_product;
 }
 /**
  * Exports visible products from the current store.
  * Result can be limited by the `limit` and `offset` GET parameters.
  */
 public function productAction()
 {
     if (Mage::helper('nosto_tagging')->isModuleEnabled()) {
         $pageSize = (int) $this->getRequest()->getParam('limit', 100);
         $currentOffset = (int) $this->getRequest()->getParam('offset', 0);
         $currentPage = $currentOffset / $pageSize + 1;
         $products = Mage::getModel('catalog/product')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId())->addAttributeToSelect('*')->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)->setPageSize($pageSize)->setCurPage($currentPage);
         if ($currentPage > $products->getLastPageNumber()) {
             $products = array();
         }
         $collection = new NostoExportProductCollection();
         foreach ($products as $product) {
             if ($product->getTypeId() === Mage_Catalog_Model_Product_Type::TYPE_BUNDLE && (int) $product->getPriceType() === Mage_Bundle_Model_Product_Price::PRICE_TYPE_FIXED) {
                 continue;
             }
             $meta = new Nosto_Tagging_Model_Meta_Product();
             $meta->loadData($product);
             $validator = new NostoValidator($meta);
             if ($validator->validate()) {
                 $collection[] = $meta;
             }
         }
         $this->export($collection);
     }
 }