/** * Add product to Google Content. * * @param int[] $productIds * @param int $storeId * @return $this * @throws \Exception|\Zend_Gdata_App_CaptchaRequiredException */ public function addProducts($productIds, $storeId) { $totalAdded = 0; $errors = array(); if (is_array($productIds)) { foreach ($productIds as $productId) { if ($this->_flag && $this->_flag->isExpired()) { break; } try { $product = $this->_productFactory->create()->setStoreId($storeId)->load($productId); if ($product->getId()) { $item = $this->_itemFactory->create(); $item->insertItem($product)->save(); // The product was added successfully $totalAdded++; } } catch (\Zend_Gdata_App_CaptchaRequiredException $e) { throw $e; } catch (\Zend_Gdata_App_Exception $e) { $errors[] = $this->_gleShoppingData->parseGdataExceptionMessage($e->getMessage(), $product); } catch (\Zend_Db_Statement_Exception $e) { $message = $e->getMessage(); if ($e->getCode() == self::ERROR_CODE_SQL_UNIQUE_INDEX) { $message = __("The Google Content item for product '%1' (in '%2' store) already exists.", $product->getName(), $this->_storeManager->getStore($product->getStoreId())->getName()); } $errors[] = $message; } catch (CoreException $e) { $errors[] = __('The product "%1" cannot be added to Google Content. %2', $product->getName(), $e->getMessage()); } catch (\Exception $e) { $this->_logger->logException($e); $errors[] = __('The product "%1" hasn\'t been added to Google Content.', $product->getName()); } } if (empty($productIds)) { return $this; } } if ($totalAdded > 0) { $this->_getNotifier()->addNotice(__('Products were added to Google Shopping account.'), __('A total of %1 product(s) have been added to Google Content.', $totalAdded)); } if (count($errors)) { $this->_getNotifier()->addMajor(__('Errors happened while adding products to Google Shopping.'), $errors); } if ($this->_flag->isExpired()) { $this->_getNotifier()->addMajor(__('Operation of adding products to Google Shopping expired.'), __('Some products may have not been added to Google Shopping bacause of expiration')); } return $this; }
/** * Add product to Google Content. * * @param int[] $productIds * @param int $storeId * @return $this * @throws \Exception|\Zend_Gdata_App_CaptchaRequiredException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function addProducts($productIds, $storeId) { $totalAdded = 0; $errors = []; if (is_array($productIds)) { foreach ($productIds as $productId) { if ($this->_flag && $this->_flag->isExpired()) { break; } try { $product = $this->productRepository->getById($productId, false, $storeId); $item = $this->_itemFactory->create(); $item->insertItem($product)->save(); // The product was added successfully $totalAdded++; } catch (\Magento\Framework\Exception\NoSuchEntityException $noEntityException) { } catch (\Zend_Gdata_App_CaptchaRequiredException $e) { throw $e; } catch (\Zend_Gdata_App_Exception $e) { $errors[] = $this->_gleShoppingData->parseGdataExceptionMessage($e->getMessage(), $product); } catch (\Zend_Db_Statement_Exception $e) { $message = $e->getMessage(); if ($e->getCode() == self::ERROR_CODE_SQL_UNIQUE_INDEX) { $message = __("The Google Content item for product '%1' (in '%2' store) already exists.", $product->getName(), $this->_storeManager->getStore($product->getStoreId())->getName()); } $errors[] = $message; } catch (CoreException $e) { $errors[] = __('We can\'t add product "%1" to Google Content right now. %2', $product->getName(), $e->getMessage()); } catch (\Exception $e) { $this->_logger->critical($e); $errors[] = __('We can\'t add product "%1" to Google Content right now.', $product->getName()); } } if (empty($productIds)) { return $this; } } if ($totalAdded > 0) { $this->_notifier->addNotice(__('You added products to the Google Shopping account.'), __('You added a total of %1 product(s) to the Google Content.', $totalAdded)); } if (count($errors)) { $this->_notifier->addMajor(__('Something went wrong while adding products to Google Shopping.'), $errors); } if ($this->_flag->isExpired()) { $this->_notifier->addMajor(__('Operation of adding products to Google Shopping expired.'), __('Some products may have not been added to Google Shopping because of expiration.')); } return $this; }