Reputation: 434
I have an add to cart button that shows the price of the item. I only want it to show 'Add to Cart'. I've been trying to remove the price, but everytime I do some other elements on the page stop working.
This is the code that's responsible for the price showing up on the button:
// Enabling add to cart button.
var addBtnText = Shopify.formatMoney(variant.price, {{ shop.money_format | json }});
if ( variant.compare_at_price > variant.price ) {
jQuery('#add').addClass('on-sale');
addBtnText += ' <span class="pre-sale-price">' + Shopify.formatMoney(variant.compare_at_price, {{ shop.money_format | json }}) + '</span>';
} else {
jQuery('#add').removeClass('on-sale');
}
addBtnText += ' <span class="div">|</span> {{ settings.txt_add | escape }}';
jQuery('#add').removeClass('disabled').removeAttr('disabled').html(addBtnText);
The first {{ shop.money_format | json }} seems to be important, because removing that removes all jquery from the page.
So is there a way for me to use the above code so the cart doesn't show the variant.price at all and only says 'Add to Cart' (and 'Added to Cart' after clicked).
Thanks for any help!
Upvotes: 0
Views: 849
Reputation: 11682
Removing the first line will cause problems because it defines the addBtnText
variable which is referred to further down in the code. If you just want to display "Add to Cart" on the button, you can do this in html:
<input type="submit" id="add" name="add" value="Add to Cart" class="btn" />
And change the value on click with jQuery:
jQuery('#add').click(function() {
jQuery('#add').val('Added to Cart');
});
Or if you want to reuse your code snippet to load the button text from settings, you could try something like this:
var addBtnText = '{{ settings.txt_add | escape }}';
jQuery('#add').html(addBtnText);
jQuery('#add').click(function() {
jQuery('#add').html('Added to Cart');
});
Upvotes: 2