From 65fb25703bf5acf45b2acb0a0f4d1fdf402243f4 Mon Sep 17 00:00:00 2001 From: max-baranikov Date: Sun, 27 Dec 2020 14:48:36 +0300 Subject: [PATCH] Added MultiStore support --- retailcrm/lib/RetailcrmCartUploader.php | 1 + retailcrm/lib/RetailcrmCli.php | 33 ++++++- retailcrm/lib/RetailcrmHistory.php | 5 +- retailcrm/lib/RetailcrmJobManager.php | 23 ++++- retailcrm/lib/RetailcrmTools.php | 11 +++ .../events/RetailcrmAbandonedCartsEvent.php | 40 +++++--- .../lib/events/RetailcrmAbstractEvent.php | 56 ++++++++++- .../lib/events/RetailcrmEventInterface.php | 7 ++ retailcrm/lib/events/RetailcrmExportEvent.php | 98 ++++++++++--------- retailcrm/lib/events/RetailcrmIcmlEvent.php | 21 +++- .../lib/events/RetailcrmInventoriesEvent.php | 44 +++++---- .../lib/events/RetailcrmMissingEvent.php | 7 +- retailcrm/lib/events/RetailcrmSyncEvent.php | 48 +++++---- retailcrm/retailcrm.php | 4 + 14 files changed, 277 insertions(+), 121 deletions(-) diff --git a/retailcrm/lib/RetailcrmCartUploader.php b/retailcrm/lib/RetailcrmCartUploader.php index d87cf3b..86b8773 100644 --- a/retailcrm/lib/RetailcrmCartUploader.php +++ b/retailcrm/lib/RetailcrmCartUploader.php @@ -358,6 +358,7 @@ class RetailcrmCartUploader $sql = 'SELECT c.id_cart, c.date_upd FROM ' . _DB_PREFIX_ . 'cart AS c WHERE id_customer != 0 + ' . Shop::addSqlRestriction(false, 'c') . ' AND TIME_TO_SEC(TIMEDIFF(\'' . pSQL(static::$now->format('Y-m-d H:i:s')) . '\', date_upd)) >= ' . pSQL(static::$syncDelay) . ' AND c.id_cart NOT IN(SELECT id_cart from ' . _DB_PREFIX_ . 'orders);'; diff --git a/retailcrm/lib/RetailcrmCli.php b/retailcrm/lib/RetailcrmCli.php index de17b8e..897e89f 100644 --- a/retailcrm/lib/RetailcrmCli.php +++ b/retailcrm/lib/RetailcrmCli.php @@ -98,9 +98,10 @@ class RetailcrmCli RetailcrmLogger::output('WARNING: cannot handle signals properly, force stop can cause problems!'); } - $shortopts = "j:"; - $longopts = array( + $shortopts = "j:s:"; + $longopts = array( "job:", + "shop:", "set-web-jobs:", "query-web-jobs", "run-jobs", @@ -110,6 +111,11 @@ class RetailcrmCli $options = getopt($shortopts, $longopts); $jobName = isset($options['j']) ? $options['j'] : (isset($options['job']) ? $options['job'] : null); + $shopId = isset($options['s']) ? $options['s'] : (isset($options['shop']) ? $options['shop'] : null); + + if (Shop::isFeatureActive()) { + Shop::setContext(Shop::CONTEXT_ALL); + } if (isset($options['reset-job-manager'])) { $this->resetJobManager(); @@ -125,7 +131,7 @@ class RetailcrmCli $this->help(); } else { $this->setCleanupOnShutdown(); - $this->runJob($jobName); + $this->runJob($jobName, $shopId); } } @@ -154,16 +160,20 @@ class RetailcrmCli * * @param string $jobName */ - private function runJob($jobName) + private function runJob($jobName, $shopId) { try { - $result = RetailcrmJobManager::runJob($jobName, true, true); + $result = RetailcrmJobManager::runJob($jobName, true, true, $shopId); RetailcrmLogger::output(sprintf( 'Job %s was executed, result: %s', $jobName, $result ? 'true' : 'false' )); } catch (\Exception $exception) { + if (Shop::isFeatureActive()) { + Shop::setContext(Shop::CONTEXT_ALL); + } + if ($exception instanceof RetailcrmJobManagerException && $exception->getPrevious() instanceof \Exception) { $this->printStack($exception->getPrevious()); } else { @@ -173,6 +183,10 @@ class RetailcrmCli self::clearCurrentJob($jobName); } + if (Shop::isFeatureActive()) { + Shop::setContext(Shop::CONTEXT_ALL); + } + if (isset($result) && $result) { self::clearCurrentJob($jobName); } @@ -213,6 +227,15 @@ class RetailcrmCli RetailcrmLogger::output(sprintf('> php %s --set-web-jobs true / false - Enable or disable web jobs', $this->cliPath)); RetailcrmLogger::output(sprintf('> php %s --query-web-jobs - Check web jobs status', $this->cliPath)); RetailcrmLogger::output(); + RetailcrmLogger::output( + "NOTICE: If you have MultiShop feature enabled, you can additionally " . + "specify shop id when manually running job: " + ); + RetailcrmLogger::output("At default jobs are running for all active shops alternately."); + RetailcrmLogger::output(); + RetailcrmLogger::output(sprintf('> php %s -j -s - Runs provided job for specified shop', $this->cliPath)); + RetailcrmLogger::output(sprintf('> php %s --job --shop - Runs provided job for specified shop', $this->cliPath)); + RetailcrmLogger::output(); RetailcrmLogger::output( "WARNING: Commands below are dangerous and should be used only when " . "job manager or cli doesn't work properly." diff --git a/retailcrm/lib/RetailcrmHistory.php b/retailcrm/lib/RetailcrmHistory.php index 879629b..36eee89 100644 --- a/retailcrm/lib/RetailcrmHistory.php +++ b/retailcrm/lib/RetailcrmHistory.php @@ -418,6 +418,8 @@ class RetailcrmHistory $cart = new Cart(); $cart->id_currency = $default_currency; $cart->id_lang = self::$default_lang; + $cart->id_shop = Context::getContext()->shop->id; + $cart->id_shop_group = intval(Context::getContext()->shop->id_shop_group); $cart->id_customer = $customer->id; $cart->id_address_delivery = (int) $address->id; $cart->id_address_invoice = (int) $address->id; @@ -471,9 +473,8 @@ class RetailcrmHistory * Create order */ $newOrder = new Order(); - $shops = Shop::getShops(); $newOrder->id_shop = Context::getContext()->shop->id; - $newOrder->id_shop_group = (int)$shops[Context::getContext()->shop->id]['id_shop_group']; + $newOrder->id_shop_group = intval(Context::getContext()->shop->id_shop_group); $newOrder->reference = $newOrder->generateReference(); $newOrder->id_address_delivery = (int) $address->id; $newOrder->id_address_invoice = (int) $address->id; diff --git a/retailcrm/lib/RetailcrmJobManager.php b/retailcrm/lib/RetailcrmJobManager.php index 88c0c60..c3f5c97 100644 --- a/retailcrm/lib/RetailcrmJobManager.php +++ b/retailcrm/lib/RetailcrmJobManager.php @@ -108,6 +108,10 @@ class RetailcrmJobManager $current = date_create('now'); $lastRuns = array(); + if (Shop::isFeatureActive()) { + Shop::setContext(Shop::CONTEXT_ALL); + } + try { $lastRuns = static::getLastRuns(); } catch (Exception $exception) { @@ -172,6 +176,10 @@ class RetailcrmJobManager $lastRuns[$job] = new \DateTime('now'); } } catch (\Exception $exception) { + if (Shop::isFeatureActive()) { + Shop::setContext(Shop::CONTEXT_ALL); + } + if ($exception instanceof RetailcrmJobManagerException && $exception->getPrevious() instanceof \Exception ) { @@ -193,6 +201,10 @@ class RetailcrmJobManager self::clearCurrentJob($job); } + if (Shop::isFeatureActive()) { + Shop::setContext(Shop::CONTEXT_ALL); + } + if (isset($result) && $result) { self::clearCurrentJob($job); } @@ -281,7 +293,7 @@ class RetailcrmJobManager * @return bool * @throws \RetailcrmJobManagerException */ - public static function runJob($job, $once = false, $cliMode = false) + public static function runJob($job, $once = false, $cliMode = false, $shopId = null) { $jobName = self::escapeJobName($job); $jobFile = implode( @@ -294,7 +306,7 @@ class RetailcrmJobManager } try { - return static::execHere($jobName, $jobFile, $once, $cliMode); + return static::execHere($jobName, $jobFile, $once, $cliMode, $shopId); } catch (\RetailcrmJobManagerException $exception) { throw $exception; } catch (\Exception $exception) { @@ -329,7 +341,7 @@ class RetailcrmJobManager */ public static function setCurrentJob($job) { - return (bool) Configuration::updateValue(self::CURRENT_TASK, $job); + return (bool)Configuration::updateValue(self::CURRENT_TASK, $job); } /** @@ -339,7 +351,7 @@ class RetailcrmJobManager */ public static function getCurrentJob() { - return (string) Configuration::get(self::CURRENT_TASK); + return (string)Configuration::get(self::CURRENT_TASK); } /** @@ -470,7 +482,7 @@ class RetailcrmJobManager * @return bool * @throws \RetailcrmJobManagerException */ - private static function execHere($jobName, $phpScript, $once = false, $cliMode = false) + private static function execHere($jobName, $phpScript, $once = false, $cliMode = false, $shopId = null) { set_time_limit(static::getTimeLimit()); @@ -513,6 +525,7 @@ class RetailcrmJobManager } $job->setCliMode($cliMode); + $job->setShopId($shopId); self::registerShutdownHandler(); diff --git a/retailcrm/lib/RetailcrmTools.php b/retailcrm/lib/RetailcrmTools.php index d7f7d56..3d3b4be 100644 --- a/retailcrm/lib/RetailcrmTools.php +++ b/retailcrm/lib/RetailcrmTools.php @@ -682,4 +682,15 @@ class RetailcrmTools return $equal; } + + /** + * Change shop in the context + * + * @param $id_shop + */ + public static function setShopContext($id_shop) + { + Shop::setContext(Shop::CONTEXT_SHOP, $id_shop); + Context::getContext()->shop = new Shop($id_shop); + } } diff --git a/retailcrm/lib/events/RetailcrmAbandonedCartsEvent.php b/retailcrm/lib/events/RetailcrmAbandonedCartsEvent.php index e6fcc36..066260b 100644 --- a/retailcrm/lib/events/RetailcrmAbandonedCartsEvent.php +++ b/retailcrm/lib/events/RetailcrmAbandonedCartsEvent.php @@ -51,29 +51,39 @@ class RetailcrmAbandonedCartsEvent extends RetailcrmAbstractEvent implements Ret $this->setRunning(); - $syncCartsActive = Configuration::get(RetailCRM::SYNC_CARTS_ACTIVE); - - if (empty($syncCartsActive)) { - RetailcrmLogger::writeCaller(__METHOD__, 'Abandoned carts is disabled, skipping...'); + $shops = $this->getShops(); + if (!$shops) { return true; } - $api = RetailcrmTools::getApiClient(); + foreach ($shops as $shop) { + RetailcrmTools::setShopContext(intval($shop['id_shop'])); - if (empty($api)) { - RetailcrmLogger::writeCaller(__METHOD__, 'Set API key & URL first'); + $syncCartsActive = Configuration::get(RetailCRM::SYNC_CARTS_ACTIVE); - return true; + if (empty($syncCartsActive)) { + RetailcrmLogger::writeCaller(__METHOD__, 'Abandoned carts is disabled, skipping...'); + + continue; + } + + $api = RetailcrmTools::getApiClient(); + + if (empty($api)) { + RetailcrmLogger::writeCaller(__METHOD__, 'Set API key & URL first'); + + continue; + } + + RetailcrmCartUploader::init(); + RetailcrmCartUploader::$api = $api; + RetailcrmCartUploader::$paymentTypes = array_keys(json_decode(Configuration::get(RetailCRM::PAYMENT), true)); + RetailcrmCartUploader::$syncStatus = Configuration::get(RetailCRM::SYNC_CARTS_STATUS); + RetailcrmCartUploader::setSyncDelay(Configuration::get(RetailCRM::SYNC_CARTS_DELAY)); + RetailcrmCartUploader::run(); } - RetailcrmCartUploader::init(); - RetailcrmCartUploader::$api = $api; - RetailcrmCartUploader::$paymentTypes = array_keys(json_decode(Configuration::get(RetailCRM::PAYMENT), true)); - RetailcrmCartUploader::$syncStatus = Configuration::get(RetailCRM::SYNC_CARTS_STATUS); - RetailcrmCartUploader::setSyncDelay(Configuration::get(RetailCRM::SYNC_CARTS_DELAY)); - RetailcrmCartUploader::run(); - return true; } diff --git a/retailcrm/lib/events/RetailcrmAbstractEvent.php b/retailcrm/lib/events/RetailcrmAbstractEvent.php index cde1bab..16058d9 100644 --- a/retailcrm/lib/events/RetailcrmAbstractEvent.php +++ b/retailcrm/lib/events/RetailcrmAbstractEvent.php @@ -41,6 +41,7 @@ require_once(dirname(__FILE__) . '/../RetailcrmPrestashopLoader.php'); abstract class RetailcrmAbstractEvent implements RetailcrmEventInterface { private $cliMode; + private $shopId; /** * @inheritDoc @@ -62,7 +63,18 @@ abstract class RetailcrmAbstractEvent implements RetailcrmEventInterface */ public function setCliMode($mode) { - $this->cliMode = (bool) $mode; + $this->cliMode = (bool)$mode; + } + + /** + * Sets context shop id. + * + * @param string|int|null $shopId + */ + public function setShopId($shopId = null) + { + if (!is_null($shopId)) + $this->shopId = intval($shopId); } /** @@ -89,4 +101,46 @@ abstract class RetailcrmAbstractEvent implements RetailcrmEventInterface return RetailcrmJobManager::setCurrentJob($this->getName()); } + + /** + * Returns array of active shops or false. + * + * @return array|false + */ + protected function getShops() + { + $shops = Shop::getShops(); + + if (Shop::isFeatureActive()) { + if ($this->shopId > 0) { + if (isset($shops[$this->shopId])) { + RetailcrmLogger::writeDebug( + __METHOD__, + sprintf( + "Running job for shop %s (%s).", + $shops[$this->shopId]['name'], + $this->shopId + ) + ); + + return [$shops[$this->shopId]]; + } else { + RetailcrmLogger::writeDebug( + __METHOD__, + sprintf( + 'Shop with id=%s not found.', + $this->shopId + ) + ); + + return []; + } + } + + return $shops; + } else { + + return [$shops[Shop::getContextShopID()]]; + } + } } diff --git a/retailcrm/lib/events/RetailcrmEventInterface.php b/retailcrm/lib/events/RetailcrmEventInterface.php index 07dc316..704c1bf 100644 --- a/retailcrm/lib/events/RetailcrmEventInterface.php +++ b/retailcrm/lib/events/RetailcrmEventInterface.php @@ -59,4 +59,11 @@ interface RetailcrmEventInterface * @param bool $mode */ public function setCliMode($mode); + + /** + * Sets context shop id. + * + * @param $shopId + */ + public function setShopId($shopId); } diff --git a/retailcrm/lib/events/RetailcrmExportEvent.php b/retailcrm/lib/events/RetailcrmExportEvent.php index a34e6c7..773bebf 100644 --- a/retailcrm/lib/events/RetailcrmExportEvent.php +++ b/retailcrm/lib/events/RetailcrmExportEvent.php @@ -51,61 +51,67 @@ class RetailcrmExportEvent extends RetailcrmAbstractEvent implements RetailcrmEv $this->setRunning(); - $api = RetailcrmTools::getApiClient(); + $shops = $this->getShops(); - if (empty($api)) { - RetailcrmLogger::writeCaller(__METHOD__, 'Set API key & URL first'); + foreach ($shops as $shop) { + RetailcrmTools::setShopContext(intval($shop['id_shop'])); - return true; - } + $api = RetailcrmTools::getApiClient(); - $orders = array(); - $orderRecords = Order::getOrdersWithInformations(); - $orderBuilder = new RetailcrmOrderBuilder(); - $orderBuilder->defaultLangFromConfiguration()->setApi($api); + if (empty($api)) { + RetailcrmLogger::writeCaller(__METHOD__, 'Set API key & URL first'); - foreach ($orderRecords as $record) { - $orderBuilder->reset(); - - $order = new Order($record['id_order']); - - $orderCart = new Cart($order->id_cart); - $orderCustomer = new Customer($order->id_customer); - - $orderBuilder->setCmsOrder($order); - - if (!empty($orderCart->id)) { - $orderBuilder->setCmsCart($orderCart); - } else { - $orderBuilder->setCmsCart(null); + continue; } - if (!empty($orderCustomer->id)) { - $orderBuilder->setCmsCustomer($orderCustomer); - } else { - //TODO - // Caused crash before because of empty RetailcrmOrderBuilder::cmsCustomer. - // Current version *shouldn't* do this, but I suggest more tests for guest customers. - $orderBuilder->setCmsCustomer(null); + $orders = array(); + $orderRecords = Order::getOrdersWithInformations(); + $orderBuilder = new RetailcrmOrderBuilder(); + $orderBuilder->defaultLangFromConfiguration()->setApi($api); + + foreach ($orderRecords as $record) { + $orderBuilder->reset(); + + $order = new Order($record['id_order']); + + $orderCart = new Cart($order->id_cart); + $orderCustomer = new Customer($order->id_customer); + + $orderBuilder->setCmsOrder($order); + + if (!empty($orderCart->id)) { + $orderBuilder->setCmsCart($orderCart); + } else { + $orderBuilder->setCmsCart(null); + } + + if (!empty($orderCustomer->id)) { + $orderBuilder->setCmsCustomer($orderCustomer); + } else { + //TODO + // Caused crash before because of empty RetailcrmOrderBuilder::cmsCustomer. + // Current version *shouldn't* do this, but I suggest more tests for guest customers. + $orderBuilder->setCmsCustomer(null); + } + + try { + $orders[] = $orderBuilder->buildOrderWithPreparedCustomer(); + } catch (\InvalidArgumentException $exception) { + RetailcrmLogger::writeCaller('export', $exception->getMessage()); + RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); + RetailcrmLogger::output($exception->getMessage()); + } + + time_nanosleep(0, 500000000); } - try { - $orders[] = $orderBuilder->buildOrderWithPreparedCustomer(); - } catch (\InvalidArgumentException $exception) { - RetailcrmLogger::writeCaller('export', $exception->getMessage()); - RetailcrmLogger::writeNoCaller($exception->getTraceAsString()); - RetailcrmLogger::output($exception->getMessage()); + unset($orderRecords); + + $orders = array_chunk($orders, 50); + + foreach ($orders as $chunk) { + $api->ordersUpload($chunk); } - - time_nanosleep(0, 500000000); - } - - unset($orderRecords); - - $orders = array_chunk($orders, 50); - - foreach ($orders as $chunk) { - $api->ordersUpload($chunk); } return true; diff --git a/retailcrm/lib/events/RetailcrmIcmlEvent.php b/retailcrm/lib/events/RetailcrmIcmlEvent.php index 0c50e46..dc5c79a 100644 --- a/retailcrm/lib/events/RetailcrmIcmlEvent.php +++ b/retailcrm/lib/events/RetailcrmIcmlEvent.php @@ -51,11 +51,24 @@ class RetailcrmIcmlEvent extends RetailcrmAbstractEvent implements RetailcrmEven $this->setRunning(); - $job = new RetailcrmCatalog(); - $data = $job->getData(); + $shops = $this->getShops(); - $icml = new RetailcrmIcml(Configuration::get('PS_SHOP_NAME'), _PS_ROOT_DIR_ . '/retailcrm.xml'); - $icml->generate($data[0], $data[1]); + $isMultiStoreActive = Shop::isFeatureActive(); + + foreach ($shops as $shop) { + RetailcrmTools::setShopContext(intval($shop['id_shop'])); + + $job = new RetailcrmCatalog(); + $data = $job->getData(); + + if ($isMultiStoreActive) + $icmlFileName = 'retailcrm_' . $shop['id_shop'] . '.xml'; + else + $icmlFileName = 'retailcrm.xml'; + + $icml = new RetailcrmIcml($shop['name'], _PS_ROOT_DIR_ . '/' . $icmlFileName); + $icml->generate($data[0], $data[1]); + } return true; } diff --git a/retailcrm/lib/events/RetailcrmInventoriesEvent.php b/retailcrm/lib/events/RetailcrmInventoriesEvent.php index b8445d5..58c2011 100644 --- a/retailcrm/lib/events/RetailcrmInventoriesEvent.php +++ b/retailcrm/lib/events/RetailcrmInventoriesEvent.php @@ -51,28 +51,34 @@ class RetailcrmInventoriesEvent extends RetailcrmAbstractEvent implements Retail $this->setRunning(); - if (!Configuration::get(RetailCRM::ENABLE_BALANCES_RECEIVING)) { - RetailcrmLogger::writeDebug( - 'RetailcrmInventoriesEvent', - 'Balances receiving is not enabled, skipping...' - ); + $shops = $this->getShops(); - return true; + foreach ($shops as $shop) { + RetailcrmTools::setShopContext(intval($shop['id_shop'])); + + if (!Configuration::get(RetailCRM::ENABLE_BALANCES_RECEIVING)) { + RetailcrmLogger::writeDebug( + 'RetailcrmInventoriesEvent', + 'Balances receiving is not enabled, skipping...' + ); + + continue; + } + + $apiUrl = Configuration::get(RetailCRM::API_URL); + $apiKey = Configuration::get(RetailCRM::API_KEY); + + if (!empty($apiUrl) && !empty($apiKey)) { + RetailcrmInventories::$api = new RetailcrmProxy($apiUrl, $apiKey, RetailcrmLogger::getLogFile()); + } else { + RetailcrmLogger::writeCaller('inventories', 'set api key & url first'); + + continue; + } + + RetailcrmInventories::loadStocks(); } - $apiUrl = Configuration::get(RetailCRM::API_URL); - $apiKey = Configuration::get(RetailCRM::API_KEY); - - if (!empty($apiUrl) && !empty($apiKey)) { - RetailcrmInventories::$api = new RetailcrmProxy($apiUrl, $apiKey, RetailcrmLogger::getLogFile()); - } else { - RetailcrmLogger::writeCaller('inventories', 'set api key & url first'); - - return true; - } - - RetailcrmInventories::loadStocks(); - return true; } diff --git a/retailcrm/lib/events/RetailcrmMissingEvent.php b/retailcrm/lib/events/RetailcrmMissingEvent.php index d3612f0..8fe5f62 100644 --- a/retailcrm/lib/events/RetailcrmMissingEvent.php +++ b/retailcrm/lib/events/RetailcrmMissingEvent.php @@ -51,7 +51,7 @@ class RetailcrmMissingEvent extends RetailcrmAbstractEvent implements RetailcrmE $this->setRunning(); - $shortopts = 'o:'; + $shortopts = 'j:o:'; $options = getopt($shortopts); if (!isset($options['o'])) { @@ -60,6 +60,9 @@ class RetailcrmMissingEvent extends RetailcrmAbstractEvent implements RetailcrmE return true; } + $orderInstance = new Order($options['o']); + RetailcrmTools::setShopContext($orderInstance->id_shop); + $apiUrl = Configuration::get(RetailCRM::API_URL); $apiKey = Configuration::get(RetailCRM::API_KEY); @@ -75,8 +78,6 @@ class RetailcrmMissingEvent extends RetailcrmAbstractEvent implements RetailcrmE $payment = json_decode(Configuration::get(RetailCRM::PAYMENT), true); $status = json_decode(Configuration::get(RetailCRM::STATUS), true); - $orderInstance = new Order($options['o']); - $order = array( 'externalId' => $orderInstance->id, 'createdAt' => $orderInstance->date_add, diff --git a/retailcrm/lib/events/RetailcrmSyncEvent.php b/retailcrm/lib/events/RetailcrmSyncEvent.php index e038cca..c2bcee7 100644 --- a/retailcrm/lib/events/RetailcrmSyncEvent.php +++ b/retailcrm/lib/events/RetailcrmSyncEvent.php @@ -51,30 +51,36 @@ class RetailcrmSyncEvent extends RetailcrmAbstractEvent implements RetailcrmEven $this->setRunning(); - if (!Configuration::get(RetailCRM::ENABLE_HISTORY_UPLOADS)) { - RetailcrmLogger::writeDebug( - __METHOD__, - 'History uploads is not enabled, skipping...' - ); + $shops = $this->getShops(); - return true; + foreach ($shops as $shop) { + RetailcrmTools::setShopContext(intval($shop['id_shop'])); + + if (!Configuration::get(RetailCRM::ENABLE_HISTORY_UPLOADS)) { + RetailcrmLogger::writeDebug( + __METHOD__, + 'History uploads is not enabled, skipping...' + ); + + continue; + } + + $apiUrl = Configuration::get(RetailCRM::API_URL); + $apiKey = Configuration::get(RetailCRM::API_KEY); + RetailcrmHistory::$default_lang = (int)Configuration::get('PS_LANG_DEFAULT'); + + if (!empty($apiUrl) && !empty($apiKey)) { + RetailcrmHistory::$api = new RetailcrmProxy($apiUrl, $apiKey, RetailcrmLogger::getLogFile()); + } else { + RetailcrmLogger::writeCaller(__METHOD__, 'Set api key & url first'); + + continue; + } + + RetailcrmHistory::customersHistory(); + RetailcrmHistory::ordersHistory(); } - $apiUrl = Configuration::get(RetailCRM::API_URL); - $apiKey = Configuration::get(RetailCRM::API_KEY); - RetailcrmHistory::$default_lang = (int) Configuration::get('PS_LANG_DEFAULT'); - - if (!empty($apiUrl) && !empty($apiKey)) { - RetailcrmHistory::$api = new RetailcrmProxy($apiUrl, $apiKey, RetailcrmLogger::getLogFile()); - } else { - RetailcrmLogger::writeCaller(__METHOD__, 'Set api key & url first'); - - return true; - } - - RetailcrmHistory::customersHistory(); - RetailcrmHistory::ordersHistory(); - return true; } diff --git a/retailcrm/retailcrm.php b/retailcrm/retailcrm.php index a7257ff..0e24024 100644 --- a/retailcrm/retailcrm.php +++ b/retailcrm/retailcrm.php @@ -153,6 +153,10 @@ class RetailCRM extends Module public function install() { + if (Shop::isFeatureActive()) { + Shop::setContext(Shop::CONTEXT_ALL); + } + return ( parent::install() && $this->registerHook('newOrder') &&