M
M
mortnes2019-12-08 20:32:23
1C-Bitrix
mortnes, 2019-12-08 20:32:23

Subscribe the user to the newsletter when placing an order with auto-registration?

There is such a code, it works during normal registration, but I can’t figure out how to do the same when registering through an order?

AddEventHandler("main", "OnAfterUserAdd", "OnAfterUserAddHandler");

function OnAfterUserAddHandler(&$arFields)
{

   $errors = Array();

   if($arFields["ID"]>0 && $arFields["UF_SUBSCRIBE"])
   {

      if(CModule::IncludeModule("subscribe")) {
      
         $USER_ID = $arFields["ID"];
         $EMAIL = $arFields["EMAIL"];
      
         $arFilter = array(
            "ACTIVE" => "Y",
            "LID" => "s1",
            "VISIBLE"=>"Y",
         );
      
         $rsRubrics = CRubric::GetList(array(), $arFilter);
         $arRubrics = array();
         while($arRubric = $rsRubrics->GetNext()) $arRubrics[] = $arRubric["ID"];
      
         $obSubscription = new CSubscription;
      
         $rsSubscription = $obSubscription->GetList(array(), array("USER_ID" => $USER_ID));
         $arSubscription = $rsSubscription->Fetch();
   
         if(is_array($arSubscription))
         {
            $rs = $obSubscription->Update(
               $arSubscription["ID"],
               array(
                  "FORMAT" => "html",
                  "RUB_ID" => $arRubrics,
               ),
               false
            );
         }
         else
         {
            $ID = $obSubscription->Add(array(
               "USER_ID" => $USER_ID,
               "ACTIVE" => "Y",
               "EMAIL" => $EMAIL,
               "FORMAT" => "html",
               "CONFIRMED" => "Y",
               "SEND_CONFIRM" => "N",
               "RUB_ID" => $arRubrics,
            ));
         }
      
      }     
   }
}

There is a suspicion that you need to add a custom user field when placing an order

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question