Ajax field promo code on any Bitrix page using D7
The last notes
All English-language materials have been translated fully automatically using the Google service
Sometimes we are required to organize the promo code field (coupon input) in an arbitrary place or add the result of processing the promo code in a pop-up form. In these cases, we need to dynamically send a request and, depending on the result, perform an action on the page. I will tell you how to organize this interaction
On the test page, add a coupon input field and an "Apply" button.
<label class = "coupon"> Promo code </label> </br>
<input id = "basket-coupon" type = "text" name = "ONE_CLICK_BUY_coupon" value = "" />
<button class = "basket-coupon-block-coupon-btn"> Apply </button>
</br>
<span class = "answer"> </span>
Add a js script:
$ ('body'). on ('click', '. basket-coupon-block-coupon-btn', function (e) {
e.preventDefault ();
e.stopPropagation ();
var coupon = $ ('# basket-coupon'). val ();
$ .ajax ({
url: "/include/libs/addcoupon.php", // where to send
type: "post", // transfer method
dataType: "json", // data transfer type
data: {
"coupon": coupon
},
success: function (data) {
$ ('. popupCoupon h2'). html (data.title);
$ ('. popupCoupon .subtitle'). html (data.result);
formLoad ('popupCoupon');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log ("Status:" + textStatus); alert ("Error:" + errorThrown);
}
});
});
Create file /include/oneclick_addcoupon.php
:
require_once ($ _ SERVER ['DOCUMENT_ROOT']. '/ bitrix / modules / main / include / prolog_before.php');
if (CModule :: IncludeModule ("sale"))
{// If there are no items in the cart, then the coupon will not apply, so we check if there are items in the cart
$ arBasketItems = array ();
$ dbBasketItems = CSaleBasket :: GetList (
array ("NAME" => "ASC", "ID" => "ASC"),
array ("FUSER_ID" => CSaleBasket :: GetBasketUserID (), "LID" => SITE_ID, "ORDER_ID" => "NULL"),
false,
false,
array ("ID", "MODULE", "PRODUCT_ID", "QUANTITY", "CAN_BUY", "PRICE"));
while ($ arItems = $ dbBasketItems-> Fetch ())
{
$ arItems = CSaleBasket :: GetByID ($ arItems ["ID"]);
$ arBasketItems [] = $ arItems;
$ cart_num + = $ arItems ['QUANTITY'];
}
}
$ msg_box = $ msg_title = "";
$ errors = array (); // container for errors
if ($ _ POST ['coupon'] == "") {$ errors [] = "Coupon code not entered";} // check if the field is correct
if ($ cart_num <= 1) {$ errors [] = "Add item to cart";}
// if the field is filled
if (empty ($ errors)) {
if (CModule :: IncludeModule ("sale") && CModule :: IncludeModule ("catalog")) {
$ coupon = $ _POST ['coupon']; // coupon number
$ couponinfo = \ Bitrix \ Sale \ DiscountCouponsManager :: getData ($ coupon, true); // get information about the coupon
if ($ couponinfo ['ACTIVE'] == "Y") {
$ addCoupon = \ Bitrix \ Sale \ DiscountCouponsManager :: add ($ coupon); // true - there is a coupon / false - there is no coupon
if ($ addCoupon) {
$ msg_box = "Promo code activated";
$ msg_title = "Promo code successfully activated";
} else {
$ msg_box = "Promo code has already been activated";
$ msg_title = "Invalid promo code";
}
} else if (! $ couponinfo ['ACTIVE']) {
$ msg_box = "There is no such promo code";
$ msg_title = "Invalid promo code";
} else {
$ msg_box = "Error activating promo code";
$ msg_title = "Invalid promo code";
}
}
} else {
// if there were errors, then display them
$ msg_box = "";
foreach ($ errors as $ one_error) {
$ msg_box. = "$ one_error";
}
$ msg_title = "Invalid promo code";
}
// make a response to the client side in JSON format
echo json_encode (array (
'result' => $ msg_box,
'title' => $ msg_title
));
Based on the material:
Comments