Skip to content

Instantly share code, notes, and snippets.

@argnist
Created July 4, 2013 10:22
Show Gist options
  • Save argnist/5926589 to your computer and use it in GitHub Desktop.
Save argnist/5926589 to your computer and use it in GitHub Desktop.
Minishop2 OrderHandler с привязкой к телефону вместо емейла
<?php
class msOrderByPhoneHandler extends msOrderHandler {
public function getCustomerId() {
$order = $this->ms2->order->get();
if (empty($order['phone'])) {return false;}
if ($this->modx->user->isAuthenticated()) {
$profile = $this->modx->user->Profile;
if (!$phone = $profile->get('phone')) {
$profile->set('phone', $order['phone']);
$profile->save();
}
$uid = $this->modx->user->id;
}
else {
/* @var modUser $user */
$phone = $order['phone'];
if ($user = $this->modx->getObject('modUser', array('username' => $phone))) {
$uid = $user->get('id');
}
else {
$user = $this->modx->newObject('modUser', array('username' => $phone, 'password' => md5(rand())));
$profile = $this->modx->newObject('modUserProfile', array('phone' => $phone, 'fullname' => $order['receiver']));
$user->addOne($profile);
$user->save();
if ($groups = $this->modx->getOption('ms2_order_user_groups', null, false)) {
$groups = array_map('trim', explode(',', $groups));
foreach ($groups as $group) {
$user->joinGroup($group);
}
}
$uid = $user->get('id');
}
}
return $uid;
}
/* @inheritdoc} */
public function submit($data = array()) {
$this->modx->invokeEvent('msOnSubmitOrder', array('data' => & $data, 'order' => $this));
if (!empty($data)) {
$this->set($data);
}
/* @var msDelivery $delivery */
if (!$delivery = $this->modx->getObject('msDelivery', array('id' => $this->order['delivery'], 'active' => 1))) {
return $this->error('ms2_order_err_delivery', array('delivery'));
}
$requires = array_map('trim', explode(',',$delivery->get('requires')));
$errors = array();
foreach ($requires as $v) {
if (!empty($v) && empty($this->order[$v])) {
$errors[] = $v;
}
}
if (!empty($errors)) {
return $this->error('ms2_order_err_requires', $errors);
}
$user_id = $this->getCustomerId();
$cart_status = $this->ms2->cart->status();
$delivery_cost = $this->getcost(false, true);
$createdon = date('Y-m-d H:i:s');
/* @var msOrder $order */
$order = $this->modx->newObject('msOrder');
$order->fromArray(array(
'user_id' => $user_id
,'createdon' => $createdon
,'num' => $this->getnum()
,'delivery' => $this->order['delivery']
,'payment' => $this->order['payment']
,'cart_cost' => $cart_status['total_cost']
,'weight' => $cart_status['total_weight']
,'delivery_cost' => $delivery_cost
,'cost' => $cart_status['total_cost'] + $delivery_cost
,'status' => 0
,'context' => $this->ms2->config['ctx']
));
// Adding address
/* @var msOrderAddress $address */
$address = $this->modx->newObject('msOrderAddress');
$address->fromArray(array_merge($this->order,array(
'user_id' => $user_id
,'createdon' => $createdon
)));
$order->addOne($address);
// Adding products
$cart = $this->ms2->cart->get();
$products = array();
foreach ($cart as $v) {
/* @var msOrderProduct $product */
$product = $this->modx->newObject('msOrderProduct');
$product->fromArray(array_merge($v, array(
'product_id' => $v['id']
,'cost' => $v['price'] * $v['count']
)));
$products[] = $product;
}
$order->addMany($products);
$this->modx->invokeEvent('msOnBeforeCreateOrder', array('order' => $this));
if ($order->save()) {
$this->modx->invokeEvent('msOnCreateOrder', array('order' => $this));
$this->ms2->cart->clean();
$this->clean();
if (empty($_SESSION['minishop2']['orders'])) {
$_SESSION['minishop2']['orders'] = array();
}
$_SESSION['minishop2']['orders'][] = $order->get('id');
$this->ms2->changeOrderStatus($order->get('id'), 1); // set status "new"
/* @var msPayment $payment*/
if ($payment = $this->modx->getObject('msPayment', array('id' => $order->get('payment'), 'active' => 1))) {
$response = $payment->send($order);
exit(is_array($response) ? $this->modx->toJSON($response) : $response);
}
else {
return $this->success('', array('msorder' => $order->get('id')));
}
}
return $this->error();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment