Change price of the products.

jertixjertix Member
in Help edited June 2013
Hi, In my e-commerce i have prices including VAT, I'm trying to remove the VAT for sales outside of Europe, so I'm trying this code:

function updateMyShippingCost() {

var isOutsideEurope = false;
var myVAT = 20;

//... my logic is here ... (change isOutsideEurope in true)

for (var p in fc_json.products) {
if (isOutsideEurope == true) {
//remove VAT (myVAT = 20)...
var totalnet = ((fc_json.products[p].price_each * fc_json.products[p].quantity)/(1+(myVAT/100))).toFixed(2);
fc_json.products[p].price = totalnet;
fc_json.products[p].base_price = totalnet;
fc_json.products[p].price_each = totalnet;

}
}
}


function capturekey(e){
var key=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
var myCountry = document.getElementById("customer_country_name");

FC.checkout.config.orderFlatRateShipping = updateMyShippingCost();
FC.checkout.updateShipping(-1);

// Update shipping cost based on categories and country when updating the price
FC.checkout.overload("updatePriceDisplay", "updateMyShippingCost", null);
FC.checkout.updatePrice(-1);
FC.checkout.updatePriceDisplay(-1);

}

- but it does not work, where am I wrong?
Comments
  • fc_adamfc_adam FoxyCart Team
    @jertix,

    Unfortunately you can't change the product by modifying the fc_json object - it's a read only object. You'll need to use JSONP to remove the existing product and re-add it with the updated properties you're after. More information on that here: http://wiki.foxycart.com/static/redirect/jsonp
  • jertixjertix Member
    edited June 2013
    Hi, Adam

    I have tried to remove the products and remove the TAX, then I tried to re-add the products by using JSONP, but fc_json never updates, what am I doing wrong?

    I have highlighted in bold the rows of code used for this.

    // Global vars..
    var isOutsideEurope = false;
    var myVAT = 20;


    function myFunc() {

    //... my logic is here ... (change isOutsideEurope in true)

    console.clear();
    console.log('START -- DEBUG');
    console.log($("#customer_country_name").val() + ' is outside Europe = ' + isOutsideEurope);

    if (isOutsideEurope == true) {
    jQuery.each(fc_json.products, function(i, product) { RemoveVAT(product); });
    } else {
    //jQuery.each(fc_json.products, function(i, product) { AddVAT(product); });
    }

    //Refresh the cart?
    FC.checkout.updatePrice(-1);
    FC.checkout.updatePriceDisplay(-1);

    }

    function RemoveVAT(product) {
    var NetBasePrice = ((product.base_price)/(1+(myVAT/100))).toFixed(2);

    var tmpBasePrice = NetBasePrice;
    var tmpPriceEach = NetBasePrice;
    var tmpPrice = NetBasePrice * product.quantity;

    var OriginalProduct = product;
    var cart_remove_url='https://mycart.foxycart.com/cart?&output=json&cart=update&id=' + product.id + '&quantity=0';

    $.ajax({
    dataType: 'jsonp',
    type: "POST",
    url: cart_remove_url,
    //async: false,
    success: function() { console.log('Product ' + product.id + ' Removed!');}
    });

    //add to cart
    var cart_add_url='https://mycart.foxycart.com/cart?&output=json&cart=add&id=' + OriginalProduct.id;
    cart_add_url = cart_add_url + '&name=' + OriginalProduct.name;
    cart_add_url = cart_add_url + '&code=' + OriginalProduct.code;
    cart_add_url = cart_add_url + '&image=' + OriginalProduct.image;
    cart_add_url = cart_add_url + '&url=' + OriginalProduct.url;
    cart_add_url = cart_add_url + '&length=' + OriginalProduct.length;
    cart_add_url = cart_add_url + '&width=' + OriginalProduct.width;
    cart_add_url = cart_add_url + '&height=' + OriginalProduct.height;
    cart_add_url = cart_add_url + '&options=' + OriginalProduct.options;
    cart_add_url = cart_add_url + '&quantity=' + OriginalProduct.quantity;
    cart_add_url = cart_add_url + '&quantity_min=' + OriginalProduct.quantity_min;
    cart_add_url = cart_add_url + '&quantity_max=' + OriginalProduct.quantity_max;
    cart_add_url = cart_add_url + '&base_price=' + tmpBasePrice;
    cart_add_url = cart_add_url + '&price_each=' + tmpPriceEach;
    cart_add_url = cart_add_url + '&price=' + tmpPrice;
    cart_add_url = cart_add_url + '&weight_each=' + OriginalProduct.weight_each;
    cart_add_url = cart_add_url + '&weight=' + OriginalProduct.weight;
    cart_add_url = cart_add_url + '&shipto=' + OriginalProduct.shipto;
    cart_add_url = cart_add_url + '&category=' + OriginalProduct.category;
    cart_add_url = cart_add_url + '&delivery_type=' + OriginalProduct.delivery_type;
    cart_add_url = cart_add_url + '&sub_frequency=' + OriginalProduct.sub_frequency;
    cart_add_url = cart_add_url + '&sub_startdate=' + OriginalProduct.sub_startdate;
    cart_add_url = cart_add_url + '&sub_nextdate=' + OriginalProduct.sub_nextdate;
    cart_add_url = cart_add_url + '&sub_enddate=' + OriginalProduct.sub_enddate;

    $.ajax({
    dataType: 'jsonp',
    type: "POST",
    url: cart_add_url,
    //async: false,
    success: function() { console.log('Product ' + product.id + ' Added! VAT removed! Old price: ' + product.price + ' new price: ' + tmpBasePrice);}
    });
    }
  • fc_adamfc_adam FoxyCart Team
    @jertix,

    You need to capture the response from the jsonp request and update the JSON object yourself.

    So something like:
    success: function(cart) { fc_json = cart; }
    
  • jertixjertix Member
    edited June 2013
    Ok, thanks Adam, in the firebug bar now I see the object fc_json with the right prices, and if I press Refresh button the cart is ok, but there any function in FC.checkout to refresh the price of the products in the cart?

    this two functions don't refresh the prices..
    FC.checkout.updatePrice(-1);
    FC.checkout.updatePriceDisplay(-1);
    
  • fc_adamfc_adam FoxyCart Team
    @jertix,

    On the checkout, there isn't any functions that rebuild the cart - that is constructed when the page is loaded, rather than dynamically with javascript. You'll need to manually rebuild that yourself.
Sign In or Register to comment.