Last active
June 30, 2018 08:54
-
-
Save nafilimonov/a4ac32c4553c19e62a3fcde15a9b4627 to your computer and use it in GitHub Desktop.
Bitrix. Sale. Купить в 1 клик.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
if (!CModule::IncludeModule('sale') | |
|| !CModule::IncludeModule('iblock') | |
|| !CModule::IncludeModule('catalog') | |
|| !CModule::IncludeModule('currency')) | |
die(); | |
global $USER, $APPLICATION; | |
$work = true; | |
// проверки | |
if (empty($_POST['user__name'])) { | |
$data = array( | |
'title' => 'Вы не ввели ФИО', | |
); | |
$work = false; | |
} | |
if (empty($_POST['user__email'])) { | |
$data = array( | |
'title' => 'Вы не ввели EMAIL', | |
); | |
$work = false; | |
} | |
if (empty($_POST['user__phone'])) { | |
$data = array( | |
'title' => 'Вы не ввели Телефон', | |
); | |
$work = false; | |
} | |
if (empty($_POST['user__product'])) { | |
$data = array( | |
'title' => 'Ошибка. Попробуйте позже', | |
); | |
$work = false; | |
} | |
if (empty($_POST['user__city'])) { | |
$_POST['user__city'] = 2355; | |
} | |
if ( $work ) { | |
$register_new_user = false; | |
$send_letter = true; | |
// авторизация и регистрация пользователя | |
if (!$USER->IsAuthorized()) { | |
$dbUser = CUser::GetList(($by = 'ID'), ($order = 'ASC'), array('=EMAIL' => $_POST['user__email'])); // ишем пользователя | |
if ($dbUser->SelectedRowsCount() == 0) { | |
$login = $_POST['user__email']; | |
$register_new_user = true; | |
} elseif ($dbUser->SelectedRowsCount() == 1) { | |
$ar_user = $dbUser->Fetch(); | |
$registeredUserID = $ar_user['ID']; | |
} | |
if ($register_new_user) { | |
$use_captcha = COption::GetOptionString('main', 'captcha_registration', 'N'); | |
if ($use_captcha == 'Y') | |
COption::SetOptionString('main', 'captcha_registration', 'N'); | |
$userPassword = randString(10); | |
$userFIO = parseFIOString($_POST['user__name']); | |
$newUser = $USER->Register($login, $userFIO[0], $userFIO[1], $userPassword, $userPassword, $login); | |
if ($use_captcha == 'Y') | |
COption::SetOptionString('main', 'captcha_registration', 'Y'); | |
if ($newUser['TYPE'] == 'ERROR') { | |
die(getResultJsonArray(GetMessage('1CB_USER_REGISTER_FAIL'), 'N', $newUser['MESSAGE'])); | |
} else { | |
$registeredUserID = $USER->GetID(); | |
if (!empty($_POST['user__phone'])) | |
$userUpd = $USER->Update($registeredUserID, array('PERSONAL_PHONE' => $_POST['user__phone'])); | |
$USER->Logout(); | |
} | |
} | |
} else { | |
$registeredUserID = $USER->GetID(); | |
} | |
// Данные по умолчанию | |
$deliveryId = 3; // доставка | |
$paymentId = 2; // оплата | |
$personType = 1; // тип плательшика | |
$currency = "RUB"; // валюта | |
$buyMode = "ONE"; | |
$basketUserID = CSaleBasket::GetBasketUserID(); // id корзины пользователя | |
//данные заказа | |
$newOrder = array( | |
'LID' => SITE_ID, | |
'PERSON_TYPE_ID' => $personType, | |
'PAYED' => 'N', | |
'CURRENCY' => $currency, | |
'USER_ID' => $registeredUserID | |
); | |
if ($buyMode == 'ALL') { | |
$db_basket_items = CSaleBasket::GetList( | |
array('SORT' => 'DESC'), | |
array('FUSER_ID' => $basketUserID, 'LID' => SITE_ID, 'ORDER_ID' => 'NULL', 'DELAY' => 'N') | |
); | |
$newOrder['COMMENTS'] = "Buy in one click from the basket"; | |
} | |
else { | |
//чистим корзину | |
CSaleBasket::DeleteAll($basketUserID); | |
//Добавляем товар в корзину | |
$addID = Add2BasketByProductID($_POST['user__product'], 1); | |
//Получаем данные товара | |
$db_basket_items = CSaleBasket::GetList( | |
array('SORT' => 'DESC'), | |
array('FUSER_ID' => $basketUserID, 'LID' => SITE_ID, 'ID' => $addID, 'ORDER_ID' => 'NULL', 'DELAY' => 'N') | |
); | |
$newOrder['COMMENTS'] = "Buy in one click from the item card"; | |
} | |
$summ = 0; | |
while ($ar_tmp = $db_basket_items->Fetch()) | |
$basket[] = $ar_tmp; | |
$arUserResult = array ( | |
1 => htmlspecialchars($_POST['user__name']), | |
2 => htmlspecialchars($_POST['user__phone']), | |
3 => htmlspecialchars($_POST['user__email']), | |
4 => htmlspecialchars($_POST['user__city']), | |
); | |
//добавляем заказ | |
$arOrderDat = CSaleOrder::DoCalculateOrder( | |
SITE_ID, | |
$registeredUserID, | |
$basket, | |
$personType, | |
$arUserResult, | |
$deliveryId, | |
$paymentId, | |
array(), | |
$arErrors, | |
$arWarnings | |
); | |
$arOrderDat['USER_ID'] = $newOrder['USER_ID']; | |
$newOrderID = CSaleOrder::DoSaveOrder($arOrderDat, $newOrder, 0, $arResult["ERROR"]); | |
if ($newOrderID) { | |
$data = array( | |
'title' => 'Заказ под номером '. $newOrderID. ' успешно создан.', | |
//'message' => $registeredUserID, | |
'status' => 'ok', | |
'close' => false, | |
'hidden' => true | |
); | |
} | |
else { | |
$strError = ''; | |
if($ex = $APPLICATION->GetException()) | |
$strError = $ex->GetString(); | |
$data = array( | |
'title' => $strError, | |
); | |
} | |
} | |
echo json_encode($data); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Устарел.