15天和45天脚本后发送邮件

时间:2018-10-31 10:26:20

标签: php prestashop

我实现了一个PHP脚本,它将每天在cron中运行,从而允许在订单通过15天后发送电子邮件。该脚本将发送第一封电子邮件,该电子邮件被注册在表格中以进行历史记录的电子邮件交易,然后在订购日期后45天将发送第二封电子邮件,并且该电子邮件也将被登录桌子。

我阻止在日期级别记录表中的第一封电子邮件,但是从未记录过第二封电子邮件。这是我的脚本:

public function cron()
{
    $state = Db::getInstance()->executeS('
        SELECT o.id_order, o.total_products_wt, o.total_paid_real, op.date_add, op.order_reference, c.id_customer, c.firstname, c.lastname, c.email, o.current_state, o.id_cart, o.date_add
        FROM ' . _DB_PREFIX_ . 'orders o
        LEFT JOIN ' . _DB_PREFIX_ . 'order_payment op ON (o.reference = op.order_reference)
        LEFT JOIN ' . _DB_PREFIX_ . 'customer c ON (o.id_customer = c.id_customer)
        WHERE o.current_state = ' . Configuration::get('BLOCKPAYMENT_STATE_1') . '
        OR o.current_state = ' . Configuration::get('BLOCKPAYMENT_STATE_2'));

    foreach ($state as $item) {
        $historyMail = $this->getHistory((int)$item['id_order']);

        $paiement = 0;

        // Days diff
        $datetime1 = new DateTime(date('Y-m-d'));
        $datetime2 = new DateTime($item['date_add']);
        $interval = $datetime1->diff($datetime2);
        $diff = $interval->format('%a');

        if ($historyMail == null && $diff == 15) {
            $paiement = 2;
        } elseif ($historyMail != null && $diff >= 45) {
            $paiement = 3;
        }

        /*dump($item);
        dump($paiement);
        dump($diff);*/

        if ((int)$item['current_state'] == (int)Configuration::get('BLOCKPAYMENT_STATE_1')) {
            $stateTitle = 'Paiement 2';
            $important = 'Vous recevrez sous quinzaine la dernière échèance de paiement par email.';
        } elseif ((int)$item['current_state'] == (int)Configuration::get('BLOCKPAYMENT_STATE_2')) {
            $stateTitle = 'Paiement 3';
            $important = '';
        } else {
            continue;
        }

        if ($this->is_ssl() != false) {
            $ssl = 'https://';
        } else {
            $ssl = 'http://';
        }

        $data = [
            '{state}' => $stateTitle,
            '{important}' => $important,
            '{firstname}' => $item['firstname'],
            '{lastname}' => $item['lastname'],
            '{reference}' => $item['order_reference'],
            '{date}' => $item['date_add'],
            '{link}' => $ssl . $_SERVER['HTTP_HOST'] . '/module/blockpayment/validation?id_cart=' . $item['id_cart']
        ];

        // Cas paiement 2: première relance
        if ($paiement == 2) {
            $history = new Historymailpayment();
            $history->id_order = (int)$item['id_order'];
            $history->id_customer = (int)$item['id_customer'];
            $history->type = $paiement;
            $history->date_send = date('Y-m-d H:i:s');

            $this->sendEmail($stateTitle, $data, $item['email'], $item['firstname'] . ' ' . $item['lastname'], 1);
            $history->save();
        } elseif ($paiement == 3) {
            $history = new Historymailpayment();
            $history->id_order = (int)$item['id_order'];
            $history->id_customer = (int)$item['id_customer'];
            $history->type = $paiement;
            $history->date_send = date('Y-m-d H:i:s');

            $this->sendEmail($stateTitle, $data, $item['email'], $item['firstname'] . ' ' . $item['lastname'], 1);
            $history->save();
        }
    }
}

状态“ BLOCKPAYMENT_STATE_1”对应于第一封电子邮件,状态“ BLOCKPAYMENT_STATE_2”对应于第二封电子邮件

我目前有3个命令:

1-在状态1下订购的16/10/18,应发送第一封电子邮件并将此交易保存在表中 2-在状态2于09/15/18订购,该状态应发送第二封电子邮件并在表中记录新行 3-于09年15月15日在状态2下订购,该状态应发送第二封电子邮件并在表格中记录新行

当我运行cron时,我的表仅添加一行

history

当我重新启动它时,没有添加其他行,仅发送了第一封邮件。

谢谢您的帮助

0 个答案:

没有答案