Trigub_Ilia

Создание заказа

Oct 27th, 2021
662
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \CModule::IncludeModule("crm");
  2.  
  3. $res=CCrmLead::LoadProductRows($feald["LEAD_ID"]);
  4.  
  5. if(!empty($res)) {
  6. foreach ($res as $product) {
  7.     $products[] = array(
  8.         'PRODUCT_ID' => $product["PRODUCT_ID"],
  9.         'NAME' => $product["PRODUCT_NAME"],
  10.         'PRICE' => $product["PRICE"],
  11.         'CURRENCY' => "RUB",
  12.         'QUANTITY' => 1
  13.     );
  14. }
  15.  
  16. $basket = Bitrix\Sale\Basket::create(SITE_ID);
  17.  
  18. foreach ($products as $product) {
  19.     $item = $basket->createItem("catalog", $product["PRODUCT_ID"]);
  20.     unset($product["PRODUCT_ID"]);
  21.     $item->setFields($product);
  22. }
  23.  
  24. //создадим объект корзины
  25. $order = Bitrix\Sale\Order::create(SITE_ID, 1);
  26. $order->setPersonTypeId(1);
  27. $order->setBasket($basket);
  28.  
  29. //создадим сам заказ
  30. $shipmentCollection = $order->getShipmentCollection();
  31. $shipment = $shipmentCollection->createItem(
  32.     Bitrix\Sale\Delivery\Services\Manager::getObjectById(1)
  33. );
  34.  
  35. //создадим отгрузку
  36. $shipmentItemCollection = $shipment->getShipmentItemCollection();
  37. foreach ($basket as $basketItem) {
  38.     $item = $shipmentItemCollection->createItem($basketItem);
  39.     $item->setQuantity($basketItem->getQuantity());
  40. }
  41.  
  42. //создадим оплату
  43. $paymentCollection = $order->getPaymentCollection();
  44. $payment = $paymentCollection->createItem(
  45.     Bitrix\Sale\PaySystem\Manager::getObjectById(1)
  46. );
  47. $payment->setField("SUM", $order->getPrice());
  48. $payment->setField("CURRENCY", $order->getCurrency());
  49.  
  50.  
  51. //Привяжем к CRM сущности
  52. $communications = $order->getContactCompanyCollection();
  53. foreach ($communications as $communication) {
  54.     $arCom = $communication->getFields()->getValues(); //с ключами ID, ORDER_ID, ENTITY_ID, ENTITY_TYPE_ID, SORT, ROLE_ID, IS_PRIMARY
  55. }
  56.  
  57. //$itemCom = $communications->createCompany(); //компания
  58. $itemCom = $communications->createContact();//контакт
  59. $itemCom->setField('ENTITY_ID', $feald["ID"]); //ID контакта или компании
  60. $itemCom->setField('IS_PRIMARY', 'Y'); // необязательно
  61. $itemCom->getField('IS_PRIMARY');
  62.  
  63. //сохраним заказ
  64. $result = $order->save();
  65. if (!$result->isSuccess()) {
  66.     //$result->getErrors();
  67. }
RAW Paste Data