Появилась задача сделать копию сайта без персональных данных клиентов, для передачи ее третьим лицам.
Как удалить все заказы и пользователей магазина на Битрикс
30.08.2022
Ниже пример скрипта, для Битрикса, который ударяет все заказы, оплаты, и пользователей (кроме админа). Можн запкскать через браузер, если заказовнемного, но лучше через консоль.
<?
$_SERVER['DOCUMENT_ROOT'] = __DIR__;
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php");
global $APPLICATION;
$log_file = $_SERVER["DOCUMENT_ROOT"]."/so_del.log";
\Bitrix\Main\Loader::includeModule('sale');
use Bitrix\Sale;
global $USER;
$rsUsers = CUser::GetList(
($by = "ID"),
($order = "desc"),
false
);
$i = 0;
while ($arUser = $rsUsers->Fetch()) {
$id_user = $arUser['ID'];
echo '$id_user: '.$id_user . PHP_EOL;
file_put_contents($log_file, "USER id: " . $id_user . PHP_EOL, FILE_APPEND);
if ($id_user > 1) {
$arFilter = Array(
"USER_ID" => $id_user
);
// Выберем все счета (в разных валютах) пользователя с кодом 21
$dbAccountCurrency = CSaleUserAccount::GetList(
array(),
array("USER_ID" => $id_user)
);
while ($arAccountCurrency = $dbAccountCurrency->Fetch()) {
$id_account_currency = $arAccountCurrency['ID'];
echo '$id_account_currency: '.$id_account_currency . PHP_EOL;
CSaleUserAccount::Delete($id_account_currency);
}
$db_sales = CSaleOrder::GetList(array("ID" => "ASC"), $arFilter);
while ($arSales = $db_sales->Fetch()) {
$id_order = $arSales['ID'];
echo '$id_order: '.$id_order . PHP_EOL;
file_put_contents($log_file, "ORDER id: " . $id_user . PHP_EOL, FILE_APPEND);
$order = \Bitrix\Sale\Order::load($id_order);
$r = $order->setField('CANCELED', 'Y');
if (!$r->isSuccess()) {
var_dump($r->getErrorMessages());
echo PHP_EOL;
file_put_contents($log_file, "ORDER canceled Error: " . print_r($r->getErrors(), true) . PHP_EOL, FILE_APPEND);
}
//отменяем оплаты если есть
$paymentCollection = $order->getPaymentCollection();
if($paymentCollection->isPaid()) {
foreach($paymentCollection as $payment) {
$payment->setReturn("Y");
}
}
//отменяем отгрузки если есть
$shipmentCollection = $order->getShipmentCollection();
if($shipmentCollection->isShipped()) {
$shipment = $shipmentCollection->getItemById($shipmentCollection[0]->getField("ID"));
$r = $shipment->setField("DEDUCTED", "N");
if(!$r->isSuccess()) {
file_put_contents($log_file, "ORDER deducted Error: " . print_r($r->getErrors(), true) . PHP_EOL, FILE_APPEND);
}
}
$r = $order->save();
if (!$r->isSuccess()) {
var_dump($r->getErrorMessages());
echo PHP_EOL;
file_put_contents($log_file, "ORDER save Error: " . print_r($r->getErrors(), true) . PHP_EOL, FILE_APPEND);
}
$r = $order->delete($id_order);
if (!$r->isSuccess()) {
var_dump($r->getErrorMessages());
echo PHP_EOL;
file_put_contents($log_file, "ORDER delete Error: " . print_r($r->getErrors(), true) . PHP_EOL, FILE_APPEND);
}
}
$r = CUser::Delete($id_user);
if (!$r) {
file_put_contents($log_file, "ORDER user Error: " . print_r($r, true) . PHP_EOL, FILE_APPEND);
var_dump($APPLICATION->LAST_ERROR);
}
var_dump($r);
echo PHP_EOL;
}
$i++;
// if ($i > 1000) {
// die();
// }
}