Skip to content

Instantly share code, notes, and snippets.

@vgrish
Created November 27, 2015 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrish/e89d4ad2b708bafeae3b to your computer and use it in GitHub Desktop.
Save vgrish/e89d4ad2b708bafeae3b to your computer and use it in GitHub Desktop.
<?php
switch ($modx->event->name) {
case 'OnWebPageInit':
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) AND $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
if (empty($_REQUEST['magic_action'])) {
return;
}
$ctx = !empty($_REQUEST['ctx']) ? (string)$_REQUEST['ctx'] : 'web';
if ($ctx != 'web') {
$modx->switchContext($ctx);
}
/* @var miniShop2 $miniShop2 */
$miniShop2 = $modx->getService('minishop2');
$miniShop2->initialize($ctx, array('json_response' => $isAjax));
if (!($miniShop2 instanceof miniShop2)) {
@session_write_close();
exit('Could not initialize miniShop2');
}
$cart = $miniShop2->cart;
$order = $miniShop2->order;
foreach ($_POST as $field => $value) {
$value = $order->validate($field, $value);
if (empty($value) OR (!$value)) {
$response = $miniShop2->error('Вы должны заполнить требуемые поля', array($field => $value));
@session_write_close();
exit($response);
}
$comment .= $field.": ".$value."\r\n";
$properties[$field] = $value;
}
// set
$id = $_POST['id'];
$receiver = $_POST['receiver'];
$email = $_POST['email'];
$class = $_POST['class'];
$count = $_POST['count'];
if (!$modx->getCount('modResource', array('id' => $id, 'published' => 1, 'deleted' => 0))) {
$response = $miniShop2->error('Данный билет нельзя приобрести!');
@session_write_close();
exit($response);
}
// cart
$cart->clean();
$cart->add($id, $count, array(
'receiver' => $receiver,
'class' => $class,
));
$status = $cart->status();
if (empty($status['total_count'])) {
$response = $miniShop2->error('Ошибка инициализации покупки!');
@session_write_close();
exit($response);
}
// order
$order->clean();
$order->set(array(
'email' => $email,
'receiver' => $receiver,
'phone' => '',
'delivery' => 1,
'payment' => 3, // msMerchant
'index' => ' ',
'address' => ' ',
'comment' => $comment,
'properties' => $modx->toJSON($properties)
)
);
$response = $miniShop2->order->submit(array(
''
));
@session_write_close();
exit($response);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment