public function sendCampaign() { // get abandoned cart : $sql = "SELECT * FROM (\n\t\tSELECT\n\t\tCONCAT(LEFT(c.`firstname`, 1), '. ', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, c.id_customer, a.id_cart, a.date_upd,a.date_add,\n\t\t\t\tIF (IFNULL(o.id_order, 'Non ordered') = 'Non ordered', IF(TIME_TO_SEC(TIMEDIFF('" . date('Y-m-d H:i:s') . "', a.`date_add`)) > 86400, 'Abandoned cart', 'Non ordered'), o.id_order) id_order, IF(o.id_order, 1, 0) badge_success, IF(o.id_order, 0, 1) badge_danger, IF(co.id_guest, 1, 0) id_guest\n\t\tFROM `" . _DB_PREFIX_ . "cart` a \n\t\t\t\tJOIN `" . _DB_PREFIX_ . "customer` c ON (c.id_customer = a.id_customer)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "currency` cu ON (cu.id_currency = a.id_currency)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "carrier` ca ON (ca.id_carrier = a.id_carrier)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "orders` o ON (o.id_cart = a.id_cart)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "connections` co ON (a.id_guest = co.id_guest AND TIME_TO_SEC(TIMEDIFF('" . date('Y-m-d H:i:s') . "', co.`date_add`)) < 1800)\n\t\t) AS toto WHERE id_order='Abandoned cart'"; $currency = Context::getContext()->currency->sign; $defaultLanguage = new Language((int) Configuration::get('PS_LANG_DEFAULT')); $abandoned_carts = Db::getInstance()->ExecuteS($sql); // get all available campaigns $sqlCampaigns = 'SELECT * FROM `' . _DB_PREFIX_ . 'campaign` WHERE active=1'; $allCampaigns = Db::getInstance()->ExecuteS($sqlCampaigns); // loop on all abandoned carts foreach ($abandoned_carts as $abncart) { // loop on all available campaigns foreach ($allCampaigns as $camp) { $cartIsOnCampaign = $this->checkIfCartIsOnCampaign($abncart['date_add'], $camp['execution_time_day'], $camp['execution_time_hour']); if ($cartIsOnCampaign) { $id_lang = (int) Configuration::get('PS_LANG_DEFAULT'); $customer = new Customer($abncart['id_customer']); $cR = new CartRule($camp['id_voucher'], $id_lang); $cart = new Cart($abncart['id_cart']); $products = $cart->getProducts(); $campM = new Campaign($camp['id_campaign']); if (!empty($products)) { $cart_content = $campM->getCartContentHeader(); } else { $cart_content = ''; } foreach ($products as $prod) { $p = new Product($prod['id_product'], true, $id_lang); $price_no_tax = Product::getPriceStatic($p->id, false, null, 2, null, false, true, 1, false, null, $abncart['id_cart'], null, $null, true, true, null, false, false); $total_no_tax = $prod['cart_quantity'] * $price_no_tax; $images = Image::getImages((int) $id_lang, (int) $p->id); $link = new Link(); $cart_content .= '<tr > <td align="center" ><img src="' . $link->getImageLink($p->link_rewrite, $images[0]['id_image']) . '" width="80"/></td> <td align="center" ><a href="' . $link->getProductLink($p) . '"/>' . $p->name . '</a></td> <td align="center" >' . Tools::displayprice($price_no_tax) . '</td> <td align="center" >' . $prod['cart_quantity'] . '</td> <td align="center" >' . Tools::displayprice($total_no_tax) . '</td> </tr>'; } $tpl_vars = array('{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{coupon_name}' => $cR->name, '{coupon_code}' => $cR->code, '{cart_content}' => $cart_content, '{coupon_value}' => $camp['voucher_amount_type'] == 'percent' ? $cR->reduction_percent . '%' : $currency . $cR->reduction_amount, '{coupon_valid_to}' => date('d/m/Y', strtotime($cR->date_to)), '{campaign_name}' => $camp['name']); $path = _PS_ROOT_DIR_ . '/modules/superabandonedcart/mails/'; // send email to customer : Mail::Send($id_lang, $campM->getFileName(), $camp['name'], $tpl_vars, $customer->email, null, null, null, null, null, $path, false, Context::getContext()->shop->id); // Email to admin : Mail::Send($id_lang, $campM->getFileName(), Mail::l(sprintf('Email sent to %s %s for campaign %s', $customer->lastname, $customer->firstname, $camp['name'])), $tpl_vars, Configuration::get('PS_SHOP_EMAIL'), null, null, null, null, null, $path, false, Context::getContext()->shop->id); // echo 'ID ' . $abncart['id_cart']; } } } }
public function sendCampaign() { // get abandoned cart : $sql = "SELECT * FROM (\n\t\tSELECT\n\t\tCONCAT(LEFT(c.`firstname`, 1), '. ', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, c.id_customer, a.id_cart, a.date_upd,a.date_add,\n\t\t\t\tIF (IFNULL(o.id_order, 'Non ordered') = 'Non ordered', IF(TIME_TO_SEC(TIMEDIFF('" . date('Y-m-d H:i:s') . "', a.`date_add`)) > 86000, 'Abandoned cart', 'Non ordered'), o.id_order) id_order, IF(o.id_order, 1, 0) badge_success, IF(o.id_order, 0, 1) badge_danger, IF(co.id_guest, 1, 0) id_guest\n\t\tFROM `" . _DB_PREFIX_ . "cart` a \n\t\t\t\tJOIN `" . _DB_PREFIX_ . "customer` c ON (c.id_customer = a.id_customer)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "currency` cu ON (cu.id_currency = a.id_currency)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "carrier` ca ON (ca.id_carrier = a.id_carrier)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "orders` o ON (o.id_cart = a.id_cart)\n\t\t\t\tLEFT JOIN `" . _DB_PREFIX_ . "connections` co ON (a.id_guest = co.id_guest AND TIME_TO_SEC(TIMEDIFF('" . date('Y-m-d H:i:s') . "', co.`date_add`)) < 1800)\n\t\t\t\tWHERE a.date_add > (NOW() - INTERVAL 60 DAY) ORDER BY a.id_cart DESC \n\t\t) AS toto WHERE id_order='Abandoned cart'"; $currency = Context::getContext()->currency->sign; $defaultLanguage = new Language((int) Configuration::get('PS_LANG_DEFAULT')); $abandoned_carts = Db::getInstance()->ExecuteS($sql); // get all available campaigns $sqlCampaigns = 'SELECT * FROM `' . _DB_PREFIX_ . 'campaign` WHERE active=1'; $allCampaigns = Db::getInstance()->ExecuteS($sqlCampaigns); if (!$allCampaigns || empty($allCampaigns)) { die('NO CAMPAIGN'); } // loop on all abandoned carts foreach ($abandoned_carts as $abncart) { if (Cart::getNbProducts((int) $abncart['id_cart']) > 0) { $emailsSent = 0; // loop on all available campaigns foreach ($allCampaigns as $camp) { if (DEBUG_SAC) { echo 'IdCustomer : ' . $abncart['id_customer'] . ' - IdCart : ' . $abncart['id_cart'] . '<br/>'; } $cartIsOnCampaign = $this->checkIfCartIsOnCampaign($abncart['date_add'], $camp['execution_time_day'], $camp['execution_time_hour']); if ($cartIsOnCampaign) { if (DEBUG_SAC) { echo 'Cart on campaign</br>'; } $id_lang = (int) Configuration::get('PS_LANG_DEFAULT'); $customer = new Customer($abncart['id_customer']); $cart = new Cart($abncart['id_cart']); $products = $cart->getProducts(); $tpl_vars = array('{firstname}' => $customer->firstname, '{lastname}' => $customer->lastname, '{campaign_name}' => $camp['name'], '{track_url}' => $this->getBaseURL() . '?id_cart=' . (int) $abncart['id_cart'] . '&id_customer=' . (int) $abncart['id_customer'], '{track_request}' => '?id_cart=' . (int) $abncart['id_cart'] . '&id_customer=' . (int) $abncart['id_customer']); $campM = new Campaign($camp['id_campaign']); if ($campM->voucher_amount && $campM->voucher_day && $campM->voucher_amount_type) { $campM->clean_old_reduction($campM->voucher_prefix); $customerVoucher = $campM->registerDiscount($customer->id, $campM->voucher_amount, $campM->voucher_day, $campM->voucher_amount_type, $campM->voucher_prefix); $tpl_vars['{coupon_name}'] = $customerVoucher->name; $tpl_vars['{coupon_code}'] = $customerVoucher->code; $tpl_vars['{coupon_value}'] = $camp['voucher_amount_type'] == 'percent' ? $customerVoucher->reduction_percent . '%' : Tools::displayprice($customerVoucher->reduction_amount); $tpl_vars['{coupon_valid_to}'] = date('d/m/Y', strtotime($customerVoucher->date_to)); } if (!empty($products)) { $cart_content = $campM->getCartContentHeader(); } else { $cart_content = ''; } foreach ($products as $prod) { $p = new Product($prod['id_product'], true, $id_lang); $price_no_tax = Product::getPriceStatic($p->id, false, null, 2, null, false, true, 1, false, null, $abncart['id_cart'], null, $null, true, true, null, false, false); $total_no_tax = $prod['cart_quantity'] * $price_no_tax; $images = Image::getImages((int) $id_lang, (int) $p->id); $link = new Link(); $cart_content .= '<tr> <td align="center" ><img src="' . Tools::getShopProtocol() . $link->getImageLink($p->link_rewrite, $images[0]['id_image']) . '" width="80"/></td> <td align="center" ><a href="' . $link->getProductLink($p) . '?id_cart=' . (int) $abncart['id_cart'] . '&id_customer=' . (int) $abncart['id_customer'] . '"/>' . $p->name . '</a></td> <td align="center" >' . Tools::displayprice($price_no_tax) . '</td> <td align="center" >' . $prod['cart_quantity'] . '</td> <td align="center" >' . Tools::displayprice($total_no_tax) . '</td> </tr>'; } $cart_content .= '</table>'; $tpl_vars['{cart_content}'] = $cart_content; $path = _PS_ROOT_DIR_ . '/modules/superabandonedcart/mails/'; // send email to customer : $mailUser = Mail::Send($id_lang, $campM->getFileName(), $camp['name'], $tpl_vars, $customer->email, null, null, null, null, null, $path, false, Context::getContext()->shop->id); // if mail user is successfully sent : if ($mailUser) { $history = new CampaignHistory(); $history->id_campaign = (int) $camp['id_campaign']; $history->id_customer = $abncart['id_customer']; $history->id_cart = $abncart['id_cart']; $history->id_cart_rule = isset($customerVoucher->id) ? $customerVoucher->id : 0; $history->click = 0; $history->converted = 0; $history->date_update = date('Y-m-d H:i:s', time()); $history->save(); // Email to admin : Mail::Send($id_lang, $campM->getFileName(), Mail::l(sprintf('Email sent to %s %s for campaign %s', $customer->lastname, $customer->firstname, $camp['name'])), $tpl_vars, Configuration::get('PS_SHOP_EMAIL'), null, null, null, null, null, $path, false, Context::getContext()->shop->id); ++$emailsSent; } else { PrestaShopLogger::addLog('Error when sending user email (tpl:' . $campM->getFileName() . ',customer:' . $customer->email . ', campagne : ' . $camp['name'], 3); } } } // log emailing results : if ($emailsSent > 0) { PrestaShopLogger::addLog($emailsSent . ' emails sent for ' . $camp['name'] . ' campaign', 1); } } } }
public function postProcess() { //ADD if (Tools::isSubmit('submitAddcampaign')) { parent::validateRules(); if (count($this->errors)) { return false; } // ADD WAY if (!($id_campaign = (int) Tools::getValue('id_campaign')) && empty($this->errors)) { $defaultLanguage = new Language((int) Configuration::get('PS_LANG_DEFAULT')); // Include voucher : // Create campaign : $campaign = new Campaign(); $campaign->name = Tools::getValue('name'); $campaign->email_tpl = Tools::getValue('email_tpl'); $campaign->execution_time_day = Tools::getValue('execution_time_day'); $campaign->execution_time_hour = Tools::getValue('execution_time_hour'); $campaign->voucher_prefix = Tools::getValue('voucher_prefix'); $campaign->voucher_amount = Tools::getValue('voucher_amount'); $campaign->voucher_amount_type = Tools::getValue('voucher_amount_type'); $campaign->voucher_day = Tools::getValue('voucher_day'); $campaign->active = Tools::getValue('active'); // Create email files : $path = _PS_ROOT_DIR_ . '/modules/superabandonedcart/mails/' . $defaultLanguage->iso_code . '/'; if (!file_exists($path)) { if (!mkdir($path, 0777, true)) { $this->errors[] = Tools::displayError('Mails directory could not be created. Please check system permissions'); } } $tpl_file_name = $campaign->getFileName('html'); // create html files $f = fopen($path . $tpl_file_name, 'w'); fwrite($f, $campaign->email_tpl); fwrite($f, PHP_EOL); fclose($f); $tpl_file_name = $campaign->getFileName('txt'); // create txt files $f = fopen($path . $tpl_file_name, 'w'); fwrite($f, strip_tags($campaign->email_tpl)); fwrite($f, PHP_EOL); fclose($f); if (!$campaign->save()) { $this->errors[] = Tools::displayError('An error has occurred: Can\'t save the current object'); } // UPDATE WAY } elseif ($id_campaign = Tools::getValue('id_campaign')) { $defaultLanguage = new Language((int) Configuration::get('PS_LANG_DEFAULT')); // Create campaign : $campaign = new Campaign($id_campaign); $campaign->name = Tools::getValue('name'); $campaign->email_tpl = Tools::getValue('email_tpl'); $campaign->execution_time_day = Tools::getValue('execution_time_day'); $campaign->execution_time_hour = Tools::getValue('execution_time_hour'); $campaign->voucher_prefix = Tools::getValue('voucher_prefix'); $campaign->voucher_amount = Tools::getValue('voucher_amount'); $campaign->voucher_amount_type = Tools::getValue('voucher_amount_type'); $campaign->voucher_day = Tools::getValue('voucher_day'); $campaign->active = Tools::getValue('active'); $path = _PS_ROOT_DIR_ . '/modules/superabandonedcart/mails/' . $defaultLanguage->iso_code . '/'; if (!file_exists($path)) { if (!mkdir($path, 0777, true)) { $this->errors[] = Tools::displayError('Mails directory could not be created. Please check system permissions'); } } $tpl_file_name = $campaign->getFileName('html'); // create html files $f = fopen($path . $tpl_file_name, 'w'); fwrite($f, $campaign->email_tpl); fwrite($f, PHP_EOL); fclose($f); $tpl_file_name = $campaign->getFileName('txt'); // create txt files $f = fopen($path . $tpl_file_name, 'w'); fwrite($f, strip_tags($campaign->email_tpl)); fwrite($f, PHP_EOL); fclose($f); if (!$campaign->save()) { $this->errors[] = Tools::displayError('An error has occurred: Can\'t save the current object'); } } } elseif (Tools::isSubmit('statuscampaign') && Tools::getValue($this->identifier)) { if ($this->tabAccess['edit'] === '1') { if (Validate::isLoadedObject($object = $this->loadObject())) { if ($object->toggleStatus()) { $identifier = (int) $object->id_parent ? '&id_campaign=' . (int) $object->id_parent : ''; Tools::redirectAdmin($this->context->link->getAdminLink('AdminSuperAbandonedCart')); } else { $this->errors[] = Tools::displayError('An error occurred while updating the status.'); } } else { $this->errors[] = Tools::displayError('An error occurred while updating the status for an object.') . ' <b>' . $this->table . '</b> ' . Tools::displayError('(cannot load object)'); } } else { $this->errors[] = Tools::displayError('You do not have permission to edit this.'); } } elseif (Tools::getIsset('deletecampaign') && Tools::getValue($this->identifier)) { $id_campaign = (int) Tools::getValue($this->identifier); $b = new Campaign($id_campaign); $b->delete(); unset($b); } elseif (Tools::getIsset('submitBulkenableSelectioncampaign') && Tools::getValue('campaignBox')) { $ids_banner_deleted = Tools::getValue('campaignBox'); // remove each banner foreach ($ids_banner_deleted as $id) { $b = new Campaign($id); $b->toggleStatus(); unset($b); } } elseif (Tools::getIsset('submitBulkdisableSelectioncampaign') && Tools::getValue('campaignBox')) { $ids_banner_deleted = Tools::getValue('campaignBox'); // remove each banner foreach ($ids_banner_deleted as $id) { $b = new Campaign($id); $b->toggleStatus(); unset($b); } } }