<?
require_once($_SERVER['DOCUMENT_ROOT'] . "/bitrix/modules/main/include/prolog_before.php");
use Bitrix\Main,
Bitrix\Main\Loader,
Bitrix\Main\Service\GeoIp,
Bitrix\Main\Context,
Bitrix\Currency\CurrencyManager,
Bitrix\Sale,
Bitrix\Sale\Order,
Bitrix\Sale\Basket,
Bitrix\Sale\Delivery,
Bitrix\Sale\PaySystem;
global $USER;
Loader::includeModule("sale");
Loader::includeModule("catalog");
$siteId = Context::getCurrent()->getSite();
$currencyCode = CurrencyManager::getBaseCurrency();
$msg_box = ""; // в этой переменной будем хранить сообщения формы
$errors = array(); // контейнер для ошибок
// проверяем корректность полей
if($_POST['order_fio'] == "") $errors[] = "Поле 'Ф.И.О.' не заполнено!";
if($_POST['order_email'] == "") $errors[] = "Поле 'E-mail' не заполнено!";
if($_POST['order_phone'] == "") $errors[] = "Поле 'Телефон' не заполнено!";
if($_POST['order_delivery'] == "") $errors[] = "Не выбран способ доставки!";
if(empty($errors)){
$order_fio = $_POST["order_fio"];
$order_phone = $_POST["order_phone"];
$order_email = $_POST["order_email"];
$order_person = $_POST["order_person"];
$order_location = $_POST["order_location"];
$order_zip = $_POST["order_zip"];
$order_city = $_POST["order_city"];
$order_ip = $_POST["order_ip"];
$order_address = $_POST["order_address"];
$order_delivery = $_POST["order_delivery"];
$order_pay = $_POST["order_pay"];
if ($USER->IsAuthorized()){
} else {
$log = str_replace([' ', '(', ')', '-'], '', $order_phone);
$psw = rand(111111, 999999);
$user = new CUser;
$arFields = Array(
"EMAIL" => $order_email,
"LOGIN" => $log,
"LID" => "ru",
"ACTIVE" => "Y",
"GROUP_ID" => array(10,11),
"PASSWORD" => $psw,
"CONFIRM_PASSWORD" => $psw
);
$ID = $user->Add($arFields);
if (intval($ID) > 0){
$arAuthResult = $USER->Login($log, $psw, "Y");
$APPLICATION->arAuthResult = $arAuthResult;
}else{
$error_box = "";
$error_box .= "<span style='color: red;'>".$user->LAST_ERROR."</span><br/>";
echo json_encode(array(
'errors' => $error_box
));
}
}
$order = Order::create($siteId, $USER->GetID());
$basket = Sale\Basket::loadItemsForFUser(Sale\Fuser::getId(), Bitrix\Main\Context::getCurrent()->getSite());
$order->setField('CURRENCY', $currencyCode);
$order->setPersonTypeId($order_person);
$order->setBasket($basket);
$shipmentCollection = $order->getShipmentCollection();
$shipment = $shipmentCollection->createItem(
Bitrix\Sale\Delivery\Services\Manager::getObjectById($order_delivery)
);
$shipmentItemCollection = $shipment->getShipmentItemCollection();
foreach ($basket as $basketItem)
{
$item = $shipmentItemCollection->createItem($basketItem);
$item->setQuantity($basketItem->getQuantity());
}
$paymentCollection = $order->getPaymentCollection();
$payment = $paymentCollection->createItem(
Bitrix\Sale\PaySystem\Manager::getObjectById($order_pay)
);
$payment->setField("SUM", $order->getPrice());
$payment->setField("CURRENCY", $order->getCurrency());
$propertyCollection = $order->getPropertyCollection();
$emailPropValue = $propertyCollection->getUserEmail();
$namePropValue = $propertyCollection->getPayerName();
$locPropValue = $propertyCollection->getDeliveryLocation();
$taxLocPropValue = $propertyCollection->getTaxLocation();
$profNamePropVal = $propertyCollection->getProfileName();
$zipPropValue = $propertyCollection->getDeliveryLocationZip();
$phonePropValue = $propertyCollection->getPhone();
$addrPropValue = $propertyCollection->getAddress();
$profName = intval(str_replace([' ', '(', ')', '-'], '', $order_phone));
if (!empty($order_email)){
$emailPropValue->setValue($order_email);
}
if (!empty($order_fio)){
$namePropValue->setValue($order_fio);
}
if (!empty($order_location)){
$locPropValue->setValue($order_location);
}
if (!empty($order_phone)){
$phonePropValue->setValue($order_phone);
}
if (!empty($order_address)){
$addrPropValue->setValue($order_address);
}
$order->doFinalAction(true);
$result = $order->save();
$orderId = $order->getId();
if ($result->isSuccess()){
$msg_box = $orderId;
} else {
$result->getErrors();
}
if ($msg_box != 0){
echo json_encode(array(
'result' => $msg_box
));
}
} else { //end if empty errors
// если были ошибки, то выводим их
$error_box = "";
foreach($errors as $one_error){
$error_box .= "<span style='color: red;'>$one_error</span><br/>";
}
echo json_encode(array(
'errors' => $error_box
));
}
Ошибка:
[Error]
Call to a member function setValue() on null (0)
/home/bitrix/ext_www/harvey.ru/api/order.php:118