Created
September 1, 2016 00:08
-
-
Save lulessa/3b5214b33e98f996afe1fb9bc45826b0 to your computer and use it in GitHub Desktop.
Wishl (app for Shopify) javascript to modify prices on wishlist page
This file contains hidden or 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
// Execute the function after wish list has completed loading. | |
$('#wishl-wrapper').on('listcomplete.wishl', function() { | |
// Change transformPrice function return value to suit your needs. | |
// Return value will directly replace item price on page. | |
// In this example, we add 25% to the price (multiply by 1.25) | |
// and move reformat it using Shopify's formatMoney function, | |
// which reflects the money format preferences of the current shop. | |
// If there is no price, or it is 0, transformPrice is not called. | |
var transformPrice = function(priceInCents) { | |
// priceInCents 10025 equals $100.25 price, 2500 is $25, and so on. | |
return Shopify.formatMoney(priceInCents * 1.25); | |
}; | |
$('.wishl-item-price').each(function(index, element) { | |
var $this = $(this), $del = $('del', this); | |
// Detach <del> tag (crossed-out price) | |
$del = $del.detach(); | |
// Reformat price and <del> tag price | |
$this.add($del).html(function(index, oldHtml) { | |
// Strip any non-digit characters | |
var originalPriceInCents = +oldHtml.replace(/\D/g, ''); | |
if (originalPriceInCents) { | |
return transformPrice(originalPriceInCents); | |
} else { | |
return oldHtml; | |
} | |
}); | |
// Reattach <del> tag | |
$this.append(' ', $del); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wishl loads the wishlist items dynamically, therefore you must use jQuery/javascript to change the price after the page loads.
When all wishlist items on the page are done loading, your prices will be transformed (almost) immediately.