trick prestashop into thinking that we're calling getProducts from back office

This commit is contained in:
Pavel 2020-04-23 15:38:48 +03:00 committed by GitHub
parent bfa41b7d14
commit 36a9c12bf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}
}