Advertisement
Trigub_Ilia

Оформление заказа на D7

Oct 4th, 2018
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.75 KB | None | 0 0
  1. <?
  2. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
  3.  
  4. //ini_set('error_reporting', E_ALL);
  5. //ini_set('display_errors', 1);
  6. //ini_set('display_startup_errors', 1);
  7.  
  8. use Bitrix\Main,
  9.     Bitrix\Main\Loader,
  10.     Bitrix\Main\Config\Option,
  11.     Bitrix\Sale,
  12.     Bitrix\Sale\Order,
  13.     Bitrix\Main\Application,
  14.     Bitrix\Sale\DiscountCouponsManager;
  15.  
  16. global $USER;
  17.  
  18. CModule::IncludeModule("catalog");
  19. CModule::IncludeModule("sale");
  20.  
  21. // Выберем записи корзины текущего пользователя
  22. $totalPrice = 0;
  23. $listItemPrice = 0;
  24. $dbBasketItems = CSaleBasket::GetList(
  25.     array(
  26.         "NAME" => "ASC",
  27.         "ID" => "ASC"
  28.     ),
  29.     array(
  30.         "FUSER_ID" => CSaleBasket::GetBasketUserID(),
  31.         "LID" => SITE_ID,
  32.         "ORDER_ID" => "NULL"
  33.     ),
  34.     false,
  35.     false,
  36.     array()
  37. );
  38. if (intval($dbBasketItems->SelectedRowsCount())>0){
  39.     while ($arItems = $dbBasketItems->Fetch())
  40.     {
  41.         $price_itogo = intval($arItems['PRICE']) * $arItems['QUANTITY'];
  42.         $sostav = $sostav.$arItems['NAME'].' - '.$arItems['QUANTITY'].' шт. - '.$price_itogo.' руб. <br>'; // ПИШЕМ ПОСТРОЧНО ЧЕРЕЗ бр НОВЫЕ ТОВАРЫ
  43.         $sostav2 = $sostav2.$arItems['NAME'].' - '.$arItems['QUANTITY'].' шт. - '.$price_itogo.' руб. '."\n\r";
  44.     }
  45. }
  46.  
  47. if (!Loader::IncludeModule('sale')) die();
  48. $request = Application::getInstance()->getContext()->getRequest();
  49. global $USER, $APPLICATION;
  50.  
  51. $siteId = \Bitrix\Main\Context::getCurrent()->getSite();
  52. $registeredUserID = ((empty($USER->GetID()) || ($USER->GetID() == 25))) ? 4 : $USER->GetID(); // или тут присваиваем идентификатор анонимного пользователя
  53.  
  54. $order = Order::create($siteId, $registeredUserID);
  55. $order->setPersonTypeId(1); // ИД типа пользователя
  56.  
  57. $basket = Sale\Basket::loadItemsForFUser(\CSaleBasket::GetBasketUserID(),
  58.     Bitrix\Main\Context::getCurrent()->getSite())->getOrderableItems();
  59.  
  60. $order->setBasket($basket);
  61.  
  62. $shipmentCollection = $order->getShipmentCollection();
  63. $shipment = $shipmentCollection->createItem();
  64.  
  65. //Доставка
  66. $shipment->setFields(array(
  67.     'DELIVERY_ID' => (!empty($_POST["delivery"])) ? 2 : 3,
  68.     'DELIVERY_NAME' => 'Самовывоз',
  69.     'CURRENCY' => $order->getCurrency(),
  70. ));
  71. $shipmentItemCollection = $shipment->getShipmentItemCollection();
  72.  
  73. foreach ($order->getBasket() as $item)
  74. {
  75.     $shipmentItem = $shipmentItemCollection->createItem($item);
  76.     $shipmentItem->setQuantity($item->getQuantity());
  77. }
  78.  
  79. if(!empty($_SESSION["CATALOG_USER_COUPONS"])){
  80.     $listItemPrice = intval($order->getPrice());
  81.     $totalPriceMail = intval($order->getPrice());
  82. }
  83. else{
  84.     if(!empty($_POST["delivery"])){
  85.         $listItemPrice = (intval($order->getPrice()) - 350) * 0.97;
  86.         $listItemPrice = $listItemPrice + 350;
  87.         $totalPriceMail = intval($order->getPrice());
  88.     }
  89.     else{
  90.         $listItemPrice = intval($order->getPrice()) * 0.97;
  91.         $totalPriceMail = intval($order->getPrice());
  92.     }
  93. }
  94.  
  95. //Оплата
  96. $paymentCollection = $order->getPaymentCollection();
  97. $extPayment = $paymentCollection->createItem();
  98. $extPayment->setFields(array(
  99.     'PAY_SYSTEM_ID' => 2,
  100.     'PAY_SYSTEM_NAME' => 'Tinkoff',
  101.     'SUM' => intval($listItemPrice)
  102. ));
  103.  
  104. $totalPrice = intval($listItemPrice);
  105. $totalPriceMail = intval($totalPriceMail);
  106.  
  107. $order->doFinalAction(true);
  108. $propertyCollection = $order->getPropertyCollection();
  109.  
  110. //Добавляем свойства заказа
  111. foreach ($propertyCollection->getGroups() as $group)
  112. {
  113.     foreach ($propertyCollection->getGroupProperties($group['ID']) as $property)
  114.     {
  115.         $p = $property->getProperty();
  116.         if ($p["CODE"] == "NAME"){
  117.             $property->setValue($_POST["fio"]);
  118.         }
  119.         elseif ($p["CODE"] == "PHONE"){
  120.             $property->setValue($_POST["phone"]);
  121.         }
  122.         elseif ($p["CODE"] == "EMAIL"){
  123.             $property->setValue($_POST["email"]);
  124.         }
  125.         elseif ($p["CODE"] == "CITY"){
  126.             $property->setValue($_POST["city"]);
  127.         }
  128.         elseif ($p["CODE"] == "ADRESS"){
  129.             $property->setValue($_POST["adres"]);
  130.         }
  131.         elseif ($p["CODE"] == "COMMENTS"){
  132.             $property->setValue($_POST["kom"]);
  133.         }
  134.         elseif ($p["CODE"] == "SALE"){
  135.             $property->setValue("3% от стоимости товаров");
  136.         }
  137.     }
  138. }
  139.  
  140. $order->setField('CURRENCY', "RUB");
  141.  
  142. $r = $order->save();
  143. $orderId = $order->GetId();
  144.  
  145. if(!$r->isSuccess()){
  146.     if ($ex = $APPLICATION->GetException())
  147.         echo $aaaa = $ex->GetString();
  148.  
  149.     // либо объекты ошибок с доп данными
  150.     print_r($r->getErrors());
  151.     // либо только сообщения
  152.     print_r($r->getErrorMessages());
  153.  
  154.     // так же в заказе могут быть предупреждения, которые не являются причиной остановки процесса сохранения заказа, но мы их сохраняем в маркировки
  155.     print_r($r->getWarnings());
  156.     print_r($r->getWarningMessages());
  157.     echo "Ошибка оформления";
  158. } else {
  159.     $coupon = "SL-IL2W0-QIDCDQ1";  //код купона, который нужно учесть в заказе
  160.     $order = Sale\Order::load($orderId);
  161.     Sale\DiscountCouponsManager::init(
  162.         Sale\DiscountCouponsManager::MODE_ORDER, [
  163.             "userId" => $order->getUserId(),
  164.             "orderId" => $order->getId()
  165.         ]
  166.     );
  167.     Sale\DiscountCouponsManager::add($coupon);
  168.     $discounts = $order->getDiscount();
  169.     $discounts->calculate();
  170.     $order->save();
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement