* @copyright 2020 DIGITAL RETAIL TECHNOLOGIES SL * @license https://opensource.org/licenses/MIT The MIT License * * Don't forget to prefix your containers with your own identifier * to avoid any conflicts with others containers. */ require_once(dirname(__FILE__) . '/../RetailcrmPrestashopLoader.php'); class RetailcrmExportEvent implements RetailcrmEventInterface { /** * @inheritDoc */ public function execute() { $api = RetailcrmTools::getApiClient(); if (empty($api)) { RetailcrmLogger::writeCaller('orderHistory', 'set api key & url first'); exit(); } $orders = array(); $orderRecords = Order::getOrdersWithInformations(); $orderBuilder = new RetailcrmOrderBuilder(); $orderBuilder->defaultLangFromConfiguration()->setApi($api); foreach ($orderRecords as $record) { $order = new Order($record['id_order']); $orderCart = new Cart($order->id_cart); $orderCustomer = new Customer($order->id_customer); 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); } if (!empty($orderCart->id)) { $orderBuilder->setCmsCart($orderCart); } else { $orderBuilder->setCmsCart(null); } $orderBuilder->setCmsOrder($order); try { $orders[] = $orderBuilder->buildOrderWithPreparedCustomer(); } catch (\InvalidArgumentException $exception) { RetailcrmLogger::writeCaller('export', $exception->getMessage()); RetailcrmLogger::writeNoCaller($e->getTraceAsString()); if (PHP_SAPI == 'cli') { echo $exception->getMessage() . PHP_EOL; } } time_nanosleep(0, 500000000); } unset($orderRecords); $orders = array_chunk($orders, 50); foreach ($orders as $chunk) { $api->ordersUpload($chunk); } } } $event = new RetailcrmExportEvent(); $event->execute();