PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/components/bitrix/sale.basket.basket/ajax.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 321 lines | 280 code | 40 blank | 1 comment | 68 complexity | 51e803aa47e71a5719b98b56fc52b5b1 MD5 | raw file
  1. <?
  2. define("STOP_STATISTICS", true);
  3. define('NO_AGENT_CHECK', true);
  4. use Bitrix\Main\Loader;
  5. if (isset($_REQUEST['site_id']) && is_string($_REQUEST['site_id']))
  6. {
  7. $siteID = trim($_REQUEST['site_id']);
  8. if ($siteID !== '' && preg_match('/^[a-z0-9_]{2}$/i', $siteID) === 1)
  9. {
  10. define('SITE_ID', $siteID);
  11. }
  12. }
  13. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
  14. if (!check_bitrix_sessid() || $_SERVER["REQUEST_METHOD"] != "POST")
  15. return;
  16. if (!Loader::includeModule('sale') || !Loader::includeModule('catalog'))
  17. return;
  18. global $USER, $APPLICATION;
  19. include(dirname(__FILE__)."/functions.php");
  20. CUtil::JSPostUnescape();
  21. $arRes = array();
  22. $newProductId = false;
  23. $newBasketId = false;
  24. $action_var = (isset($_POST["action_var"]) && strlen(trim($_POST["action_var"])) > 0) ? trim($_POST["action_var"]) : "action";
  25. $arErrors = array();
  26. if (isset($_POST[$action_var]) && strlen($_POST[$action_var]) > 0)
  27. {
  28. if (array_key_exists('COUPON', $_POST) && !array_key_exists('coupon', $_POST))
  29. {
  30. $_POST["coupon"] = $_POST["COUPON"];
  31. }
  32. $arPropsValues = isset($_POST["props"]) ? $_POST["props"] : array();
  33. $strColumns = isset($_POST["select_props"]) ? $_POST["select_props"] : "";
  34. $arColumns = explode(",", $strColumns);
  35. $strOffersProps = isset($_POST["offers_props"]) ? $_POST["offers_props"] : "";
  36. $strOffersProps = explode(",", $strOffersProps);
  37. if ($_POST[$action_var] == "select_item")
  38. {
  39. $arItemSelect = array(
  40. "ID",
  41. "XML_ID",
  42. "PRODUCT_ID",
  43. "PRICE",
  44. "CURRENCY",
  45. "WEIGHT",
  46. "QUANTITY",
  47. "MODULE",
  48. "PRODUCT_PROVIDER_CLASS",
  49. "CALLBACK_FUNC",
  50. "NOTES"
  51. );
  52. $arItem = false;
  53. $currentId = 0;
  54. if (isset($_POST['basketItemId']))
  55. $currentId = (int)$_POST['basketItemId'];
  56. if ($currentId > 0)
  57. {
  58. $dbItemRes = CSaleBasket::GetList(array(),
  59. array('ID' => $currentId),
  60. false,
  61. false,
  62. $arItemSelect
  63. );
  64. $arItem = $dbItemRes->Fetch();
  65. }
  66. if ($arItem)
  67. {
  68. $dbProp = CSaleBasket::GetPropsList(
  69. array("SORT" => "ASC", "ID" => "ASC"),
  70. array("BASKET_ID" => $arItem["ID"]),
  71. false,
  72. false,
  73. array('NAME', 'CODE', 'VALUE', 'SORT')
  74. );
  75. while ($arProp = $dbProp->Fetch())
  76. {
  77. if (!isset($arItem['PROPS']))
  78. $arItem['PROPS'] = array();
  79. $arItem['PROPS'][] = $arProp;
  80. }
  81. $element = false;
  82. $sku = false;
  83. $parentId = 0;
  84. $elementIterator = \Bitrix\Iblock\ElementTable::getList(array(
  85. 'select' => array('ID', 'IBLOCK_ID', 'XML_ID'),
  86. 'filter' => array('ID' => $arItem['PRODUCT_ID'])
  87. ));
  88. $element = $elementIterator->fetch();
  89. unset($elementIterator);
  90. if (!empty($element))
  91. {
  92. $sku = CCatalogSKU::GetInfoByOfferIBlock($element['IBLOCK_ID']);
  93. if (!empty($sku))
  94. {
  95. $propertyIterator = CIBlockElement::GetProperty(
  96. $element['IBLOCK_ID'],
  97. $element['ID'],
  98. array(),
  99. array('ID' => $sku['SKU_PROPERTY_ID'])
  100. );
  101. if ($property = $propertyIterator->Fetch())
  102. {
  103. $parentId = (int)$property['VALUE'];
  104. }
  105. unset($property, $propertyIterator);
  106. }
  107. }
  108. if (!empty($element) && $parentId > 0)
  109. {
  110. $bBasketUpdate = false;
  111. $arPropsValues["CML2_LINK"] = $parentId;
  112. $newProductId = getProductByProps($element['IBLOCK_ID'], $arPropsValues, true);
  113. if (!empty($newProductId))
  114. {
  115. if ($productProvider = CSaleBasket::GetProductProvider($arItem))
  116. {
  117. $arFieldsTmp = $productProvider::GetProductData(array(
  118. "PRODUCT_ID" => $newProductId['ID'],
  119. "QUANTITY" => $arItem['QUANTITY'],
  120. "RENEWAL" => "N",
  121. "USER_ID" => $USER->GetID(),
  122. "SITE_ID" => SITE_ID,
  123. "BASKET_ID" => $arItem['ID'],
  124. "CHECK_QUANTITY" => "Y",
  125. "CHECK_PRICE" => "Y",
  126. "NOTES" => $arItem["NOTES"]
  127. ));
  128. }
  129. elseif (isset($arItem["CALLBACK_FUNC"]) && !empty($arItem["CALLBACK_FUNC"]))
  130. {
  131. $arFieldsTmp = CSaleBasket::ExecuteCallbackFunction(
  132. $arItem["CALLBACK_FUNC"],
  133. $arItem["MODULE"],
  134. $newProductId['ID'],
  135. $arItem['QUANTITY'],
  136. "N",
  137. $USER->GetID(),
  138. SITE_ID
  139. );
  140. }
  141. if (!empty($arFieldsTmp) && is_array($arFieldsTmp))
  142. {
  143. $arFields = array(
  144. 'PRODUCT_ID' => $newProductId['ID'],
  145. 'PRODUCT_PRICE_ID' => $arFieldsTmp["PRODUCT_PRICE_ID"],
  146. 'PRICE' => $arFieldsTmp["PRICE"],
  147. 'CURRENCY' => $arFieldsTmp["CURRENCY"],
  148. 'QUANTITY' => $arFieldsTmp['QUANTITY'],
  149. 'WEIGHT' => $arFieldsTmp['WEIGHT'],
  150. );
  151. $arProps = array();
  152. if (strpos($newProductId['XML_ID'], '#') === false)
  153. {
  154. $parentIterator = \Bitrix\Iblock\ElementTable::getList(array(
  155. 'select' => array('ID', 'XML_ID'),
  156. 'filter' => array('ID' => $parentId)
  157. ));
  158. if ($parentProduct = $parentIterator->fetch())
  159. {
  160. $newProductId['XML_ID'] = $parentProduct['XML_ID'].'#'.$newProductId['XML_ID'];
  161. }
  162. unset($parentProduct, $parentIterator);
  163. }
  164. $arFields["PRODUCT_XML_ID"] = $newProductId['XML_ID'];
  165. $propertyIterator = \Bitrix\Iblock\PropertyTable::getList(array(
  166. 'select' => array('ID', 'CODE'),
  167. 'filter' => array('IBLOCK_ID' => $newProductId['IBLOCK_ID'], '!ID' => $sku['SKU_PROPERTY_ID'])
  168. ));
  169. while ($property = $propertyIterator->fetch())
  170. {
  171. $property['CODE'] = (string)$property['CODE'];
  172. $arPropsSku[] = ($property['CODE'] != '' ? $property['CODE'] : $property['ID']);
  173. }
  174. unset($property, $propertyIterator);
  175. $product_properties = CIBlockPriceTools::GetOfferProperties(
  176. $newProductId['ID'],
  177. $sku['PRODUCT_IBLOCK_ID'],
  178. $arPropsSku
  179. );
  180. $newValues = array();
  181. foreach ($product_properties as $productSkuProp)
  182. {
  183. $bFieldExists = false;
  184. foreach ($strOffersProps as $existingSkuProp)
  185. {
  186. if ($existingSkuProp == $productSkuProp["CODE"])
  187. {
  188. $bFieldExists = true;
  189. break;
  190. }
  191. }
  192. if ($bFieldExists === true)
  193. {
  194. $newValues[] = array(
  195. "NAME" => $productSkuProp["NAME"],
  196. "CODE" => $productSkuProp["CODE"],
  197. "VALUE" => $productSkuProp["VALUE"],
  198. "SORT" => $productSkuProp["SORT"]
  199. );
  200. }
  201. }
  202. $newValues[] = array(
  203. "NAME" => "Product XML_ID",
  204. "CODE" => "PRODUCT.XML_ID",
  205. "VALUE" => $newProductId["XML_ID"]
  206. );
  207. $arFields['PROPS'] = (isset($arItem['PROPS']) ? updateBasketOffersProps($arItem['PROPS'], $newValues) : $newValues);
  208. unset($newValues);
  209. if (empty($arErrors))
  210. {
  211. $bBasketUpdate = CSaleBasket::Update($arItem['ID'], $arFields);
  212. }
  213. }
  214. else
  215. {
  216. $arErrors[] = GetMessage('SBB_PRODUCT_PRICE_NOT_FOUND');
  217. }
  218. }
  219. if ($bBasketUpdate === true)
  220. {
  221. CBitrixComponent::includeComponentClass("bitrix:sale.basket.basket");
  222. $basket = new CBitrixBasketComponent();
  223. $basket->weightKoef = htmlspecialcharsbx(COption::GetOptionString('sale', 'weight_koef', 1, SITE_ID));
  224. $basket->weightUnit = htmlspecialcharsbx(COption::GetOptionString('sale', 'weight_unit', "", SITE_ID));
  225. $basket->columns = $arColumns;
  226. $basket->offersProps = $strOffersProps;
  227. $basket->quantityFloat = (isset($_POST["quantity_float"]) && $_POST["quantity_float"] == "Y") ? "Y" : "N";
  228. $basket->countDiscount4AllQuantity = (isset($_POST["count_discount_4_all_quantity"]) && $_POST["count_discount_4_all_quantity"] == "Y") ? "Y" : "N";
  229. $basket->priceVatShowValue = (isset($_POST["price_vat_show_value"]) && $_POST["price_vat_show_value"] == "Y") ? "Y" : "N";
  230. $basket->hideCoupon = (isset($_POST["hide_coupon"]) && $_POST["hide_coupon"] == "Y") ? "Y" : "N";
  231. $basket->usePrepayment = (isset($_POST["use_prepayment"]) && $_POST["use_prepayment"] == "Y") ? "Y" : "N";
  232. $columnsData = $basket->getCustomColumns();
  233. $basketData = $basket->getBasketItems();
  234. $arRes["DELETE_ORIGINAL"] = "Y";
  235. $arRes["BASKET_DATA"] = $basketData;
  236. $arRes["BASKET_DATA"]["GRID"]["HEADERS"] = $columnsData;
  237. $arRes["COLUMNS"] = $strColumns;
  238. $arRes["BASKET_ID"] = $arItem['ID'];
  239. }
  240. $arRes["CODE"] = ($bBasketUpdate === true) ? "SUCCESS" : "ERROR";
  241. if ($bBasketUpdate === false && is_array($arErrors) && !empty($arErrors))
  242. {
  243. foreach ($arErrors as $error)
  244. {
  245. $arRes["MESSAGE"] .= (strlen($arRes["MESSAGE"]) > 0 ? "<br/>" : ""). $error;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. else if ($_POST[$action_var] == "recalculate")
  252. {
  253. // todo: extract duplicated code to function
  254. CBitrixComponent::includeComponentClass("bitrix:sale.basket.basket");
  255. $basket = new CBitrixBasketComponent();
  256. $basket->onIncludeComponentLang();
  257. $basket->weightKoef = htmlspecialcharsbx(COption::GetOptionString('sale', 'weight_koef', 1, SITE_ID));
  258. $basket->weightUnit = htmlspecialcharsbx(COption::GetOptionString('sale', 'weight_unit', "", SITE_ID));
  259. $basket->columns = $arColumns;
  260. $basket->offersProps = $strOffersProps;
  261. $basket->quantityFloat = (isset($_POST["quantity_float"]) && $_POST["quantity_float"] == "Y") ? "Y" : "N";
  262. $basket->countDiscount4AllQuantity = (isset($_POST["count_discount_4_all_quantity"]) && $_POST["count_discount_4_all_quantity"] == "Y") ? "Y" : "N";
  263. $basket->priceVatShowValue = (isset($_POST["price_vat_show_value"]) && $_POST["price_vat_show_value"] == "Y") ? "Y" : "N";
  264. $basket->hideCoupon = (isset($_POST["hide_coupon"]) && $_POST["hide_coupon"] == "Y") ? "Y" : "N";
  265. $basket->usePrepayment = (isset($_POST["use_prepayment"]) && $_POST["use_prepayment"] == "Y") ? "Y" : "N";
  266. $res = $basket->recalculateBasket($_POST);
  267. foreach ($res as $key => $value)
  268. {
  269. $arRes[$key] = $value;
  270. }
  271. $arRes["BASKET_DATA"] = $basket->getBasketItems();
  272. $arRes["BASKET_DATA"]["GRID"]["HEADERS"] = $basket->getCustomColumns();
  273. $arRes["COLUMNS"] = $strColumns;
  274. $arRes["CODE"] = "SUCCESS";
  275. }
  276. }
  277. $arRes["PARAMS"]["QUANTITY_FLOAT"] = (isset($_POST["quantity_float"]) && $_POST["quantity_float"] == "Y") ? "Y" : "N";
  278. $APPLICATION->RestartBuffer();
  279. header('Content-Type: application/json; charset='.LANG_CHARSET);
  280. echo CUtil::PhpToJSObject($arRes);
  281. die();