Created
October 3, 2016 14:05
-
-
Save u7ter/be734d98cf8e0e1bdaa2fe338256f3a7 to your computer and use it in GitHub Desktop.
Bitrix. New order d7
This file contains hidden or 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
| Так можно ручками создать заказ | |
| <?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php"); | |
| global $USER; | |
| use \Bitrix\Main, | |
| \Bitrix\Main\Localization\Loc as Loc, | |
| Bitrix\Main\Loader, | |
| Bitrix\Main\Config\Option, | |
| Bitrix\Sale\Delivery, | |
| Bitrix\Sale\PaySystem, | |
| Bitrix\Sale, | |
| Bitrix\Sale\Order, | |
| Bitrix\Sale\DiscountCouponsManager, | |
| Bitrix\Main\Context; | |
| if (!Loader::IncludeModule('sale')) | |
| die(); | |
| function getPropertyByCode($propertyCollection, $code) { | |
| foreach ($propertyCollection as $property) | |
| { | |
| if($property->getField('CODE') == $code) | |
| return $property; | |
| } | |
| } | |
| $siteId = \Bitrix\Main\Context::getCurrent()->getSite(); | |
| $fio = 'Пупкин Василий'; | |
| $phone = '9511111111'; | |
| $email = 'pupkin@mail.ru'; | |
| $currencyCode = Option::get('sale', 'default_currency', 'RUB'); | |
| DiscountCouponsManager::init(); | |
| $order = Order::create($siteId, \CSaleUser::GetAnonymousUserID()); | |
| $order->setPersonTypeId(1); | |
| $basket = Sale\Basket::loadItemsForFUser(\CSaleBasket::GetBasketUserID(), $siteId)->getOrderableItems(); | |
| /* Действия над товарами | |
| $basketItems = $basket->getBasketItems(); | |
| foreach ($basketItems as $basketItem) { | |
| } | |
| */ | |
| $order->setBasket($basket); | |
| /*Shipment*/ | |
| $shipmentCollection = $order->getShipmentCollection(); | |
| $shipment = $shipmentCollection->createItem(); | |
| $shipmentItemCollection = $shipment->getShipmentItemCollection(); | |
| $shipment->setField('CURRENCY', $order->getCurrency()); | |
| foreach ($order->getBasket() as $item) | |
| { | |
| $shipmentItem = $shipmentItemCollection->createItem($item); | |
| $shipmentItem->setQuantity($item->getQuantity()); | |
| } | |
| $arDeliveryServiceAll = Delivery\Services\Manager::getRestrictedObjectsList($shipment); | |
| $shipmentCollection = $shipment->getCollection(); | |
| if (!empty($arDeliveryServiceAll)) { | |
| reset($arDeliveryServiceAll); | |
| $deliveryObj = current($arDeliveryServiceAll); | |
| if ($deliveryObj->isProfile()) { | |
| $name = $deliveryObj->getNameWithParent(); | |
| } else { | |
| $name = $deliveryObj->getName(); | |
| } | |
| $shipment->setFields(array( | |
| 'DELIVERY_ID' => $deliveryObj->getId(), | |
| 'DELIVERY_NAME' => $name, | |
| 'CURRENCY' => $order->getCurrency() | |
| )); | |
| $shipmentCollection->calculateDelivery(); | |
| } | |
| /**/ | |
| /*Payment*/ | |
| $arPaySystemServiceAll = []; | |
| $paySystemId = 1; | |
| $paymentCollection = $order->getPaymentCollection(); | |
| $remainingSum = $order->getPrice() - $paymentCollection->getSum(); | |
| if ($remainingSum > 0 || $order->getPrice() == 0) | |
| { | |
| $extPayment = $paymentCollection->createItem(); | |
| $extPayment->setField('SUM', $remainingSum); | |
| $arPaySystemServices = PaySystem\Manager::getListWithRestrictions($extPayment); | |
| $arPaySystemServiceAll += $arPaySystemServices; | |
| if (array_key_exists($paySystemId, $arPaySystemServiceAll)) | |
| { | |
| $arPaySystem = $arPaySystemServiceAll[$paySystemId]; | |
| } | |
| else | |
| { | |
| reset($arPaySystemServiceAll); | |
| $arPaySystem = current($arPaySystemServiceAll); | |
| } | |
| if (!empty($arPaySystem)) | |
| { | |
| $extPayment->setFields(array( | |
| 'PAY_SYSTEM_ID' => $arPaySystem["ID"], | |
| 'PAY_SYSTEM_NAME' => $arPaySystem["NAME"] | |
| )); | |
| } | |
| else | |
| $extPayment->delete(); | |
| } | |
| /**/ | |
| $order->doFinalAction(true); | |
| $propertyCollection = $order->getPropertyCollection(); | |
| $emailProperty = getPropertyByCode($propertyCollection, 'EMAIL'); | |
| $emailProperty->setValue($email); | |
| $phoneProperty = getPropertyByCode($propertyCollection, 'PHONE'); | |
| $phoneProperty->setValue($phone); | |
| $order->setField('CURRENCY', $currencyCode); | |
| $order->setField('COMMENTS', 'Комментарии'); | |
| $order->save(); | |
| $orderId = $order->GetId(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment