Ajax field promo code on any Bitrix page using D7
Rus
Eng
Ajax поле промо-код на любой странице Битрикс с помощью D7

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

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number