Skip to content

Instantly share code, notes, and snippets.

@vozhukh
Created February 18, 2017 23:10
Show Gist options
  • Save vozhukh/d9c6a707ebb31ca4947ef2c6fad89ec1 to your computer and use it in GitHub Desktop.
Save vozhukh/d9c6a707ebb31ca4947ef2c6fad89ec1 to your computer and use it in GitHub Desktop.
Створення вручну замовлення через D7
<?
// Створення замовлення вруну
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 = '0975285878';
$email = 'fien_name@mail.net';
$currencyCode = Option::get('sale', 'default_currency', 'UAH');
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