簡體   English   中英

在Checkout上使用Opencart 2.1.0.1中的cURL發送短信

[英]Send SMS using cURL in Opencart 2.1.0.1 on Checkout

我需要了解如何在客戶成功完成Opencart 2.1.0.1中的結賬時向客戶發送通知(結賬/成功)

我在網上閱讀了一些論壇和開發人員帖子,並相應地對我的catalog / controller / checkout / success.php文件進行了更改

 class ControllerCheckoutSuccess extends Controller { public function index() { $this->load->language('checkout/success'); if (isset($this->session->data['order_id'])) { 

我在這些行之間添加的代碼

            $this->load->model('account/customer');
$cnum = $this->customer->gettelephone(); // get customer mobile number
$getorderid = $this->session->data['order_id']; // get order id


            //URL to shorten
$track_url = "https://www.example.com/index.php?route=account/order/info&order_id=".$getorderid; 

/////////////////////////////////


// EDIT THIS: your auth parameters
$signature = '4a6fe0b26c';

// EDIT THIS: the query parameters
$keyword = '';                        // optional keyword
$title   = '';                // optional, if omitted YOURLS will lookup title with an HTTP request
$format  = 'json';                       // output format: 'json', 'xml' or 'simple'

// EDIT THIS: the URL of the API file
$api_url = 'http://abc.abc/yourls-api.php';

// Init the CURL session
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
        'url'      => $track_url,
        'keyword'  => $keyword,
        'title'    => $title,
        'format'   => $format,
        'action'   => 'shorturl',
        'signature' => $signature,
    ) );

// Fetch and return content
$data = curl_exec($ch);
$json = json_decode($data);
$order_track_url = $json->shorturl;
curl_close($ch);

            $msg = "Your order $getorderid has been successfully placed and will be shipped once verified. Shipping is expected in 48 hours and Delivery in 5-6 days
            Track your order $order_track_url";
            $encode= rawurlencode($msg);
//request parameters array
$requestParams = array(
    'channel' => '2',
    'APIKey' => 'gatewaykey',
    'senderid' => 'ABCDEF',
    'Number' => $cnum,
    'Text' => $encode,
    'DCS' => '0',
    'Route' => '1',
    'Flashsms' => '0',
);

//merge API url and parameters
$apiUrl = "https://login.smscompanyapilink.com/api/mt/SendSMS?";
foreach($requestParams as $key => $val){
    $apiUrl .= $key.'='.($val).'&';
}
$apiUrl = rtrim($apiUrl, "&");

//API call
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$apiUrl);
 curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,0);

$data = curl_exec($ch);
if (curl_errno($ch) > 0) {
    print 'There was a cURL error: ' . $curl_error($ch);
}

$json = json_decode($data);
$JobID = $json->{'JobId'};
curl_close($ch);

$this->cart->clear();

新訂單目錄/型號/結帳/訂單.php上的客戶提醒電子郵件代碼

// If order status is 0 then becomes greater than 0 send main html email
        if (!$order_info['order_status_id'] && $order_status_id) {
            // Check for any downloadable products
            $download_status = false;

            $order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$order_id . "'");

            foreach ($order_product_query->rows as $order_product) {
                // Check if there are any linked downloads
                $product_download_query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "product_to_download` WHERE product_id = '" . (int)$order_product['product_id'] . "'");

                if ($product_download_query->row['total']) {
                    $download_status = true;
                }
            }

            // Load the language for any mails that might be required to be sent out
            $language = new Language($order_info['language_directory']);
            $language->load($order_info['language_directory']);
            $language->load('mail/order');

            $order_status_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_status WHERE order_status_id = '" . (int)$order_status_id . "' AND language_id = '" . (int)$order_info['language_id'] . "'");

            if ($order_status_query->num_rows) {
                $order_status = $order_status_query->row['name'];
            } else {
                $order_status = '';
            }

            $subject = sprintf($language->get('text_new_subject'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'), $order_id);

            // HTML Mail
            $data = array();

            $data['title'] = sprintf($language->get('text_new_subject'), $order_info['store_name'], $order_id);

            $data['text_greeting'] = sprintf($language->get('text_new_greeting'), $order_info['store_name']);
            $data['text_link'] = $language->get('text_new_link');
            $data['text_download'] = $language->get('text_new_download');
            $data['text_order_detail'] = $language->get('text_new_order_detail');
            $data['text_instruction'] = $language->get('text_new_instruction');
            $data['text_order_id'] = $language->get('text_new_order_id');
            $data['text_date_added'] = $language->get('text_new_date_added');
            $data['text_payment_method'] = $language->get('text_new_payment_method');
            $data['text_shipping_method'] = $language->get('text_new_shipping_method');
            $data['text_email'] = $language->get('text_new_email');
            $data['text_telephone'] = $language->get('text_new_telephone');
            $data['text_ip'] = $language->get('text_new_ip');
            $data['text_order_status'] = $language->get('text_new_order_status');
            $data['text_payment_address'] = $language->get('text_new_payment_address');
            $data['text_shipping_address'] = $language->get('text_new_shipping_address');
            $data['text_product'] = $language->get('text_new_product');
            $data['text_model'] = $language->get('text_new_model');
            $data['text_quantity'] = $language->get('text_new_quantity');
            $data['text_price'] = $language->get('text_new_price');
            $data['text_total'] = $language->get('text_new_total');
            $data['text_footer'] = $language->get('text_new_footer');

            $data['logo'] = $this->config->get('config_url') . 'image/' . $this->config->get('config_logo');
            $data['store_name'] = $order_info['store_name'];
            $data['store_url'] = $order_info['store_url'];
            $data['customer_id'] = $order_info['customer_id'];
            $data['link'] = $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id;

            if ($download_status) {
                $data['download'] = $order_info['store_url'] . 'index.php?route=account/download';
            } else {
                $data['download'] = '';
            }

            $data['order_id'] = $order_id;
            $data['date_added'] = date($language->get('date_format_short'), strtotime($order_info['date_added']));
            $data['payment_method'] = $order_info['payment_method'];
            $data['shipping_method'] = $order_info['shipping_method'];
            $data['email'] = $order_info['email'];
            $data['telephone'] = $order_info['telephone'];
            $data['ip'] = $order_info['ip'];
            $data['order_status'] = $order_status;

            if ($comment && $notify) {
                $data['comment'] = nl2br($comment);
            } else {
                $data['comment'] = '';
            }

            if ($order_info['payment_address_format']) {
                $format = $order_info['payment_address_format'];
            } else {
                $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
            }

            $find = array(
                '{firstname}',
                '{lastname}',
                '{company}',
                '{address_1}',
                '{address_2}',
                '{city}',
                '{postcode}',
                '{zone}',
                '{zone_code}',
                '{country}'
            );

            $replace = array(
                'firstname' => $order_info['payment_firstname'],
                'lastname'  => $order_info['payment_lastname'],
                'company'   => $order_info['payment_company'],
                'address_1' => $order_info['payment_address_1'],
                'address_2' => $order_info['payment_address_2'],
                'city'      => $order_info['payment_city'],
                'postcode'  => $order_info['payment_postcode'],
                'zone'      => $order_info['payment_zone'],
                'zone_code' => $order_info['payment_zone_code'],
                'country'   => $order_info['payment_country']
            );

            $data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

            if ($order_info['shipping_address_format']) {
                $format = $order_info['shipping_address_format'];
            } else {
                $format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
            }

            $find = array(
                '{firstname}',
                '{lastname}',
                '{company}',
                '{address_1}',
                '{address_2}',
                '{city}',
                '{postcode}',
                '{zone}',
                '{zone_code}',
                '{country}'
            );

            $replace = array(
                'firstname' => $order_info['shipping_firstname'],
                'lastname'  => $order_info['shipping_lastname'],
                'company'   => $order_info['shipping_company'],
                'address_1' => $order_info['shipping_address_1'],
                'address_2' => $order_info['shipping_address_2'],
                'city'      => $order_info['shipping_city'],
                'postcode'  => $order_info['shipping_postcode'],
                'zone'      => $order_info['shipping_zone'],
                'zone_code' => $order_info['shipping_zone_code'],
                'country'   => $order_info['shipping_country']
            );

            $data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

            $this->load->model('tool/upload');

            // Products
            $data['products'] = array();

            foreach ($order_product_query->rows as $product) {
                $option_data = array();

                $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . (int)$product['order_product_id'] . "'");

                foreach ($order_option_query->rows as $option) {
                    if ($option['type'] != 'file') {
                        $value = $option['value'];
                    } else {
                        $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

                        if ($upload_info) {
                            $value = $upload_info['name'];
                        } else {
                            $value = '';
                        }
                    }

                    $option_data[] = array(
                        'name'  => $option['name'],
                        'value' => (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value)
                    );
                }

                $data['products'][] = array(
                    'name'     => $product['name'],
                    'model'    => $product['model'],
                    'option'   => $option_data,
                    'quantity' => $product['quantity'],
                    'price'    => $this->currency->format($product['price'] + ($this->config->get('config_tax') ? $product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value']),
                    'total'    => $this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value'])
                );
            }

            // Vouchers
            $data['vouchers'] = array();

            $order_voucher_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_voucher WHERE order_id = '" . (int)$order_id . "'");

            foreach ($order_voucher_query->rows as $voucher) {
                $data['vouchers'][] = array(
                    'description' => $voucher['description'],
                    'amount'      => $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']),
                );
            }

            // Order Totals
            $order_total_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_total` WHERE order_id = '" . (int)$order_id . "' ORDER BY sort_order ASC");

            foreach ($order_total_query->rows as $total) {
                $data['totals'][] = array(
                    'title' => $total['title'],
                    'text'  => $this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']),
                );
            }

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/order.tpl')) {
                $html = $this->load->view($this->config->get('config_template') . '/template/mail/order.tpl', $data);
            } else {
                $html = $this->load->view('default/template/mail/order.tpl', $data);
            }

            // Text Mail
            $text  = sprintf($language->get('text_new_greeting'), html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')) . "\n\n";
            $text .= $language->get('text_new_order_id') . ' ' . $order_id . "\n";
            $text .= $language->get('text_new_date_added') . ' ' . date($language->get('date_format_short'), strtotime($order_info['date_added'])) . "\n";
            $text .= $language->get('text_new_order_status') . ' ' . $order_status . "\n\n";

            if ($comment && $notify) {
                $text .= $language->get('text_new_instruction') . "\n\n";
                $text .= $comment . "\n\n";
            }

            // Products
            $text .= $language->get('text_new_products') . "\n";

            foreach ($order_product_query->rows as $product) {
                $text .= $product['quantity'] . 'x ' . $product['name'] . ' (' . $product['model'] . ') ' . html_entity_decode($this->currency->format($product['total'] + ($this->config->get('config_tax') ? ($product['tax'] * $product['quantity']) : 0), $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";

                $order_option_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_option WHERE order_id = '" . (int)$order_id . "' AND order_product_id = '" . $product['order_product_id'] . "'");

                foreach ($order_option_query->rows as $option) {
                    if ($option['type'] != 'file') {
                        $value = $option['value'];
                    } else {
                        $upload_info = $this->model_tool_upload->getUploadByCode($option['value']);

                        if ($upload_info) {
                            $value = $upload_info['name'];
                        } else {
                            $value = '';
                        }
                    }

                    $text .= chr(9) . '-' . $option['name'] . ' ' . (utf8_strlen($value) > 20 ? utf8_substr($value, 0, 20) . '..' : $value) . "\n";
                }
            }

            foreach ($order_voucher_query->rows as $voucher) {
                $text .= '1x ' . $voucher['description'] . ' ' . $this->currency->format($voucher['amount'], $order_info['currency_code'], $order_info['currency_value']);
            }

            $text .= "\n";

            $text .= $language->get('text_new_order_total') . "\n";

            foreach ($order_total_query->rows as $total) {
                $text .= $total['title'] . ': ' . html_entity_decode($this->currency->format($total['value'], $order_info['currency_code'], $order_info['currency_value']), ENT_NOQUOTES, 'UTF-8') . "\n";
            }

            $text .= "\n";

            if ($order_info['customer_id']) {
                $text .= $language->get('text_new_link') . "\n";
                $text .= $order_info['store_url'] . 'index.php?route=account/order/info&order_id=' . $order_id . "\n\n";
            }

            if ($download_status) {
                $text .= $language->get('text_new_download') . "\n";
                $text .= $order_info['store_url'] . 'index.php?route=account/download' . "\n\n";
            }

            // Comment
            if ($order_info['comment']) {
                $text .= $language->get('text_new_comment') . "\n\n";
                $text .= $order_info['comment'] . "\n\n";
            }

            $text .= $language->get('text_new_footer') . "\n\n";

            $mail = new Mail();
            $mail->protocol = $this->config->get('config_mail_protocol');
            $mail->parameter = $this->config->get('config_mail_parameter');
            $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
            $mail->smtp_username = $this->config->get('config_mail_smtp_username');
            $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
            $mail->smtp_port = $this->config->get('config_mail_smtp_port');
            $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');

            $mail->setTo($order_info['email']);
            $mail->setFrom($this->config->get('config_email'));
            $mail->setSender(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8'));
            $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
            $mail->setHtml($html);
            $mail->setText($text);
            $mail->send();

>管理員提醒郵件代碼

我的短信代碼


                                 $this->load->model('account/customer');
$cnum = $data['text_telephone'] = $language->get('text_new_telephone');
$getorderid = $data['text_order_id'] = $language->get('text_new_order_id');


//////   URL to shorten
$track_url = $order_info['store_url'] . 'index.php?route=account/order/info&order_id='. $order_id 

/////////////////////////////////


// EDIT THIS: your auth parameters
$signature = 'e4635e2730';

// EDIT THIS: the query parameters
$keyword = '';                        // optional keyword
$title   = '';                // optional, if omitted YOURLS will lookup title with an HTTP request
$format  = 'json';                       // output format: 'json', 'xml' or 'simple'

// EDIT THIS: the URL of the API file
$api_url = 'http://apna.co/yourls-api.php';

// Init the CURL session
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $api_url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );            // No header in the result
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Return, do not echo result
curl_setopt( $ch, CURLOPT_POST, 1 );              // This is a POST request
curl_setopt( $ch, CURLOPT_POSTFIELDS, array(      // Data to POST
        'url'      => $track_url,
        'keyword'  => $keyword,
        'title'    => $title,
        'format'   => $format,
        'action'   => 'shorturl',
        'signature' => $signature,
    ) );

// Fetch and return content
$data = curl_exec($ch);
$json = json_decode($data);
$order_track_url = $json->shorturl;
curl_close($ch);

            $msg = "Your order ".$getorderid." has been successfully placed and will be shipped once verified. Shipping is expected in 48 hours and Delivery in 5-6 days.
            Track your order ".$order_track_url;
            $encode= rawurlencode($msg);
//request parameters array
$requestParams = array(
    'channel' => '2',
    'APIKey' => 'J8yAjtZkTUqMnDy6SDQN6Q',
    'senderid' => 'APNAPR',
    'Number' => $cnum,
    'Text' => $encode,
    'DCS' => '0',
    'Route' => '1',
    'Flashsms' => '0',
);

//merge API url and parameters
$apiUrl = 'https://login.smscompanyapilink.com/api/mt/SendSMS?';
foreach($requestParams as $key => $val){
    $apiUrl .= $key.'='.($val).'&';
}
$apiUrl = rtrim($apiUrl, "&");

//API call
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$apiUrl);
 curl_setopt($ch, CURLOPT_HEADER,0);
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_TIMEOUT,60);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_POSTFIELDS,0);

$data = curl_exec($ch);
if (curl_errno($ch) > 0) {
    print 'There was a cURL error: ' . $curl_error($ch);
}

$json = json_decode($data);
$JobID = $json->{'JobId'};
curl_close($ch);

緊隨其后的是閉合式大括號

URL Shorten-er和SMS在所有其他代碼中都像魅力一樣,並經過全面測試。

這是錯誤的文件嗎? 我需要修改一個或多個文件嗎?

請提前謝謝! :)

如果您想在訂購成功后向客戶發送短信,則需要在此處集成您的代碼catalog/model/checkout/order.php 在訂單模型中,您將找到addOrderHistory()方法。 按方法名稱搜索。 在此方法結束時,您將找到向客戶和管理員發送郵件的代碼。 所以在這里集成你的短信代碼。

示例代碼

public function addOrderHistory($order_id, $order_status_id, $comment = '', $notify = false, $override = false) {
....
....
// default code
....
....

// your code to send sms
$this->load->model('account/customer');
$cnum = $this->customer->gettelephone(); // get customer mobile number

//URL to shorten
$track_url = "https://www.example.com/index.php?route=account/order/info&order_id=".$order_id; 

// EDIT THIS: your auth parameters
$signature = '4a6fe0b26c';
....
....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM