Version: current

     1 <?php
     2 // Уведомление клиента по емайл (создание заказа и изменение заказа )
     3 class ClientEmailOrderNotification extends OrderHandlersDecorator {
     4
     5     function OnAfterCreateOrder(& $member) {
     6         $conf = SiteConfig::current_site_config();
     7
     8         $email = $this->getClientEmail();
     9         if ($conf->ClientEmailOrderNotificationActive && $email) {
    10             $mail = new Email();
    11             $mail->setFrom($conf->CartSenderEmail());
    12             $mail->setSubject($this->owner->fill_vars($conf->CartUserNewOrderSubject($this->owner->ID)));
    13             $mail->setTo($email);
    14             $mail->setTemplate('Order_UserReceiptEmail');
    15             $mail->populateTemplate(array(
    16                 'PurchaseCompleteMessage' => DataObject::get_one('CheckoutPage')->PurchaseComplete,
    17                 'Order' => $this->owner,
    18                 'SiteConfig' => $conf,
    19             ));
    20             $mail->send();
    21         }
    22     }
    23
    24     // после смены стутса
    25     function OnAfterChangeStatus($member, $changedStatus, $note) {
    26         $conf = SiteConfig::current_site_config();
    27         if (!$conf->ClientEmailOrderNotificationActive) return false;
    28
    29         $email = $this->getClientEmail();
    30         if (!$email) return false;
    31
    32         if (!$note) {
    33             $log = DataObject::get_one('Order_StatusLog', "OrderID = {$this->owner->ID}", "Created DESC");
    34             $note = $log->Note;
    35         }
    36
    37         //если отложенная оплата и заказ прошел проверку (статус поменялся с Query на Unpaid), то добавляем в письмо о смене статуса ссылку на оплату
    38         $paymentLink = false;
    39         if (Order::$use_payments && PaymentSiteConfig::getPaymentAfterConfirm() && $this->owner->isChanged('Status')) {
    40             $changes = $this->owner->getChangedFields();
    41             $statusData = $changes['Status'];
    42             if ($statusData['before'] == 'Unpaid' && $statusData['after'] == 'Query') { //если заказ прошел проверку
    43                 $paymentLink = $this->owner->PaymentLink();
    44             }
    45         }
    46
    47         $mail = new Email();
    48         $mail->setFrom($conf->CartSenderEmail());
    49         $mail->setTo($email);
    50         $mail->setSubject($this->owner->fill_vars($conf->CartUserStatusSubject($this->owner->ID)));
    51         $mail->setTemplate('Order_UserStatusEmail');
    52
    53         $mail->populateTemplate(array(
    54             'Order' => $this->owner,
    55             'Note' => $note,
    56             'Link' => $paymentLink,
    57             'SiteConfig' => $conf,
    58         ));
    59         $mail->send();
    60     }
    61
    62     /**
    63      * Ищем email клиента
    64      *
    65      * @return string
    66      */
    67     private function getClientEmail() {
    68         $email = $this->owner->Email;
    69         if (!$email) {
    70             if ($member = $this->owner->Member()) {
    71                 $email = $member->Email;
    72             }
    73         }
    74         return $email;
    75     }
    76
    77 }
    78
    79 // Декоратор SiteConfig для этого типа уведомлений
    80 class ClientEmailOrderNotification_SiteConfig extends SiteConfigDecorator {
    81     function extraStatics() {
    82         return array(
    83             'db' => array(
    84                 'ClientEmailOrderNotificationActive' => 'Boolean', // активность
    85             )
    86         );
    87     }
    88     public function updateCMSFields(FieldSet &$fields) {
    89         $tab = self::get_config_tab($fields, 'OrderNotifications.EmailOrderNotification');
    90         $tab->push(new CheckboxField('ClientEmailOrderNotificationActive', $this->owner->FieldLabel('ClientEmailOrderNotificationActive')));
    91     }
    92 }
    93

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -