Last active
January 16, 2018 17:25
-
-
Save guilhermemarconi/64bca946e4accc2eda603746d5e599dd to your computer and use it in GitHub Desktop.
Este script não escreve nada no DOM. Apenas retorna o preço formatado no console. É preciso pegar o resultado desse cálculo e imprimir no DOM, no lugar certo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax('/api/catalog_system/pub/products/search?fq=productId:' + skuJson.productId) | |
.done(function(product) { | |
product = product[0]; | |
if (!product['Desconto progressivo'][0]) return; | |
/** | |
* Pega a porcentagem de desconto, de acordo com a descrição da promoção | |
* Ex.: "<b>GANHE 10% DE DESCONTO</b> COLOCANDO <b>12 UNIDADES</b> OU MAIS NO CARRINHO." | |
*/ | |
var discountBase = +product['Desconto progressivo'][0].match(/\d+\%/g)[0].replace('%',''); | |
// Pega o preço do SKU | |
var price = product.items[0].sellers[0].commertialOffer.Price; | |
var discountPrice = (price - (price / discountBase)).toFixed(2).replace('.',','); | |
var formattedPrice = 'R$ ' + discountPrice; | |
console.log(formattedPrice); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment