Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
- //ini_set('error_reporting', E_ALL);
- //ini_set('display_errors', 1);
- //ini_set('display_startup_errors', 1);
- use Bitrix\Main,
- Bitrix\Main\Loader,
- Bitrix\Main\Config\Option,
- Bitrix\Sale,
- Bitrix\Sale\Order,
- Bitrix\Main\Application,
- Bitrix\Sale\DiscountCouponsManager;
- global $USER;
- CModule::IncludeModule("catalog");
- CModule::IncludeModule("sale");
- // Выберем записи корзины текущего пользователя
- $totalPrice = 0;
- $listItemPrice = 0;
- $dbBasketItems = CSaleBasket::GetList(
- array(
- "NAME" => "ASC",
- "ID" => "ASC"
- ),
- array(
- "FUSER_ID" => CSaleBasket::GetBasketUserID(),
- "LID" => SITE_ID,
- "ORDER_ID" => "NULL"
- ),
- false,
- false,
- array()
- );
- if (intval($dbBasketItems->SelectedRowsCount())>0){
- while ($arItems = $dbBasketItems->Fetch())
- {
- $price_itogo = intval($arItems['PRICE']) * $arItems['QUANTITY'];
- $sostav = $sostav.$arItems['NAME'].' - '.$arItems['QUANTITY'].' шт. - '.$price_itogo.' руб. <br>'; // ПИШЕМ ПОСТРОЧНО ЧЕРЕЗ бр НОВЫЕ ТОВАРЫ
- $sostav2 = $sostav2.$arItems['NAME'].' - '.$arItems['QUANTITY'].' шт. - '.$price_itogo.' руб. '."\n\r";
- }
- }
- if (!Loader::IncludeModule('sale')) die();
- $request = Application::getInstance()->getContext()->getRequest();
- global $USER, $APPLICATION;
- $siteId = \Bitrix\Main\Context::getCurrent()->getSite();
- $registeredUserID = ((empty($USER->GetID()) || ($USER->GetID() == 25))) ? 4 : $USER->GetID(); // или тут присваиваем идентификатор анонимного пользователя
- $order = Order::create($siteId, $registeredUserID);
- $order->setPersonTypeId(1); // ИД типа пользователя
- $basket = Sale\Basket::loadItemsForFUser(\CSaleBasket::GetBasketUserID(),
- Bitrix\Main\Context::getCurrent()->getSite())->getOrderableItems();
- $order->setBasket($basket);
- $shipmentCollection = $order->getShipmentCollection();
- $shipment = $shipmentCollection->createItem();
- //Доставка
- $shipment->setFields(array(
- 'DELIVERY_ID' => (!empty($_POST["delivery"])) ? 2 : 3,
- 'DELIVERY_NAME' => 'Самовывоз',
- 'CURRENCY' => $order->getCurrency(),
- ));
- $shipmentItemCollection = $shipment->getShipmentItemCollection();
- foreach ($order->getBasket() as $item)
- {
- $shipmentItem = $shipmentItemCollection->createItem($item);
- $shipmentItem->setQuantity($item->getQuantity());
- }
- if(!empty($_SESSION["CATALOG_USER_COUPONS"])){
- $listItemPrice = intval($order->getPrice());
- $totalPriceMail = intval($order->getPrice());
- }
- else{
- if(!empty($_POST["delivery"])){
- $listItemPrice = (intval($order->getPrice()) - 350) * 0.97;
- $listItemPrice = $listItemPrice + 350;
- $totalPriceMail = intval($order->getPrice());
- }
- else{
- $listItemPrice = intval($order->getPrice()) * 0.97;
- $totalPriceMail = intval($order->getPrice());
- }
- }
- //Оплата
- $paymentCollection = $order->getPaymentCollection();
- $extPayment = $paymentCollection->createItem();
- $extPayment->setFields(array(
- 'PAY_SYSTEM_ID' => 2,
- 'PAY_SYSTEM_NAME' => 'Tinkoff',
- 'SUM' => intval($listItemPrice)
- ));
- $totalPrice = intval($listItemPrice);
- $totalPriceMail = intval($totalPriceMail);
- $order->doFinalAction(true);
- $propertyCollection = $order->getPropertyCollection();
- //Добавляем свойства заказа
- foreach ($propertyCollection->getGroups() as $group)
- {
- foreach ($propertyCollection->getGroupProperties($group['ID']) as $property)
- {
- $p = $property->getProperty();
- if ($p["CODE"] == "NAME"){
- $property->setValue($_POST["fio"]);
- }
- elseif ($p["CODE"] == "PHONE"){
- $property->setValue($_POST["phone"]);
- }
- elseif ($p["CODE"] == "EMAIL"){
- $property->setValue($_POST["email"]);
- }
- elseif ($p["CODE"] == "CITY"){
- $property->setValue($_POST["city"]);
- }
- elseif ($p["CODE"] == "ADRESS"){
- $property->setValue($_POST["adres"]);
- }
- elseif ($p["CODE"] == "COMMENTS"){
- $property->setValue($_POST["kom"]);
- }
- elseif ($p["CODE"] == "SALE"){
- $property->setValue("3% от стоимости товаров");
- }
- }
- }
- $order->setField('CURRENCY', "RUB");
- $r = $order->save();
- $orderId = $order->GetId();
- if(!$r->isSuccess()){
- if ($ex = $APPLICATION->GetException())
- echo $aaaa = $ex->GetString();
- // либо объекты ошибок с доп данными
- print_r($r->getErrors());
- // либо только сообщения
- print_r($r->getErrorMessages());
- // так же в заказе могут быть предупреждения, которые не являются причиной остановки процесса сохранения заказа, но мы их сохраняем в маркировки
- print_r($r->getWarnings());
- print_r($r->getWarningMessages());
- echo "Ошибка оформления";
- } else {
- $coupon = "SL-IL2W0-QIDCDQ1"; //код купона, который нужно учесть в заказе
- $order = Sale\Order::load($orderId);
- Sale\DiscountCouponsManager::init(
- Sale\DiscountCouponsManager::MODE_ORDER, [
- "userId" => $order->getUserId(),
- "orderId" => $order->getId()
- ]
- );
- Sale\DiscountCouponsManager::add($coupon);
- $discounts = $order->getDiscount();
- $discounts->calculate();
- $order->save();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement