|
Доброго времени суток. Помогите, пожалуйста, внедрить прикрепление купона к заказу в api. Необходимо внедрить прикрепление купона на странице корзины, чтобы сразу видеть изменение цены, с записью в заказ. Если я не ошибаюсь, это страница корзины Скрытый текст |
---|
Код |
---|
if (isset($_GET['cart']) && !isset($_GET['update']) && !isset($_GET['order']) && !isset($_GET['remove'])) {
foreach (getallheaders() as $name => $value) {
if (strtolower($name) == 'token-auth') {
$get_headers = explode('.', $value);
$ID_USER = $get_headers[1];
$TOKEN = $get_headers[0];
}
}
$rsUser = CUser::GetByID($ID_USER);
if ($arUser = $rsUser->Fetch()) {
if (md5($CONF_SECRET_TOKEN . md5($arUser['PASSWORD'])) == $TOKEN) {
global $USER;
$USER->Authorize($ID_USER);
$dbBasketItems = CSaleBasket::GetList(
array(
"NAME" => "ASC",
"ID" => "ASC"
),
array(
"FUSER_ID" => CSaleBasket::GetBasketUserID(),
"LID" => 's2',
"ORDER_ID" => "NULL",
),
false,
false,
array(
'*'
)
);
$return_array = array();
while ($arItems = $dbBasketItems->Fetch()) {
$totalPrePrice = $totalPrePrice + (get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'regular_price') * intval($arItems['QUANTITY']));
}
$dbBasketItems = CSaleBasket::GetList(
array(
"NAME" => "ASC",
"ID" => "ASC"
),
array(
"FUSER_ID" => CSaleBasket::GetBasketUserID(),
"LID" => 's2',
"ORDER_ID" => "NULL",
),
false,
false,
array(
'*'
)
);
while ($arItems = $dbBasketItems->Fetch()) {
if (intval($IBLOCK_SECTION_ID) == 0) {
$IBLOCK_SECTION_ID = get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'iblock_section');
}
if ($totalPrePrice > 1000) {
$priceclient = get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'regular_price');
} else {
$priceclient = get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'old_price');
}
$add_cart = array(
'id_item' => $arItems['ID'],
'id' => $arItems['PRODUCT_ID'],
'name' => get_text($arItems['NAME']),
'code' => get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'code'),
'image' => get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'image'),
'image_preview' => get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'image_preview'),
'regular_price' => $priceclient,
'old_price' => get_field_object($arItems['PRODUCT_ID'], $IBLOCK_ID, $URL_CONFIG, 'old_price'),
'count' => intval($arItems['QUANTITY'])
);
$totalPrice = $totalPrice + ($priceclient * intval($arItems['QUANTITY']));
array_push($return_array, $add_cart);
}
$status = 200;
$array_auth = array(
'products' => $return_array,
'status' => $status,
'totalPrice' => ceil($totalPrice),
'similars' => get_similars(array($IBLOCK_SECTION_ID), $URL_CONFIG, $IBLOCK_ID),
);
header($_SERVER["SERVER_PROTOCOL"] . " $status Status", true, $status);
echo json_encode($array_auth);
}
}
}
|
|
Это запись в заказ Скрытый текст |
---|
Код |
---|
if (isset($_GET['cart']) && isset($_GET['order'])) {
$delivery_type = trim($_POST['delivery_type']);
$exp_delivery_type = explode('_', $delivery_type);
if ($exp_delivery_type[1] > 0) {
$delivery_type = $exp_delivery_type[0];
$delivery_type_store = $exp_delivery_type[1];
}
$company_name = trim($_POST['company_name']);
$company_address = trim($_POST['company_address']);
$company_inn = trim($_POST['company_inn']);
$company_kpp = trim($_POST['company_kpp']);
$user_email = trim($_POST['user_email']);
$user_name = trim($_POST['user_name']);
$user_phone = trim($_POST['user_phone']);
$user_type = trim($_POST['user_type']);
$user_comments = trim($_POST['user_comments']);
$subscribe = trim($_POST['subscribe']);
$delivery_city = trim($_POST['delivery_city']);
$delivery_street = trim($_POST['delivery_street']);
$payments = trim($_POST['payments']);
$products = trim($_POST['products']);
foreach (getallheaders() as $name => $value) {
if (strtolower($name) == 'token-auth') {
$get_headers = explode('.', $value);
$ID_USER = $get_headers[1];
$TOKEN = $get_headers[0];
}
}
$rsUser = CUser::GetByID($ID_USER);
if ($arUser = $rsUser->Fetch()) {
if (md5($CONF_SECRET_TOKEN . md5($arUser['PASSWORD'])) == $TOKEN) {
global $USER;
$USER->Authorize($ID_USER);
$cart_sum = 0;
$cart_num = 0;
$arBasketItems = array();
$dbBasketItems = CSaleBasket::GetList(
array("NAME" => "ASC", "ID" => "ASC"),
array("FUSER_ID" => CSaleBasket::GetBasketUserID(), "LID" => SITE_ID, "ORDER_ID" => "NULL"),
false,
false,
array("ID", "MODULE", "PRODUCT_ID", "QUANTITY", "CAN_BUY", "PRICE")
);
while ($arItems = $dbBasketItems->Fetch()) {
$arItems = CSaleBasket::GetByID($arItems["ID"]);
$arBasketItems[] = $arItems;
$cart_num += $arItems['QUANTITY'];
$cart_sum += $arItems['PRICE'] * $arItems['QUANTITY'];
}
$currencyCode = CurrencyManager::getBaseCurrency();
$siteId = \Bitrix\Main\Context::getCurrent()->getSite();
$order = Order::create($siteId, $ID_USER);
$order->setPersonTypeId($user_type);
$basket = Bitrix\Sale\Basket::loadItemsForFUser(
\CSaleBasket::GetBasketUserID(),
Bitrix\Main\Context::getCurrent()->getSite()
)->getOrderableItems();
$order->setBasket($basket);
$shipmentCollection = $order->getShipmentCollection();
$shipment = $shipmentCollection->createItem();
$arDeliv = CSaleDelivery::GetByID($delivery_type);
if ($arDeliv) {
//echo "Доставка \"".$arDeliv["NAME"]."\" стоит ".CurrencyFormat($arDeliv["PRICE"], $arDeliv["CURRENCY"]);
}
//'STORE_ID' => $delivery_type_store
if ($delivery_type == 2) {
$arrD = array(
'DELIVERY_ID' => $delivery_type,
'DELIVERY_NAME' => $arDeliv["NAME"],
'CURRENCY' => $order->getCurrency()
);
} else {
$arrD = array(
'DELIVERY_ID' => $delivery_type,
'DELIVERY_NAME' => $arDeliv["NAME"],
'CURRENCY' => $order->getCurrency()
);
}
$shipment->setFields($arrD);
if ($delivery_type_store > 0) {
//\Bitrix\Sale\Shipment::setStoreId($storeId);
$shipment->setStoreId($delivery_type_store);
}
$shipmentItemCollection = $shipment->getShipmentItemCollection();
foreach ($order->getBasket() as $item) {
$shipmentItem = $shipmentItemCollection->createItem($item);
$shipmentItem->setQuantity($item->getQuantity());
}
//arPaySys = CSalePaySystem::GetByID($payments, $user_type);
$arPaySys = CSalePaySystemAction::GetByID($payments);
$paymentCollection = $order->getPaymentCollection();
$extPayment = $paymentCollection->createItem();
$extPayment->setFields(array(
'PAY_SYSTEM_ID' => $payments,
'PAY_SYSTEM_NAME' => $arPaySys['NAME'],
'SUM' => $order->getPrice()
));
$order->doFinalAction(true);
$propertyCollection = $order->getPropertyCollection();
foreach ($propertyCollection->getGroups() as $group) {
foreach ($propertyCollection->getGroupProperties($group['ID']) as $property) {
$p = $property->getProperty();
if ($p["CODE"] == "CONTACT_PERSON") {
$property->setValue($user_name);
}
if ($p["CODE"] == "Mobilka") {
$property->setValue('Y');
}
if ($p["CODE"] == "FIO") {
$property->setValue($arUser['NAME'] . ' ' . $arUser['LAST_NAME']);
}
if ($p["CODE"] == "EMAIL") {
$property->setValue($user_email);
}
if ($p["CODE"] == "PHONE") {
$property->setValue($user_phone);
}
if ($p["CODE"] == "ADDRESS") {
$property->setValue($delivery_street);
}
if ($p["CODE"] == "INN") {
$property->setValue($company_inn);
}
if ($p["CODE"] == "KPP") {
$property->setValue($company_kpp);
}
if ($p["CODE"] == "COMPANY") {
$property->setValue($company_name);
}
if ($p["CODE"] == "COMPANY_ADR") {
$property->setValue($company_address);
}
}
}
if ($delivery_street <> '') {
$inner_delivery_street = 'Доставка по адресу для курьера: ' . $delivery_street;
}
$order->setField('CURRENCY', $currencyCode);
$order->setField('USER_DESCRIPTION', '' . $user_comments);
$r = $order->save();
$ORDER_ID = $order->GetId();
if (!$r->isSuccess()) {
if ($ex = $APPLICATION->GetException())
$qw = $ex->GetString();
$error = array();
$error[0] = $qw;
$error[1] = $r->getErrors();
$error[2] = $r->getErrorMessages();
$error[3] = $r->getWarnings();
$error[4] = $r->getWarningMessages();
}
$ORDER_ID = IntVal($ORDER_ID);
if ($ORDER_ID > 0) {
if ($payments == 21) {
$orderObj = Bitrix\Sale\Order::load($ORDER_ID);
$paymentCollection = $orderObj->getPaymentCollection();
$payment = $paymentCollection[0];
$service = Bitrix\Sale\PaySystem\Manager::getObjectById($payment->getPaymentSystemId());
$context = \Bitrix\Main\Application::getInstance()->getContext();
//$finish = $service->initiatePay($payment, $context->getRequest());
$initResult = $service->initiatePay($payment, $context->getRequest(), \Bitrix\Sale\PaySystem\BaseServiceHandler::STRING);
$buffered_output = $initResult->getTemplate();
preg_match('|href="(.*?)"|i', $buffered_output, $arr);
$link_payment = $URL_CONFIG . '/redirect.php?r=' . $arr[1];
}
$status = 200;
$array_auth = array(
'status' => $status,
'success' => true,
'link_payment' => $link_payment,
'id_order' => $ORDER_ID,
'score' => false,
'array_score' => [],
'array_score_text' => [],
);
if ($payments == 17) {
$array_auth['score'] = true;
$array_auth['array_score_text'] = array(
'number' => $ORDER_ID,
'date' => date('d.m.Y', time()),
'num' => $cart_num,
'sum' => $cart_sum,
);
$array_auth['array_score'] = array(
'СЧЕТ № ' . $ORDER_ID . ' от ' . date('d.m.Y', time()),
'Всего наименований ' . $cart_num . ', на сумму ' . $cart_sum . ' р.',
'Заказ принят, мы свяжемся с вами и направим счет',
);
}
} else {
$status = 400;
$array_auth = array(
'status' => $status,
'success' => false,
'message' => $error
);
}
header($_SERVER["SERVER_PROTOCOL"] . " $status Status", true, $status);
echo json_encode($array_auth);
}
}
}
|
|
Признаюсь честно, я никогда не работал с api. Попытался внедрить варианты, описанные выше, ничего не получилось
|