customerRepository = $customerRepository; $this->customerFactory = $customerFactory; $this->addressFactory = $addressFactory; $this->helper = $helper; $this->logger = $logger; } /** * Create or Update a customer based on the provided data * * @param array $data * @return array * @throws LocalizedException */ public function createOrSkip(array $data) { $this->logger->info("[CustomersSyncService] createOrSkip: customer data -> " . json_encode($data, JSON_PRETTY_PRINT)); if (!isset($data['email']) || empty($data['email'])) { throw new LocalizedException(__('Email is required.')); } if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) { throw new LocalizedException(__('Invalid email address.')); } try { // Try to load existing customer by email $customer = $this->customerRepository->get($data['email']); return [ 'data' => $customer, 'is_new' => false ]; } catch (NoSuchEntityException $e) { if (!isset($data['firstname']) || empty($data['firstname'])) { throw new LocalizedException(__('First name is required.')); } if (!isset($data['lastname']) || empty($data['lastname'])) { throw new LocalizedException(__('Last name is required.')); } // Customer does not exist, create a new one $customer = $this->customerFactory->create(); $customer->setWebsiteId('1')->setStoreId('1'); // Populate customer object with new data $customer->setEmail($data['email']) ->setFirstname($data['firstname'] ?? '') ->setLastname($data['lastname'] ?? ''); // set customer group if (!empty($data['group_id'] ?? '')) { $customer->setGroupId($data['group_id']); } // set reporting office if (!empty($data['reporting_office'] ?? '')) { $customer->setNetsuiteCsegNcgRepOffice($data['reporting_office']); } // Additional data can be set as needed $customer // ->setCustomAttribute('is_active', $data['is_active'] ?? true) // ->setCustomAttribute('is_netsuite_customer', true) ->setCustomAttribute('company_name', $data['company_name'] ?? '') ->setCustomAttribute('netsuite_id', $data['netsuite_customer_id'] ?? ''); // Save customer $customer = $this->customerRepository->save($customer); $addresses = []; // If shipping_address data is provided, create and associate address with customer $shippingAddressData = $data['shipping_address'] ?? null; if ($shippingAddressData && !empty($shippingAddressData['street'][0]) && !empty($shippingAddressData['city']) && !empty($shippingAddressData['postcode'])) { $shippingAddress = $this->addressFactory->create(['data' => $shippingAddressData]); $shippingAddress->setIsDefaultShipping(true); $addresses[] = $shippingAddress; } // If billing_address data is provided, create and associate address with customer $billingAddressData = $data['billing_address'] ?? null; if ($billingAddressData && !empty($billingAddressData['street'][0]) && !empty($billingAddressData['city']) && !empty($billingAddressData['postcode'])) { $billingAddress = $this->addressFactory->create(['data' => $billingAddressData]); $billingAddress->setIsDefaultBilling(true); $addresses[] = $billingAddress; } $customer->setAddresses($addresses); $customer = $this->customerRepository->save($customer); return [ 'data' => $customer, 'is_new' => true ]; } } /** * Save NetSuite Customer ID to Magento Customer * * @param string $email * @param string $netsuiteCustomerId * @return void */ public function saveNetsuiteId(string $email, string $netsuiteCustomerId) { $customer = $this->customerRepository->get($email); $customer->setCustomAttribute('netsuite_id', $netsuiteCustomerId); $this->customerRepository->save($customer); } }