Change Price of Catalog Item using Catalog Client Script

santoshsahoonis
Kilo Guru

Hello,

I need to change the price in the cart using catalog client script.
I am using a text box to enter a value in it, and this value gets added to the price in the cart.

Just let me know how to set the price using catalog client script.

Thanks,
santosh

13 REPLIES 13

jamesmcwhinney
Giga Guru

Hi Guys,


Here is a workaround I came up with:


How to create a catalog item with a user specified cost


Subhadip Saman1
Mega Expert

Recently I came across the same requirement. I hope this work around will be of some help to someone.

The trick is to update "Price if checked" field of a checkbox variable with the price entered by the user in the catalog form and using a onChange client script to select the checkbox after the user is done entering the price.

 

1. Add  a checkbox variable to the catalog item.

2. Using a UI policy hide the checkbox.

3. Add another single text field named "Price". This is the field where user enters the price.

4. Create a script include and paste the below function into it. Mark the script include as Client callable.

changePrice: function() {
        var newPrice = this.getParameter("sysparm_new_price");
        //the variable should always be of type check box
        var variable = this.getParameter("sysparm_variable");
        //gs.log("price : " + newPrice + " " + typeof newPrice);
        var gr = new GlideRecord("item_option_new");
        if(gr.get(variable)) {
            gr.price_if_checked = '' + newPrice;
            var flag = gr.update();
            if(flag) return true;
        }
        return false;
    }

5. Add an onChange client script to the catalog item and add the below script to it. NOTE: change the <variable names> and <sys_id> accordingly in the script with the variable name and sys_id of the hidden checkbox defined in the catalog item. Also, change the name of the price field in the script below. Please check the comments for more info.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
//replace with the name of the hidden check box below.
        g_form.setValue("<variable_name>", false);
        return;
    }
//replace with the name of the hidden check box below.
    g_form.setValue("hidden_price", false);
//replace with the name of the script include you defined.
    var ga = new GlideAjax("ChangePriceDynamically");
    ga.addParam("sysparm_name", "changePrice");
//if you want to change the currency replace USD with other currency's short hand. Also, change the name of the price field.
    ga.addParam("sysparm_new_price", "USD;" + g_form.getValue("<price_field_name>"));
//replace the sys_id with the sys id of the hidden checkbox variable.
    ga.addParam("sysparm_variable", "<sys_id>");
    ga.getXMLAnswer(callback);
    //Type appropriate comment here, and begin script below
}


function callback(answer) {
    if(answer === 'true')
        g_form.setValue("hidden_price", true);
    else alert("Price couldn't be set");
}

6. Its done! Now try entering a price and check whether the price is getting dynamically changed as expected.

Hi @Subhadip Saman1,

 

Thank you for posting this. This is what I am trying to achieve exactly and have followed your steps however I am getting the error "Price couldn't be set".

 

Do you mind sharing your screenshots of your configuration?

RudhraKAM
Tera Guru