Korusol
8/3/2017 - 2:16 PM

#bitrix #trilastin.su генерация купонов перед отправкой письма

#bitrix #trilastin.su генерация купонов перед отправкой письма

\Bitrix\Main\Loader::includeModule('sale');

$arFilterUser = array('ID' => 228);
$userId = CSaleOrder::GetList(array('ID' => 'DESC'), $arFilterUser, false, false, ['USER_ID'])->fetch();

$arFilterOrder = array('USER_ID' => $userId['USER_ID']);
$rsOrdersUser = CSaleOrder::GetList(array('ID' => 'DESC'), $arFilterOrder, false, false, ['ID']);
$OrdersUser = [];
while ($OrderUser = $rsOrdersUser->fetch()) {
    $OrdersUser[] = $OrderUser['ID'];
}

$countOrdersUser = count($OrdersUser);

$couponResult = '';

if($countOrdersUser <= 1){
    $coupon = \Bitrix\Sale\Internals\DiscountCouponTable::generateCoupon(true);

    $addDb = \Bitrix\Sale\Internals\DiscountCouponTable::add(array(
        'DISCOUNT_ID' => 2,
        'COUPON' => $coupon,
        'TYPE' => \Bitrix\Sale\Internals\DiscountCouponTable::TYPE_ONE_ORDER,
        'MAX_USE' => 1,
        'USER_ID' => $userId['USER_ID'],
        'DESCRIPTION' => ''
    ));

    if ($addDb->isSuccess()) {
        $couponResult = $coupon;
    }
}