|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +ini_set('display_errors', 1); |
| 5 | +ini_set('display_startup_errors', 1); |
| 6 | +error_reporting(E_ALL); |
| 7 | + |
| 8 | +require_once('fs_cleverreach_interface_rest_client.php'); |
| 9 | +require("../includes/application_top_export.php"); |
| 10 | +require("../inc/xtc_not_null.inc.php"); |
| 11 | +require("../inc/xtc_get_country_name.inc.php"); |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +if(!defined('MODULE_FS_CLEVERREACH_INTERFACE_STATUS') || MODULE_FS_CLEVERREACH_INTERFACE_STATUS != 'true') |
| 17 | +{ |
| 18 | + header('Location: ' . preg_replace("/[\r\n]+(.*)$/i", "", html_entity_decode($_SERVER['HTTP_REFERER']))); |
| 19 | + exit(); |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +if(!defined('MODULE_FS_CLEVERREACH_INTERFACE_CLIENT_ID') || !defined('MODULE_FS_CLEVERREACH_INTERFACE_USERNAME') || !defined('MODULE_FS_CLEVERREACH_INTERFACE_PASSWORD')) |
| 25 | +{ |
| 26 | + |
| 27 | + header('Location: ' . preg_replace("/[\r\n]+(.*)$/i", "", html_entity_decode($_SERVER['HTTP_REFERER']))); |
| 28 | + exit(); |
| 29 | +} |
| 30 | + |
| 31 | +$rest = new \CR\tools\rest("https://rest.cleverreach.com/v2"); |
| 32 | + |
| 33 | + |
| 34 | +if (trim(MODULE_FS_CLEVERREACH_INTERFACE_CLIENT_ID) == '' || trim(MODULE_FS_CLEVERREACH_INTERFACE_USERNAME) == '' || trim(MODULE_FS_CLEVERREACH_INTERFACE_PASSWORD) == '') { |
| 35 | + die('Please enter all login credentials for Cleverreach!'); |
| 36 | +} |
| 37 | + |
| 38 | +$token = $rest->post('/login', |
| 39 | + array( |
| 40 | + "client_id"=> MODULE_FS_CLEVERREACH_INTERFACE_CLIENT_ID, |
| 41 | + "login"=> MODULE_FS_CLEVERREACH_INTERFACE_USERNAME, |
| 42 | + "password"=> MODULE_FS_CLEVERREACH_INTERFACE_PASSWORD |
| 43 | + ) |
| 44 | +); |
| 45 | + |
| 46 | +$rest->setAuthMode("bearer", $token); |
| 47 | + |
| 48 | +$groups = $rest->get("/groups"); |
| 49 | + |
| 50 | +$group_id = MODULE_FS_CLEVERREACH_INTERFACE_GROUP_ID; |
| 51 | + |
| 52 | +if (!isset($group_id)) { |
| 53 | + die('No groups found! Please create one in the Cleverreach backend'); |
| 54 | +} |
| 55 | + |
| 56 | +$receivers = array(); |
| 57 | + |
| 58 | + |
| 59 | + $manual_registered_customers = xtc_db_query("SELECT |
| 60 | + customers_id, |
| 61 | + customers_email_address as email, |
| 62 | + date_added as registered, |
| 63 | + customers_firstname as firstname, |
| 64 | + customers_lastname as lastname |
| 65 | + FROM " . TABLE_NEWSLETTER_RECIPIENTS . " WHERE mail_status = '1' "); |
| 66 | + |
| 67 | + while ($customer = xtc_db_fetch_array($manual_registered_customers)) { |
| 68 | + $orders = array(); |
| 69 | + $order_rows = xtc_db_query("SELECT o.orders_id, op.products_id, op.products_name, op.products_price, op.products_quantity from " . TABLE_ORDERS . " o JOIN " . TABLE_ORDERS_PRODUCTS . " op ON o.orders_id = op.orders_id WHERE customers_id = '" . $customer['customers_id'] . "' ORDER BY date_purchased "); |
| 70 | + while ($order_row = xtc_db_fetch_array($order_rows)) { |
| 71 | + |
| 72 | + $orders[] = array( |
| 73 | + "order_id" => $order_row["orders_id"], //required |
| 74 | + "product_id" => $order_row["products_id"], //optional |
| 75 | + "product" => $order_row["products_name"], //required |
| 76 | + "price" => $order_row["products_price"], //optional |
| 77 | + "currency" => "EUR", //optional |
| 78 | + "amount" => $order_row["products_quantity"], //optional |
| 79 | + "source" => STORE_NAME //optional |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + $gender_query = xtc_db_query("SELECT c.customers_gender as gender, ab.entry_country_id as country, ab.entry_city as city, ab.entry_postcode as zip, ab.entry_street_address as street FROM " . TABLE_CUSTOMERS . " c JOIN ".TABLE_ADDRESS_BOOK." ab ON c.customers_id = ab.customers_id WHERE customers_id = '" . $customer['customers_id'] . "' AND c.customers_default_address_id = ab.address_book_id"); |
| 84 | + $customers_data = xtc_db_fetch_array($gender_query); |
| 85 | + $country = xtc_get_country_name($customers_data['country']); |
| 86 | + |
| 87 | + $receivers[] = array( |
| 88 | + "email" => $customer["email"], |
| 89 | + "registered" => strtotime($customer["registered"]), |
| 90 | + "activated" => strtotime($customer["registered"]), |
| 91 | + "source" => STORE_NAME, |
| 92 | + "attributes" => array( |
| 93 | + "city" => $customer["city"], |
| 94 | + "street" => $customer["street"], |
| 95 | + "zip" => $customer["zip"], |
| 96 | + "country" => $country |
| 97 | + ), |
| 98 | + "global_attributes" => array( |
| 99 | + "firstname" => $customer["firstname"], |
| 100 | + "lastname" => $customer["lastname"], |
| 101 | + "geschlecht" => $customers_data["gender"] |
| 102 | + ), |
| 103 | + "orders" => $orders |
| 104 | + ); |
| 105 | + |
| 106 | + if (count($receivers) > 1000) { |
| 107 | + $rest->post("/groups.json/".$group_id."/receivers", $receivers); |
| 108 | + $receivers = array(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | +if (MODULE_FS_CLEVERREACH_INTERFACE_IMPORT_BUYERS == 'true') { |
| 114 | + $order_rows = xtc_db_query("SELECT DISTINCT o.orders_id, o.customers_id, o.customers_email_address as email, o.customers_firstname as firstname, o.customers_lastname as lastname, o.customers_gender as gender, o.customers_street_address as street, o.customers_city as city, o.customers_postcode as zip, o.customers_country as country, o.date_purchased, op.products_id, op.products_name, op.products_price, op.products_quantity from " . TABLE_ORDERS . " o JOIN " . TABLE_ORDERS_PRODUCTS . " op ON o.orders_id = op.orders_id GROUP BY o.customers_id ORDER BY o.date_purchased "); |
| 115 | + while ($order_row = xtc_db_fetch_array($order_rows)) { |
| 116 | + |
| 117 | + $orders = array(); |
| 118 | + |
| 119 | + $orders[] = array( |
| 120 | + "order_id" => $order_row["orders_id"], //required |
| 121 | + "product_id" => $order_row["products_id"], //optional |
| 122 | + "product" => utf8_encode($order_row["products_name"]), //required |
| 123 | + "price" => $order_row["products_price"], //optional |
| 124 | + "currency" => "EUR", //optional |
| 125 | + "amount" => $order_row["products_quantity"], //optional |
| 126 | + "source" => STORE_NAME //optional |
| 127 | + ); |
| 128 | + |
| 129 | + $flagged_customers = xtc_db_query(" SELECT c.customers_date_added as registered, c.customers_gender as gender, c.customers_dob as dob FROM " . TABLE_CUSTOMERS . " c WHERE c.customers_id = '" . $order_row["customers_id"] . "'"); |
| 130 | + $receivers = array('postdata' => array()); |
| 131 | + |
| 132 | + if (xtc_db_num_rows($flagged_customers) > 0) { |
| 133 | + while ($customer = xtc_db_fetch_array($flagged_customers)) { |
| 134 | + |
| 135 | + $receivers["postdata"][] = array( |
| 136 | + "email" => $order_row["email"], |
| 137 | + "activated" => strtotime($customer["registered"]), |
| 138 | + "registered" => strtotime($customer["registered"]), |
| 139 | + "source" => STORE_NAME, |
| 140 | + "global_attributes" => array( |
| 141 | + "nachname" => utf8_encode($order_row["firstname"]), |
| 142 | + "vorname" => utf8_encode($order_row["lastname"]), |
| 143 | + "m__nnlich_weiblich" => $customer["gender"], |
| 144 | + "geburtsdatum" => $customer['dob'], |
| 145 | + "ort" => utf8_encode($order_row["city"]), |
| 146 | + "street" => utf8_encode($order_row["street"]), |
| 147 | + "zip" => $order_row["zip"], |
| 148 | + "country" => utf8_encode($order_row["country"]) |
| 149 | + ), |
| 150 | + "orders" => $orders |
| 151 | + ); |
| 152 | + |
| 153 | + if (count($receivers) > 1000) { |
| 154 | + $rest->post("/groups.json/".$group_id."/receivers", $receivers); |
| 155 | + $receivers = array(); |
| 156 | + } |
| 157 | + } |
| 158 | + } else { |
| 159 | + $receivers["postdata"][] = array( |
| 160 | + "email" => $order_row["email"], |
| 161 | + "activated" => strtotime($order_row["date_purchased"]), |
| 162 | + "registered" => strtotime($order_row["date_purchased"]), |
| 163 | + "source" => STORE_NAME, |
| 164 | + "global_attributes" => array( |
| 165 | + "nachname" => utf8_encode($order_row["firstname"]), |
| 166 | + "vorname" => utf8_encode($order_row["lastname"]), |
| 167 | + "m__nnlich_weiblich" => $customer["gender"], |
| 168 | + "geburtsdatum" => $customer['dob'], |
| 169 | + "ort" => utf8_encode($order_row["city"]), |
| 170 | + "street" => utf8_encode($order_row["street"]), |
| 171 | + "zip" => $order_row["zip"], |
| 172 | + "country" => utf8_encode($order_row["country"]) |
| 173 | + ), |
| 174 | + "orders" => $orders |
| 175 | + ); |
| 176 | + |
| 177 | + if (count($receivers) > 1000) { |
| 178 | + $rest->post("/groups.json/".$group_id."/receivers", $receivers); |
| 179 | + $receivers = array(); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +if (count($receivers) > 0) { |
| 186 | + $rest->post("/groups.json/".$group_id."/receivers", $receivers); |
| 187 | +} else { |
| 188 | + die('No new receivers found'); |
| 189 | +} |
| 190 | +$receivers = array(); |
| 191 | + |
| 192 | +header('Location: ' . preg_replace("/[\r\n]+(.*)$/i", "", html_entity_decode($_SERVER['HTTP_REFERER']))); |
| 193 | +exit(); |
0 commit comments