I
I
Ilia Malashko2020-09-01 18:01:52
1C-Bitrix
Ilia Malashko, 2020-09-01 18:01:52

Why isn't a coupon created with an activity interval?

Hello.

I create a coupon like this:

CModule::IncludeModule("sale");
        CModule::IncludeModule("catalog");
        CModule::IncludeModule("iblock");

        $COUPON = randString(6);

        $addDb = \Bitrix\Sale\Internals\DiscountCouponTable::add(array(
            'DISCOUNT_ID' => 3,
            'COUPON' => $COUPON,
            'TYPE' => \Bitrix\Sale\Internals\DiscountCouponTable::TYPE_ONE_ORDER,
            'MAX_USE' => 1,
            'USER_ID' => 0,
            'DESCRIPTION' => ''
        ));

Everything works as it should, but if you try to set the activity interval, then the coupon is not created, it does not give errors. Bitrix uses datatime in this format: 01.09.2020 21:47:00
Maybe you need to transfer it in another format? I tried it in different ways, here is the last not working example:
CModule::IncludeModule("sale");
        CModule::IncludeModule("catalog");
        CModule::IncludeModule("iblock");

        $COUPON = randString(6);

        $unixStart = strtotime(date("d.m.Y H:i:s"));
        $unixEnd = $unixStart+43200; //12 часов

        $addDb = \Bitrix\Sale\Internals\DiscountCouponTable::add(array(
            'DISCOUNT_ID' => 3,
            'COUPON' => $COUPON,
            'TYPE' => \Bitrix\Sale\Internals\DiscountCouponTable::TYPE_ONE_ORDER,
            'MAX_USE' => 1,
            "ACTIVE_FROM"=>ConvertTimeStamp($unixStart, "FULL"),
            "ACTIVE_TO"=>ConvertTimeStamp($unixEnd, "FULL"),
            'USER_ID' => 0,
            'DESCRIPTION' => ''
        ));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Emelyanov, 2020-09-01
@miv-men

Try with this type new \Bitrix\Main\Type\DateTime();
AND add error handling:

if (!$addDb->isSuccess())
{
  print_r( $addDb->getErrorMessages() );
}

And instead:
$unixStart = strtotime(date("d.m.Y H:i:s"));
$unixEnd = $unixStart+43200; //12 часов
use date offset like:
$objDateTime = new DateTime("01.01.2012 00:00:00"); // "2012-01-01 00:00:00"
$objDateTime->add("1 day"); // "2012-01-02 00:00:00"
$objDateTime->add("-1 day"); // "2011-12-31 00:00:00"
$objDateTime->add("3 months - 5 days + 10 minutes"); // "2012-03-27 00:10:00"

see https://dev.1c-bitrix.ru/api_d7/bitrix/main/type/d...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question