_productRepositoryFactory = $productRepositoryFactory; $this->imageHelperFactory = $imageHelperFactory; $this->activeCampaignOrderHelper = $activeCampaignOrderHelper; $this->activeCampaignHelper = $activeCampaignHelper; $this->configInterface = $configInterface; $this->curl = $curl; $this->_customerRepositoryInterface = $customerRepositoryInterface; $this->storeRepository = $storeRepository; $this->customerFactory = $customerFactory; $this->storeManager = $storeManager; $this->customerModel = $customerModel; $this->addressRepository = $addressRepository; $this->eavAttribute = $eavAttribute; $this->coreHelper = $coreHelper; $this->customerResource = $customerResource; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * @param $OrderSyncNum * @return array */ public function orderDataSend($order) { $return = []; $acOrderId = 0; $isEnabled = $this->activeCampaignOrderHelper->isOrderSyncEnabled(); if ($isEnabled) { try { $connectionId = $this->activeCampaignHelper->getConnectionId($order->getStoreId()); $customerId = $order->getCustomerId(); $customerAcId = 0; if ($customerId) { $this->createEcomCustomer($order->getCustomerId(), $order); $customerEmail = $order->getCustomerEmail(); $customerModel = $this->customerFactory->create(); $this->customerResource->load($customerModel, $customerId); if ($customerModel->getAcCustomerId()) { $customerAcId = $customerModel->getAcCustomerId(); } } else { $customerEmail = $order->getBillingAddress()->getEmail(); $websiteId = $this->storeManager->getWebsite()->getWebsiteId(); $customerModel = $this->customerModel; $customerModel->setWebsiteId($websiteId); $customerModel->loadByEmail($customerEmail); if ($customerModel->getId()) { $customerId = $customerModel->getId(); } else { $customerId = 0; } $this->createEcomCustomer($customerId, $order); $customerModel = $this->customerFactory->create(); $this->customerResource->load($customerModel, $customerId); if ($customerModel->getAcCustomerId()) { $customerAcId = $customerModel->getAcCustomerId(); } else { if ($order->getAcTempCustomerId()) { $customerAcId = $order->getAcTempCustomerId(); } else { $AcCustomer = $this->curl->listAllCustomers( self::GET_METHOD, self::ECOM_CUSTOMER_ENDPOINT, $customerEmail ); foreach ($AcCustomer['data']['ecomCustomers'] as $Ac) { if ($Ac['connectionid'] == $connectionId) { $customerAcId = $Ac['id']; } } } } } foreach ($order->getAllVisibleItems() as $item) { $product = $this->_productRepositoryFactory->create() ->get($item->getSku()); $imageUrl = $this->imageHelperFactory->create() ->init($product, 'product_thumbnail_image')->getUrl(); $items[] = [ "externalid" => $item->getProductId(), "name" => $item->getName(), "price" => $this->activeCampaignHelper->priceToCents($item->getPrice()), "quantity" => $item->getQtyOrdered(), "category" => implode(', ', $product->getCategoryIds()), "sku" => $item->getSku(), "description" => $item->getDescription(), "imageUrl" => $imageUrl, "productUrl" => $product->getProductUrl() ]; } $data = [ "ecomOrder" => [ "externalid" => $order->getId(), "source" => 1, "email" => $customerEmail, "orderProducts" => $items, "orderDiscounts" => [ "discountAmount" => $this->activeCampaignHelper->priceToCents($order->getDiscountAmount()) ], "externalCreatedDate" => $order->getCreatedAt(), "externalUpdatedDate" => $order->getUpdatedAt(), "shippingMethod" => $order->getShippingMethod(), "totalPrice" => $this->activeCampaignHelper->priceToCents($order->getGrandTotal()), "shippingAmount" => $this->activeCampaignHelper->priceToCents($order->getShippingAmount()), "taxAmount" => $this->activeCampaignHelper->priceToCents($order->getTaxAmount()), "discountAmount" => $this->activeCampaignHelper->priceToCents($order->getDiscountAmount()), "currency" => $order->getOrderCurrencyCode(), "orderNumber" => $order->getIncrementId(), "connectionid" => $connectionId, "customerid" => $customerAcId ] ]; if (!$order->getAcOrderSyncId()) { $result = $this->curl->orderDataSend( self::METHOD, self::URL_ENDPOINT, $data ); if ($result['status'] == '422' || $result['status'] == '400') { $ecomAlreadyExistOrderData = []; $ecomAlreadyExistOrderResult = $this->curl->createContacts( self::GET_METHOD, self::URL_ENDPOINT, $ecomAlreadyExistOrderData ); $ecomOrders = $ecomAlreadyExistOrderResult['data']['ecomOrders']; foreach ($ecomOrders as $ecomKey => $customers) { $ecomOrderArray[$ecomOrders[$ecomKey]['email']] = $ecomOrders[$ecomKey]['id']; } $acOrderId = $ecomOrderArray[$customerEmail]; } else { $acOrderId = isset($result['data']['ecomOrders']['id']) ? $result['data']['ecomOrders']['id'] : null; } } else { $acOrderId = $order->getAcOrderSyncId(); } if ($acOrderId != 0) { $syncStatus = CronConfig::SYNCED; } else { $syncStatus = CronConfig::FAIL_SYNCED; } $order->setData("ac_order_sync_status", $syncStatus) ->setData("ac_order_sync_id", $acOrderId) ->save(); if (isset($result['success'])) { $return['success'] = __("Order data successfully synced!!"); } } catch (\Exception $e) { $return['success'] = false; $return['errorMessage'] = __($e->getMessage()); } } return $return; } /** * @param null $billingId * @return string|null * @throws \Magento\Framework\Exception\LocalizedException */ private function getTelephone($billingId = null) { if ($billingId) { $address = $this->addressRepository->getById($billingId); return $address->getTelephone(); } return null; } /** * @param $customerId * @return array * @throws \Magento\Framework\Exception\LocalizedException * @throws \Magento\Framework\Exception\NoSuchEntityException */ private function getFieldValues($customerId) { $fieldValues = []; $customAttributes = $this->_customerRepositoryInterface->getById($customerId); $customAttributes->getCustomAttributes(); if (!empty($customAttributes)) { foreach ($customAttributes as $attribute) { $attributeId = $this->eavAttribute->getIdByCode(CustomerModel::ENTITY, $attribute->getAttributeCode()); $attributeValues['field'] = $attributeId; $attributeValues['value'] = $attribute->getValue(); $fieldValues[] = $attributeValues; } } return $fieldValues; } /** * @param $customerId * @return array */ private function getCustomer($customerId) { $customerModel = $this->customerFactory->create(); $this->customerResource->load($customerModel, $customerId); return $customerModel; } /** * @param $customerId * @return array */ private function createEcomCustomer($customerId, $quote) { $ecomOrderArray = []; $ecomCustomerId = 0; $syncStatus = CronConfig::NOT_SYNCED; $customer = $this->getCustomer($customerId); if ($customerId) { $customerId = $customer->getId(); $contact['email'] = $customer->getEmail(); $customerEmail = $customer->getEmail(); $contact['firstName'] = $customer->getFirstname(); $contact['lastName'] = $customer->getLastname(); $contact['phone'] = $this->getTelephone($customer->getDefaultBilling()); $contact['fieldValues'] = $this->getFieldValues($customerId); } else { $customerId = 0; $contact['email'] = $quote->getBillingAddress()->getEmail(); $customerEmail = $quote->getBillingAddress()->getEmail(); $contact['firstName'] = $quote->getBillingAddress()->getFirstname(); $contact['lastName'] = $quote->getBillingAddress()->getLastname(); $contact['phone'] = $quote->getBillingAddress()->getTelephone(); $contact['fieldValues'] = []; } $contactData['contact'] = $contact; try { $contactResult = $this->curl->createContacts(self::METHOD, self::CONTACT_ENDPOINT, $contactData); $contactId = isset($contactResult['data']['contact']['id']) ? $contactResult['data']['contact']['id'] : null; $connectionid = $this->coreHelper->getConnectionId($customer->getStoreId()); if (isset($contactResult['data']['contact']['id'])) { if (!$customer->getAcCustomerId()) { $ecomCustomer['connectionid'] = $connectionid; $ecomCustomer['externalid'] = $customerId; $ecomCustomer['email'] = $customerEmail; $ecomCustomerData['ecomCustomer'] = $ecomCustomer; $AcCustomer = $this->curl->listAllCustomers( self::GET_METHOD, self::ECOM_CUSTOMERLIST_ENDPOINT, $customerEmail ); if (ISSET($AcCustomer['data']['ecomCustomers'][0])) { foreach ($AcCustomer['data']['ecomCustomers'] as $Ac) { if ($Ac['connectionid'] == $connectionid) { $ecomCustomerId = $Ac['id']; } } } if (!$ecomCustomerId) { $ecomCustomerResult = $this->curl->createContacts( self::METHOD, self::ECOM_CUSTOMERLIST_ENDPOINT, $ecomCustomerData ); $ecomCustomerId = isset($ecomCustomerResult['data']['ecomCustomer']['id']) ? $ecomCustomerResult['data']['ecomCustomer']['id'] : null; } } else { $ecomCustomerId = $customer->getAcCustomerId(); } } if ($ecomCustomerId != 0) { $syncStatus = CronConfig::SYNCED; } else { $syncStatus = CronConfig::FAIL_SYNCED; } if ($customerId) { $this->saveCustomerResult($customerId, $syncStatus, $contactId, $ecomCustomerId); } else { $this->saveCustomerResultQuote($quote, $ecomCustomerId); } } catch (\Exception $e) { $this->logger->critical($e); } } /** * @param $customerId * @param $syncStatus * @param $contactId * @param $ecomCustomerId * @throws \Magento\Framework\Exception\AlreadyExistsException */ private function saveCustomerResult($customerId, $syncStatus, $contactId, $ecomCustomerId) { $customerModel = $this->customerFactory->create(); if ($customerId) { $this->customerResource->load($customerModel, $customerId); } $customerModel->setAcSyncStatus($syncStatus); $customerModel->setAcContactId($contactId); $customerModel->setAcCustomerId($ecomCustomerId); $this->customerResource->save($customerModel); } /** * @param $quote * @param $ecomCustomerId * @throws \Magento\Framework\Exception\AlreadyExistsException */ private function saveCustomerResultQuote($quote, $ecomCustomerId) { if ($ecomCustomerId) { $quote->setData("ac_temp_customer_id", $ecomCustomerId); $quote->save(); } } }