Skip to content

Instantly share code, notes, and snippets.

@sleemanj
Last active January 9, 2020 04:39
Show Gist options
  • Save sleemanj/a56415ef7c9b8983888a7d7ec8686eb2 to your computer and use it in GitHub Desktop.
Save sleemanj/a56415ef7c9b8983888a7d7ec8686eb2 to your computer and use it in GitHub Desktop.
Tampermonkey for Aliexpress order details page to amortise the shipping, discount and tax into the item price
// ==UserScript==
// @name Adjust order details to amortize shipping, discount and tax into item prices
// @namespace https://gist.githubusercontent.com/sleemanj/a56415ef7c9b8983888a7d7ec8686eb2
// @updateURL https://gist.githubusercontent.com/sleemanj/a56415ef7c9b8983888a7d7ec8686eb2/raw/aliexpress-amortise-shipping-discount-tax.js
// @downloadURL https://gist.githubusercontent.com/sleemanj/a56415ef7c9b8983888a7d7ec8686eb2/raw/aliexpress-amortise-shipping-discount-tax.js
// @version 1.0
// @description Adjust order details to amortize shipping, discount and tax into item prices
// @author James Sleeman
// @match *://trade.aliexpress.com/order_detail.htm*
// @grant GM_addStyle
// ==/UserScript==
(function(){
// Leave null to calculate tax rate, or set to, eg 0.15 for 15%
var taxRate = null;
// Sum how many items are in the order
var totalItems = 0;
Array.from(document.getElementsByClassName('price')).forEach( function(i){
if(i.tagName.toLowerCase() == 'td')
{
var qty = parseFloat(i.parentNode.querySelector('.quantity').title);
totalItems += qty;
}
});
// The item price and shipping price are BEFORE discount (financial tab)
var preDiscountProducts =
parseFloat(document.querySelector('td.product-price').innerText.replace(/[^0-9.]+/, ''));
var preDiscountShipping =
parseFloat(document.querySelector('td.shipping-price').innerText.replace(/[^0-9.]+/, ''));
var preDiscountProductPlusShipping = preDiscountProducts + preDiscountShipping;
// The tax is calculated after the discount (financial tab)
var totalTax =
parseFloat(document.querySelector('td.tax-price').innerText.replace(/[^0-9.]+/, ''));
// as is the total (includes tax, this is the amount actually paid) (financial tab)
var finalTotal =
parseFloat(document.querySelector('td.order-price').innerText.replace(/[^0-9.]+/, ''));
// Subtract the tax
var afterDiscountProductPlusShipping = finalTotal - totalTax;
var discountCalculated = preDiscountProductPlusShipping - afterDiscountProductPlusShipping;
// Work out how much we need to reduce that by
var reductionPercent = afterDiscountProductPlusShipping / preDiscountProductPlusShipping;
// Work that forward
var afterDiscountShipping = preDiscountShipping * reductionPercent;
var afterDiscountProducts = preDiscountProducts * reductionPercent;
if(taxRate === null)
{
taxRate = (totalTax / (afterDiscountShipping + afterDiscountProducts));
console.log("Calculated Tax Rate: " + taxRate);
}
console.log("Total Item Count: " + totalItems);
console.log("Discount (calculated): " + discountCalculated.toFixed(2));
console.log("After discount products: " + afterDiscountProducts.toFixed(2));
console.log("After discount shipping: " + afterDiscountShipping.toFixed(2));
console.log("After discount product + shipping: " + (afterDiscountShipping + afterDiscountProducts).toFixed(2));
console.log("After discount inc Tax (calculated): " + ((afterDiscountShipping + afterDiscountProducts)*(1+taxRate)).toFixed(2));
// Record adjusted GST inclusive product+shipping item totals on the
var crossCheckTotal = 0;
Array.from(document.getElementsByClassName('price')).forEach( function(i){
if(i.tagName.toLowerCase() == 'td')
{
var qty = parseFloat(i.parentNode.querySelector('.quantity').title);
var unit = parseFloat(i.title);
var adjustedUnit = ((unit * reductionPercent) + (afterDiscountShipping / totalItems)) * (1+taxRate);
crossCheckTotal += adjustedUnit * qty;
i.innerHTML += '<div title="Including Tax and Amortised Shipping">('+adjustedUnit.toFixed(3)+')</div>';
}
});
console.log("Crosscheck Total: " + crossCheckTotal.toFixed(2) + " (this should be the same as the total actually paid)");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment