Skip to content

Instantly share code, notes, and snippets.

@EscApp2
Last active November 29, 2023 09:49
Show Gist options
  • Save EscApp2/b25d0a7b53baeddfaff4462250e360f0 to your computer and use it in GitHub Desktop.
Save EscApp2/b25d0a7b53baeddfaff4462250e360f0 to your computer and use it in GitHub Desktop.
bitrix, add to delay , add_to_delay
<?php
define("NO_KEEP_STATISTIC", true); // Не собираем стату по действиям AJAX
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
$ajax_action = strval($_REQUEST['ajax_action']);
if($ajax_action == 'AddToDelay'){
$arParams = $_REQUEST['arParams'];
\Bitrix\Main\Loader::includeModule('iblock');
global $APPLICATION;
$strError = '';
$successfulAdd = true;
$action = strtoupper($_REQUEST[$arParams["ACTION_VARIABLE"]]);
$productID = (int)$_REQUEST[$arParams["PRODUCT_ID_VARIABLE"]];
if (\Bitrix\Main\Loader::includeModule("sale") && \Bitrix\Main\Loader::includeModule("catalog"))
{
$QUANTITY = 0;
$product_properties = array();
$intProductIBlockID = (int)CIBlockElement::GetIBlockByID($productID);
if (0 < $intProductIBlockID)
{
if ($arParams['ADD_PROPERTIES_TO_BASKET'] == 'Y')
{
if ($intProductIBlockID == $arParams["IBLOCK_ID"])
{
if (!empty($arParams["PRODUCT_PROPERTIES"]))
{
if (
isset($_REQUEST[$arParams["PRODUCT_PROPS_VARIABLE"]])
&& is_array($_REQUEST[$arParams["PRODUCT_PROPS_VARIABLE"]])
)
{
$product_properties = CIBlockPriceTools::CheckProductProperties(
$arParams["IBLOCK_ID"],
$productID,
$arParams["PRODUCT_PROPERTIES"],
$_REQUEST[$arParams["PRODUCT_PROPS_VARIABLE"]],
$arParams['PARTIAL_PRODUCT_PROPERTIES'] == 'Y'
);
if (!is_array($product_properties))
{
$strError = "CATALOG_PARTIAL_BASKET_PROPERTIES_ERROR";
$successfulAdd = false;
}
}
else
{
$strError = "CATALOG_EMPTY_BASKET_PROPERTIES_ERROR";
$successfulAdd = false;
}
}
}
else
{
$skuAddProps = (isset($_REQUEST['basket_props']) && !empty($_REQUEST['basket_props']) ? $_REQUEST['basket_props'] : '');
if (!empty($arParams["OFFERS_CART_PROPERTIES"]) || !empty($skuAddProps))
{
$product_properties = CIBlockPriceTools::GetOfferProperties(
$productID,
$arParams["IBLOCK_ID"],
$arParams["OFFERS_CART_PROPERTIES"],
$skuAddProps
);
}
}
}
if ($arParams["USE_PRODUCT_QUANTITY"])
{
if (isset($_REQUEST[$arParams["PRODUCT_QUANTITY_VARIABLE"]]))
{
$QUANTITY = doubleval($_REQUEST[$arParams["PRODUCT_QUANTITY_VARIABLE"]]);
}
}
if (0 >= $QUANTITY)
{
$rsRatios = CCatalogMeasureRatio::getList(
array(),
array('PRODUCT_ID' => $productID),
false,
false,
array('PRODUCT_ID', 'RATIO')
);
if ($arRatio = $rsRatios->Fetch())
{
$intRatio = (int)$arRatio['RATIO'];
$dblRatio = doubleval($arRatio['RATIO']);
$QUANTITY = ($dblRatio > $intRatio ? $dblRatio : $intRatio);
}
}
if (0 >= $QUANTITY)
$QUANTITY = 1;
}
else
{
$strError = 'CATALOG_ELEMENT_NOT_FOUND';
$successfulAdd = false;
}
if ($successfulAdd)
{
$notifyOption = COption::GetOptionString("sale", "subscribe_prod", "");
$arNotify = unserialize($notifyOption);
$arRewriteFields = array();
if ($action == "SUBSCRIBE_PRODUCT" && $arNotify[SITE_ID]['use'] == 'Y')
{
$arRewriteFields["SUBSCRIBE"] = "Y";
$arRewriteFields["CAN_BUY"] = "N";
}
if ($action == "ADD_TO_DELAY"){
$arRewriteFields["DELAY"] = "Y";
}
}
if ($successfulAdd)
{
if(!Add2BasketByProductID($productID, $QUANTITY, $arRewriteFields, $product_properties))
{
if ($ex = $APPLICATION->GetException()){
$strError = $ex->GetString();
}else{
$strError = "CATALOG_ERROR2BASKET";
}
$successfulAdd = false;
}
}
if ($successfulAdd)
{
$addResult = array('STATUS' => 'OK', 'MESSAGE' => 'CATALOG_SUCCESSFUL_ADD_TO_BASKET');
}
else
{
$addResult = array('STATUS' => 'ERROR', 'MESSAGE' => $strError);
}
echo CUtil::PhpToJSObject($addResult);
}
die();
}
if($ajax_action == 'RemoveFromDelay'){
$arParams = $_REQUEST['arParams'];
\Bitrix\Main\Loader::includeModule('iblock');
global $APPLICATION;
$strError = '';
$successfulAdd = true;
$action = strtoupper($_REQUEST[$arParams["ACTION_VARIABLE"]]);
$productID = (int)$_REQUEST[$arParams["PRODUCT_ID_VARIABLE"]];
if (\Bitrix\Main\Loader::includeModule("sale") && \Bitrix\Main\Loader::includeModule("catalog"))
{
$dbBasketItems = CSaleBasket::GetList(
array(),
array(
"FUSER_ID" => CSaleBasket::GetBasketUserID(),
"LID" => SITE_ID,
"ORDER_ID" => "NULL",
"DELAY"=>"Y",
"PRODUCT_ID"=>$productID,
),
false,
false,
array("ID", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "QUANTITY", "DELAY", "CAN_BUY", "CURRENCY")
);
if($ar = $dbBasketItems->Fetch()){
CSaleBasket::Delete($ar['ID']);
}
}
die();
}
?>
<?
CModule::IncludeModule('sale');
$dbBasketItems = CSaleBasket::GetList(
array(),
array(
"FUSER_ID" => CSaleBasket::GetBasketUserID(),
"LID" => SITE_ID,
"ORDER_ID" => "NULL",
"DELAY"=>"Y",
),
false,
false,
array("ID", "CALLBACK_FUNC", "MODULE", "PRODUCT_ID", "QUANTITY", "DELAY", "CAN_BUY", "CURRENCY")
);
$_SESSION["CATALOG_DELAY_LIST"] = array();
while($ar = $dbBasketItems->Fetch()){
$_SESSION["CATALOG_DELAY_LIST"][$ar['PRODUCT_ID']] = $ar['PRODUCT_ID'];
}
<?php
function AddToCartAjaxData($arParams,$arResult){
$arPost = array(
"ajax_action"=>"AddToDelay",
$arParams["ACTION_VARIABLE"] => "ADD_TO_DELAY",
"arParams"=>$arParams,
$arParams["PRODUCT_ID_VARIABLE"] => $arResult['ID'],
$arParams["PRODUCT_QUANTITY_VARIABLE"] => 1,
);
if ('Y' == $arParams['ADD_PROPERTIES_TO_BASKET'] && !empty($arResult['PRODUCT_PROPERTIES'])){
$TmpPropId = array();
$PROPS = array();
if (!empty($arResult['PRODUCT_PROPERTIES_FILL'])){
foreach ($arResult['PRODUCT_PROPERTIES_FILL'] as $propID => $propInfo){
$TmpPropId[] = $propID;
$PROPS[$propID] = htmlspecialcharsbx($propInfo['ID']);
}
}
foreach ($arResult['PRODUCT_PROPERTIES'] as $propID => $propInfo)
{
if(in_array($propID,$TmpPropId)){
continue;
}
foreach($propInfo['VALUES'] as $valueID => $value)
{
if($valueID == $propInfo['SELECTED']){
$PROPS[$propID][$valueID] = $valueID;
}
}
}
$arPost[$arParams['PRODUCT_PROPS_VARIABLE']] = $PROPS;
}
return $arPost;
}
var DelayPopup = {}
window.DelayPopup.InitPopupWindow = function(){
if (!!window.DelayPopup.obPopupWin){
return;
}
window.DelayPopup.obPopupWin = BX.PopupWindowManager.create("", null, {
autoHide: false,
offsetLeft: 0,
offsetTop: 0,
overlay : true,
closeByEsc: true,
titleBar: true,
closeIcon: {top: '-15px', right: '-10px'}
});
}
window.DelayPopup.BasketButton = function(params) {
window.DelayPopup.BasketButton.superclass.constructor.apply(this, arguments);
this.nameNode = BX.create('span', {
props : { className : 'bx_medium bx_bt_button', id : this.id },
style: typeof(params.style) === 'object' ? params.style : {},
text: params.text
});
this.buttonNode = BX.create('span', {
attrs: { className: params.ownerClass },
children: [this.nameNode],
events : this.contextEvents
});
if (BX.browser.IsIE())
{
this.buttonNode.setAttribute("hideFocus", "hidefocus");
}
};
BX.extend(window.DelayPopup.BasketButton, BX.PopupWindowButton);
window.DelayPopup.DelayRedirect = function(){
window.location.href = "/personal/cart/?delay=Y";
}
window.DelayPopup.CompareRedirect = function(){
window.location.href = "/catalog/compare/";
}
function AddToDelayResult(result){
var popupContent, popupButtons, popupTitle;
if (!!window.DelayPopup.obPopupWin)
{
window.DelayPopup.obPopupWin.close();
}
if (typeof result !== 'object')
{
return false;
}
window.DelayPopup.InitPopupWindow();
popupTitle = {
content: BX.create('h2', {
style: { marginRight: '30px', whiteSpace: 'nowrap' },
text: BX.message('DELAY_TITLE')
})
};
if (result.STATUS === 'OK')
{
BX.onCustomEvent('OnBasketChange');
popupContent = '<div style="width: 96%; margin: 10px 2%; text-align: center;"><p>'+BX.message('DELAY_MESSAGE_OK')+'</p></div>';
popupButtons = [
new window.DelayPopup.BasketButton({
ownerClass: "bx_item_list_bestsellers bx_black delay",
text: BX.message('BTN_MESSAGE_DELAY_REDIRECT'),
events: {
click: BX.delegate(window.DelayPopup.DelayRedirect, this)
},
}),
new window.DelayPopup.BasketButton({
ownerClass: "bx_item_list_bestsellers bx_black delay",
text: BX.message('BTN_MESSAGE_CLOSE_POPUP'),
events: {
click: BX.delegate(window.DelayPopup.obPopupWin.close, window.DelayPopup.obPopupWin)
}
})
];
}
window.DelayPopup.obPopupWin.setTitleBar(popupTitle);
window.DelayPopup.obPopupWin.setContent(popupContent);
window.DelayPopup.obPopupWin.setButtons(popupButtons);
window.DelayPopup.obPopupWin.show();
}
function AddToDelayList(obj_name, Data){
var OBJ = window[obj_name];
if(OBJ.productType == 3){ // sku
Data[Data.arParams.PRODUCT_ID_VARIABLE] = OBJ.offers[OBJ.offerNum].ID;
}
$.ajax({
url: "/ajax/",
type: "POST",
dataType: "html",
data: Data,
cache: false,
}).done(function(result)
{
result = eval("("+result+")");
AddToDelayResult(result);
});
}
var delay_popup = null;
$(function() {
delay_popup = new BX.PopupWindow('popup', null, {
content: '',
closeIcon: {top: '10px', right: '10px'},
closeByEsc: true,
titleBar: {
content: BX.create('div', {html: 'Товар добавлен в отложенные', props: {style: 'margin-right:30px; white-space:nowrap;'}}),
},
overlay : true,
offsetLeft: 0,
offsetTop: 0,
buttons: [
new BX.PopupWindowButtonLink({
text: 'Перейти в отложенные',
className: 'bx_medium bx_bt_button margin-right-10',
events: {
click: function() {
location.href = '/personal/cart/?delay=Y';
}
}
}),
new BX.PopupWindowButtonLink({
text: 'Продолжить покупки',
className: 'bx_medium bx_bt_button',
events: {
click: function() {
this.popupWindow.close();
}
}
})
]
});
});
function AddToDelayList(obj_name, Data, elem){
var OBJ = window[obj_name];
if(OBJ.productType == 3){ // sku
Data[Data.arParams.PRODUCT_ID_VARIABLE] = OBJ.offers[OBJ.offerNum].ID;
}
var that = $(elem);
if(that.find('.favorite_button_icon').hasClass('active')){
location.href = '/personal/cart/?delay=Y';
}else{
$.ajax({
url: "/ajax/",
type: "POST",
dataType: "html",
data: Data,
cache: false,
}).done(function(result)
{
result = eval("("+result+")");
if (typeof result !== 'object'){
return false;
}
if (result.STATUS === 'OK'){
BX.onCustomEvent('OnBasketChange');
that.find('.favorite_button_icon').addClass('active');
delay_popup.setContent(
'<div style="width: 96%; margin: 10px 2%; text-align: center;">' +
"<p>Товар добавлен в список отложенных товаров </p>"+
'</div>'
);
delay_popup.show();
}
});
}
}
function AddToDelayList(obj_name, Data, elem){
var OBJ = window[obj_name];
if(OBJ.productType == 3){ // sku
Data[Data.arParams.PRODUCT_ID_VARIABLE] = OBJ.offers[OBJ.offerNum].ID;
}
var that = $(elem);
if(that.find('.favorite_button_icon').hasClass('active')){
//location.href = '/personal/cart/?delay=Y';
Data.ajax_action="RemoveFromDelay";
$.ajax({
url: "/ajax/",
type: "POST",
dataType: "html",
data: Data,
cache: false,
}).done(function(result)
{
BX.onCustomEvent('OnBasketChange');
that.find('.favorite_button_icon').removeClass('active');
that.find('.favorite_button_icon').attr('title','Отложить');
});
}else{
$.ajax({
url: "/ajax/",
type: "POST",
dataType: "html",
data: Data,
cache: false,
}).done(function(result)
{
result = eval("("+result+")");
if (typeof result !== 'object'){
return false;
}
if (result.STATUS === 'OK'){
BX.onCustomEvent('OnBasketChange');
that.find('.favorite_button_icon').addClass('active');
that.find('.favorite_button_icon').attr('title','Отложено');
/*
delay_popup.setContent(
'<div style="width: 96%; margin: 10px 2%; text-align: center;">' +
"<p>Товар добавлен в список отложенных товаров </p>"+
'</div>'
);
delay_popup.show();
*/
}
});
}
}
<?
foreach($arResult['ITEMS'] as $ITEM){
$arResult['AJAX_POST_DATA'][$ITEM['ID']] = AddToCartAjaxData($arParams,$ITEM); // init.php
}
?>
<a href="javascript:void(0)" onclick="AddToDelayList('<?=$strObName?>',<?echo CUtil::PhpToJSObject($arResult['AJAX_POST_DATA'][$arItem['ID']]);?>)" class="two">в закладки</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment