// Если нажали оформить заказ
if(isset($_POST['checkout']))
{
$order = new stdClass;
$order->delivery_id = $this->request->post('delivery_id', 'integer');
$order->name = $this->request->post('name');
$order->email = $this->request->post('email');
$order->address = $this->request->post('address');
$order->phone = $this->request->post('phone');
$order->comment = $this->request->post('comment');
$order->ip = $_SERVER['REMOTE_ADDR'];
$this->design->assign('delivery_id', $order->delivery_id);
$this->design->assign('name', $order->name);
$this->design->assign('email', $order->email);
$this->design->assign('phone', $order->phone);
$this->design->assign('address', $order->address);
$captcha_code = $this->request->post('captcha_code', 'string');
// Скидка
$cart = $this->cart->get_cart();
$order->discount = $cart->discount;
if($cart->coupon)
{
$order->coupon_discount = $cart->coupon_discount;
$order->coupon_code = $cart->coupon->code;
}
//
if(!empty($this->user->id))
$order->user_id = $this->user->id;
if(empty($order->name))
{
$this->design->assign('error', 'empty_name');
$json['error'] = 'Заполните поле: Имя';
echo json_encode($json);
}
elseif(empty($order->email))
{
$this->design->assign('error', 'empty_email');
$json['error'] = 'Заполните поле: email';
echo json_encode($json);
}
elseif($_SESSION['captcha_code'] != $captcha_code || empty($captcha_code))
{
$this->design->assign('error', 'captcha');
$json['error'] = 'Не верно введена captcha';
echo json_encode($json);
}
else
{
// Добавляем заказ в базу
$order_id = $this->orders->add_order($order);
$_SESSION['order_id'] = $order_id;
// Если использовали купон, увеличим количество его использований
if($cart->coupon)
$this->coupons->update_coupon($cart->coupon->id, array('usages'=>$cart->coupon->usages+1));
// Добавляем товары к заказу
foreach($this->request->post('amounts') as $variant_id=>$amount)
{
$this->orders->add_purchase(array('order_id'=>$order_id, 'variant_id'=>intval($variant_id), 'amount'=>intval($amount)));
}
$order = $this->orders->get_order($order_id);
// Стоимость доставки
$delivery = $this->delivery->get_delivery($order->delivery_id);
if(!empty($delivery) && $delivery->free_from > $order->total_price)
{
$this->orders->update_order($order->id, array('delivery_price'=>$delivery->price, 'separate_delivery'=>$delivery->separate_payment));
}
// Отправляем письмо пользователю
$this->notify->email_order_user($order->id);
// Отправляем письмо администратору
$this->notify->email_order_admin($order->id);
// Очищаем корзину (сессию)
$this->cart->empty_cart();
// Перенаправляем на страницу заказа
header('Location: '.$this->config->root_url.'/order/'.$order->url);
}