getUntrackedProducts(); $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); if (is_array($checkProducts) && sizeof($checkProducts) > 0) { $shop = get_option('woo_ml_shop_id', false); if ($shop === false) { return [ 'error' => true, 'message' => 'Shop is not activated.' ]; } $syncProducts = []; foreach ($checkProducts as $post) { if (!isset($post->ID)) { continue; } $product = wc_get_product($post->ID); $productID = $product->get_id(); $productName = $product->get_name() ?: 'Untitled product'; $productPrice = floatval($product->get_price('edit')); $productImage = ProductProcess::getInstance()->productImage($product); $productURL = $product->get_permalink(); $categories = get_the_terms($productID, 'product_cat'); $productCategories = []; foreach ($categories as $category) { if (isset($category->term_id) && is_numeric($category->term_id)) { if(!in_array((string) $category->term_id, $productCategories)) { $productCategories[] = (string)$category->term_id; } } } $exclude_automation = get_post_meta($productID, '_woo_ml_product_ignored', true) === "1"; $syncProduct = [ 'resource_id' => (string)$productID, 'name' => $productName, 'price' => $productPrice, 'url' => $productURL, 'exclude_from_automations' => $exclude_automation, 'categories' => $productCategories ]; if (!empty($productImage)) { $syncProduct['image'] = (string)$productImage; } $syncProducts[] = $syncProduct; } $syncCount = 0; if (count($syncProducts) > 0) { $result = $mailerliteClient->importProducts($shop, $syncProducts); if (empty($result) || $mailerliteClient->responseCode() == 422 || $mailerliteClient->responseCode() == 500) { $errorMsg = json_decode($mailerliteClient->getResponseBody()); $message = 'Oops, we did not manage to sync all of your products, please try again. (' . $mailerliteClient->responseCode() . ')'; if ($mailerliteClient->responseCode() == 422 && isset($errorMsg->message)) { $message = $errorMsg->message; } return [ 'error' => true, 'code' => $mailerliteClient->responseCode(), 'message' => $message ]; } if ($mailerliteClient->responseCode() == 201 || $mailerliteClient->responseCode() == 200) { foreach ($syncProducts as $product) { MailerLiteSettings::getInstance()->completeProductTracking($product['resource_id']); $syncCount++; } } } return [ 'completed' => $syncCount, 'code' => $mailerliteClient->responseCode() ]; } return []; } catch (\Exception $e) { return [ 'error' => true, 'message' => $message ]; } } /** * Bulk synchronize untracked resources * woo_ml_sync_untracked_resources * @return bool * @throws Exception */ public function syncUntrackedResources() { if(!get_option('woo_ml_shop_id', false) && (get_option('woo_ml_wizard_setup', 0) == 1)) { MailerLiteSettings::getInstance()->updateSettings([]); } $trackingData = TrackingData::getInstance(); if ((int)get_option('woo_mailerlite_platform', 1) === ApiType::CURRENT) { $untracked_categories_count = $trackingData->getUntrackedCategoriesCount(); if ($untracked_categories_count > 0) { return json_encode(array_merge([ 'allDone' => false, ], $this->syncUntrackedCategories())); } $untracked_products_count = $trackingData->getUntrackedProductsCount(); if ($untracked_products_count > 0) { return json_encode(array_merge([ 'allDone' => false, ], $this->syncUntrackedProducts())); } } $total_customers_count = TrackingData::getInstance()->getAllCustomersCount(); $tracked_customers_count = TrackingData::getInstance()->getTrackedCustomersCount(); $tracked_guest_count = TrackingData::getInstance()->getTrackedGuestCustomersCount(); $untracked_customers_count = $total_customers_count - $tracked_customers_count - $tracked_guest_count; if ($untracked_customers_count > 0) { return json_encode(array_merge([ 'allDone' => false, ], $this->syncUntrackedCustomers())); } return json_encode([ 'allDone' => true, 'completed' => 0, ]); } /** * Bulk synchronize untracked customers * woo_ml_sync_untracked_customers * @return array * @throws Exception */ public function syncUntrackedCustomers() { set_time_limit(600); $message = 'Oops, we did not manage to sync all of your customers, please try again.'; try { $trackingData = TrackingData::getInstance(); $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); $shop = get_option('woo_ml_shop_id', false); if ($shop === false && $mailerliteClient->getApiType() == ApiType::CURRENT) { return [ 'error' => true, 'message' => 'Shop is not activated.' ]; } $registeredCustomers = $trackingData->getRegisteredCustomersToSync(); $syncCount = 0; $syncCustomers = []; if(count($registeredCustomers)) { foreach($registeredCustomers as $customer) { if(is_null($customer['email'])) { continue; } $wc_customer = new \WC_Customer($customer['user_id']); $syncCustomer = [ 'resource_id' => $customer['resource_id'], 'email' => $customer['email'], 'create_subscriber' => $customer['create_subscriber'], 'accepts_marketing' => $customer['create_subscriber'], 'orders_count' => $customer['orders_count'], 'total_spent' => $customer['total_spent'], 'last_order_id' => $customer['last_order_id'], 'last_order' => $customer['last_order'], ]; $syncCustomer['subscriber_fields'] = [ "name" => $customer['name'], "last_name" => $customer['last_name'], "company" => $customer['company'], "city" => $customer['city'], "state" => $customer['state'], "country" => $customer['country'], "phone" => $customer['phone'], ]; if ($mailerliteClient->getApiType() == ApiType::CURRENT) { $syncCustomer['subscriber_fields']['z_i_p'] = $customer['postcode']; } else { $syncCustomer['subscriber_fields']['zip'] = $customer['postcode']; } $wc_customer->add_meta_data('_woo_ml_customer_tracked', true, true); $wc_customer->save_meta_data(); $syncCustomers[] = $syncCustomer; } } else { $lastSyncedGuest = get_option('woo_ml_last_synced_guest_id', 0); $guestCustomers = $trackingData->getGuestCustomersToSync($lastSyncedGuest); if(!count($guestCustomers)) { return [ 'allDone' => true, 'completed' => 0, ]; } foreach ($guestCustomers as $customer) { if(is_null($customer['email'])) { continue; } $syncCustomer = []; $syncCustomer = [ 'resource_id' => $customer['resource_id'], 'email' => $customer['email'], 'create_subscriber' => $customer['create_subscriber'], 'accepts_marketing' => $customer['create_subscriber'], 'orders_count' => $customer['orders_count'], 'total_spent' => $customer['total_spent'], 'last_order_id' => $customer['last_order_id'], 'last_order' => $customer['last_order'], ]; $syncCustomer['subscriber_fields'] = [ "name" => $customer['name'], "last_name" => $customer['last_name'], "company" => $customer['company'], "city" => $customer['city'], "state" => $customer['state'], "country" => $customer['country'], "phone" => $customer['phone'], ]; if ($mailerliteClient->getApiType() == ApiType::CURRENT) { $syncCustomer['subscriber_fields']['z_i_p'] = $customer['postcode']; } else { $syncCustomer['subscriber_fields']['zip'] = $customer['postcode']; } $syncCustomers[] = $syncCustomer; update_option('woo_ml_last_synced_guest_id', end($guestCustomers)['resource_id']); } } if (count($syncCustomers) > 0) { if ($mailerliteClient->getApiType() == ApiType::CLASSIC) { foreach ($syncCustomers as $syncCustomer) { $subscriber_fields = $syncCustomer['subscriber_fields']; $subscriber_fields['woo_orders_count'] = $syncCustomer['orders_count']; $subscriber_fields['woo_total_spent'] = $syncCustomer['total_spent']; $subscriber_fields['woo_last_order'] = $syncCustomer['last_order']; $subscriber_fields['woo_last_order_id'] = $syncCustomer['last_order_id']; $store = home_url(); $subscriber_updated = $mailerliteClient->syncCustomer($store, $syncCustomer['resource_id'], $syncCustomer['email'], $subscriber_fields); if (isset($subscriber_updated->updated_subscriber)) { // used for updating order meta } $syncCount++; } } if ($mailerliteClient->getApiType() == ApiType::CURRENT) { $result = $mailerliteClient->importCustomers($shop, $syncCustomers); if ($mailerliteClient->responseCode() !== 200) { $errorMsg = json_decode($mailerliteClient->getResponseBody()); $message = 'Oops, we did not manage to sync all of your customers, please try again. (' . $mailerliteClient->responseCode() . ')'; if ($mailerliteClient->responseCode() == 422 && isset($errorMsg->message)) { $message = $errorMsg->message; } return [ 'error' => true, 'code' => $mailerliteClient->responseCode(), 'message' => $mailerliteClient->getResponseBody() ]; } $syncCount += count($result); } } return [ 'completed' => $syncCount, 'data' => $syncCustomers ]; } catch (\Exception $e) { return [ 'error' => true, 'message' => $message ]; } } /** * Bulk synchronize untracked categories * woo_ml_sync_untracked_categories * @return array */ public function syncUntrackedCategories() { set_time_limit(600); try { $syncCount = 0; $checkCategories = TrackingData::getInstance()->getUntrackedCategories(); $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); if (is_array($checkCategories) && sizeof($checkCategories) > 0) { $shop = get_option('woo_ml_shop_id', false); if ($shop === false) { return [ 'error' => true, 'message' => 'Shop is not activated.' ]; } $importCategories = []; foreach ($checkCategories as $category) { if (!isset($category->term_id)) { continue; } $importCategories[] = [ 'name' => $category->name, 'resource_id' => (string)$category->term_id ]; } if (count($importCategories) > 0) { $result = $mailerliteClient->importCategories($shop, $importCategories); if ($mailerliteClient->responseCode() !== 200) { return [ 'error' => true, ]; } foreach ($result as $category) { MailerLiteSettings::getInstance()->completeCategoryTracking($category->resource_id, $category->id); $syncCount++; } } } return [ 'completed' => $syncCount, ]; } catch (\Exception $e) { return [ 'error' => true, ]; } } /** * Call to handle product, order and customer delete event * mailerlite_wp_sync_post_delete */ public function syncPostDelete($post_id) { $mailerliteClient = new PlatformAPI(MAILERLITE_WP_API_KEY); if ($mailerliteClient->getApiType() === ApiType::CURRENT) { $shop = get_option('woo_ml_shop_id', false); if ($shop === false) { return false; } if (get_post_type($post_id) === 'product') { $mailerliteClient->deleteProduct($shop, $post_id); } if (OrderUtil::get_order_type($post_id) === 'shop_order') { $mailerliteClient->deleteOrder($shop, $post_id); } } } }