Skip to content

Instantly share code, notes, and snippets.

@vitaly-zdanevich
Created May 6, 2016 17:38
Show Gist options
  • Save vitaly-zdanevich/524c5119c16904b279eb74efa8092e69 to your computer and use it in GitHub Desktop.
Save vitaly-zdanevich/524c5119c16904b279eb74efa8092e69 to your computer and use it in GitHub Desktop.
temp code example: sending email about order
<?php
class ControllerModuleCheckout extends Controller {
public function checkout() {
$products = json_decode(html_entity_decode($_POST['products'])); //create object from json string
$message = '<html><head></head><body><table>';
$priceTotalBYR = 0;
foreach($products as $product) :
$message .= '<tr>';
$product_id = $product->product_id;
$product_quantity = $product->quantity;
$product_warranty = $product->warranty;
$this->load->model('catalog/product');
$this->load->model('tool/image');
$product_db = $this->model_catalog_product->getProduct($product_id);
$product_name = $product_db['name'];
$url = $product_db['url_alias'];
$image = $this->model_tool_image->resize($product_db['image'], 300, 300);
$message .= '<td><a href="http://zvukovik.by/'.$url.'"><img src="'.$image.'"/></a></td>';
$message .= '<td><a href="http://zvukovik.by/'.$url.'">'.$product_name.'</a><br>';
$priceUSD = $product_db['price'];
$priceBYR = $this->currency->format($priceUSD); //result will be for example 425 000 BYR
$priceProductBYRNumbers = preg_replace('/[^0-9]/', '', $priceBYR);
if($product->quantity > 1 || $product->warranty == 'true' || count($products) > 1)
$message .= 'Цена за штуку: '.$this->currency->format($priceUSD).'<br>';
if($product->quantity > 1) $message .= ' Количество: '.$product->quantity.'<br>';
if($product->warranty == 'true') { // if we need to calc sum for one product
$message .= ($product->quantity == 1) ? '[С допгарантией на год за 150 000 byr]' : '[С допгарантией на год за 150 000 byr каждые]';
$priceProductTotal = $priceProductBYRNumbers * $product->quantity;
if($product->warranty == 1) $priceProductTotal += $product->quantity * 150000;
$priceTotalBYR += $priceProductTotal;
} else {
$priceTotalBYR += $priceProductBYRNumbers * $product->quantity;
}
if($product->quantity > 1)
$message .= '<br>Итого за этот продукт: '.number_format($priceProductTotal, 0, ' ', ' ').' byr';
$message .= '</td></tr>';
endforeach;
$message .= '<b>ИТОГО: '.number_format($priceTotalBYR, 0, ' ', ' ').' byr</b><br><br>';
$data = array();
$data['ip'] = $_SERVER['REMOTE_ADDR'];
$data['accept_language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$data['city'] = '';
if (isset($_POST['city']) && strlen($_POST['city'] ) > 1) :
$message .= 'Город: ' . $_POST['city'] . "<br>";
$data['city'] = $_POST['city'];;
endif;
if (isset($_POST['name']) && strlen($_POST['name'] ) > 1) :
$message .= 'Имя: ' . $_POST['name'] . "<br>";
$data['name'] = $_POST['name'];
endif;
if (isset($_POST['phone']) && strlen($_POST['phone'] ) > 1) :
$message .= 'Телефон: ' . $_POST['phone'] . "<br>";
$data['phone'] = $_POST['phone'];
endif;
if (isset($_POST['email']) && strlen($_POST['email'] ) > 1) :
$message .= 'email: ' . $_POST['email'] . "<br>";
$data['email'] = $_POST['email'];
endif;
if (isset($_POST['address']) && strlen($_POST['address']) > 1) $message .= 'Адрес: ' . $_POST['address'] . "<br>";
if (isset($_POST['comment']) && strlen($_POST['comment']) > 1) $message .= 'Комментарий: '. $_POST['comment'] . "<br>";
$message .= '</table></body></html>';
$message = wordwrap($message, 50);
mail('zvukovik.by@gmail.com', $_POST['subject'], $message, 'Content-Type: text/html');
$this->load->model('checkout/order');
$this->session->data['order_id'] = $this->model_checkout_order->addOrder($data);
// header('HTTP/1.1 500 Internal Server Error'); if something wrong but what?
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment