From 36a9c12bf85777b70a6bef7e522ff27f5aee0771 Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Thu, 23 Apr 2020 15:38:48 +0300 Subject: [PATCH] trick prestashop into thinking that we're calling getProducts from back office --- retailcrm/lib/RetailcrmCartUploader.php | 87 ++++++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/retailcrm/lib/RetailcrmCartUploader.php b/retailcrm/lib/RetailcrmCartUploader.php index edd12cd..d87cf3b 100644 --- a/retailcrm/lib/RetailcrmCartUploader.php +++ b/retailcrm/lib/RetailcrmCartUploader.php @@ -42,6 +42,15 @@ class RetailcrmCartUploader */ static $api; + /** @var \Context|\ContextCore */ + static $context; + + /** @var Cart|\CartCore */ + static $origCart; + + /** @var \Employee|\EmployeeCore */ + static $origEmployee; + /** * @var array */ @@ -92,6 +101,7 @@ class RetailcrmCartUploader static::$syncDelay = 0; static::$syncStatus = ''; static::$now = new \DateTime(); + static::$context = Context::getContext(); } /** @@ -103,6 +113,9 @@ class RetailcrmCartUploader return; } + static::backupCurrentCart(); + static::backupCurrentEmployee(); + static::setAnyEmployeeToContext(); static::loadAbandonedCartsIds(); foreach (static::$cartsIds as $cartId) { @@ -125,6 +138,8 @@ class RetailcrmCartUploader continue; } + static::populateContextWithCart($cart); + $response = static::$api->ordersGet($cartExternalId); if (!($response instanceof RetailcrmApiResponse)) { @@ -159,6 +174,9 @@ class RetailcrmCartUploader } } } + + static::restoreCurrentCart(); + static::restoreCurrentEmployee(); } /** @@ -247,13 +265,13 @@ class RetailcrmCartUploader try { // Don't upload empty cartsIds. - if (count($cart->getProducts()) == 0 || !$shouldBeUploaded) { + if (count($cart->getProducts(true)) == 0 || !$shouldBeUploaded) { return true; } } catch (\Exception $exception) { RetailcrmLogger::writeCaller( __METHOD__, - sprintf("Failure while trying to get cart total (cart id=%d)", $cart->id) + sprintf("Failure while trying to get cart products (cart id=%d)", $cart->id) ); RetailcrmLogger::writeCaller(__METHOD__, "Error message and stacktrace will be printed below"); RetailcrmLogger::writeCaller(__METHOD__, $exception->getMessage()); @@ -387,4 +405,69 @@ class RetailcrmCartUploader return true; } + + /** + * Backups current cart in context + */ + private static function backupCurrentCart() + { + self::$origCart = self::$context->cart; + } + + /** + * Restores current cart in context + */ + private static function restoreCurrentCart() + { + self::populateContextWithCart(self::$origCart); + } + + /** + * Populates current context with provided cart. + * + * @param Cart|\CartCore $cart + */ + private static function populateContextWithCart($cart) + { + self::$context->cart = $cart; + } + + /** + * Backups current employee in context + */ + private static function backupCurrentEmployee() + { + self::$origEmployee = self::$context->employee; + } + + /** + * Restores current employee in context + */ + private static function restoreCurrentEmployee() + { + self::populateContextWithEmployee(self::$origEmployee); + } + + /** + * Sets any employee to context + */ + private static function setAnyEmployeeToContext() + { + $employees = EmployeeCore::getEmployees(); + $employee = reset($employees); + + if (isset($employee['id_employee'])) { + self::$context->employee = new Employee($employee['id_employee']); + } + } + + /** + * Populates current context with provided cart. + * + * @param \Employee|\EmployeeCore $employee + */ + private static function populateContextWithEmployee($employee) + { + self::$context->employee = $employee; + } }