Программно повторить заказ битрикс D7

Скрипт js, в котором передаем id текущего заказа:

$('body').on('click', '#repeat-detail', function(e){
$cancel = confirm("Вы уверены, что хотите повторить заказ?");
if($cancel) {
$id = $(this).data('order');
var sendData = {
id: $id
};
sendAjax(sendData, '/ajax/personal/personal-repeat.php', successCancelRep);
}
}); function sendAjax(sendData, link, func) {
$.ajax({
url: link,
type: 'POST',
data: ({sendData: sendData}),
dataType: "html",
error: error,
success: func
})
}
function error(){
alert('Ошибка при загрузке данных!');
}
function successCancelRep(result){
window.location.reload();
}

Содержимое файла personal-repeat.php:

<?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\Basket,
Bitrix\Sale,
Bitrix\Sale\Order,
Bitrix\Sale\DiscountCouponsManager,
Bitrix\Main\Context;
if (CModule::IncludeModule("sale") && CModule::IncludeModule("catalog"))
{
$order_id = $_POST['sendData']['id'];
$siteId = Context::getCurrent()->getSite();
$order = Order::loadByAccountNumber($order_id);
$currencyCode = Option::get('sale', 'default_currency', 'RUB');
$basket = $order->getBasket();
$fields = $order->getFields();
$propertyCollection = $order->getPropertyCollection();
$paymentCollection = $order->getPaymentCollection();
foreach ($paymentCollection as $payment) {
$psID = $payment->getPaymentSystemId();
$psName = $payment->getPaymentSystemName();
}
$shipmentCollection = $order->getShipmentCollection();
foreach ($shipmentCollection as $shipment) {
if($shipment->isSystem())
continue;
$shName = $shipment->getField('DELIVERY_NAME');
$shId = $shipment->getField('DELIVERY_ID');
}
$orderNew = Order::create($siteId, $USER->GetID());
$orderNew->setPersonTypeId(2);
$basketNew = Basket::create($siteId);

foreach ($basket as $key=>$basketItem){
$item=$basketNew->createItem('catalog',$basketItem->getProductId());
$item->setFields(array('QUANTITY'=>$basketItem->getQuantity(),'CURRENCY'=>$currencyCode,'LID'=>$siteId,'PRODUCT_PROVIDER_CLASS'=>'\CCatalogProductProvider',));
}
$orderNew->setBasket($basketNew);
$shipmentCollectionNew = $orderNew->getShipmentCollection();
$shipmentNew = $shipmentCollectionNew->createItem();
$shipmentNew->setFields(array(
'DELIVERY_ID' => $shId,
'DELIVERY_NAME' => $shName,
'CURRENCY' => $order->getCurrency()
));
$shipmentCollectionNew->calculateDelivery();
$paymentCollectionNew = $orderNew->getPaymentCollection();
$PaymentNew = $paymentCollectionNew->createItem();
$PaymentNew->setFields(array(
'PAY_SYSTEM_ID' => $psID,
'PAY_SYSTEM_NAME' => $psName
));
$orderNew->setField('CURRENCY', $currencyCode);

$orderNew->doFinalAction(true);
$r = $orderNew->save();
if (!$r->isSuccess())
{
var_dump($r->getErrorMessages());
} else {
echo $_POST['sendData']['id'];
}
}?>
Конечно, ненужные классы можно не подключать.

bitrixone © 2024