0

JS is not my forte but there's something that needs done. The GTM code below is FB-related and takes price from the website. Problem is, the prices use a comma and they need a full stop.

How to adjust the code below so it swaps one for another?

<script>
  requirejs(['jquery'], function( jQuery ) {
    jQuery(".c-btn--pdp").click(function() {
       if(jQuery(".c-product-size__selected-option").text() != ""){
            var sku = jQuery(".product-info-stock-sku").find(".value").text();
            var price = jQuery("[data-price-type='finalPrice']").find(".price").text().substring(2);

            console.log("Added Product SKU: " + sku);
            console.log("Added Product Price: " + price);

            fbq('track', 'AddToCart', {
            content_ids: sku,
            content_type: 'product',
            value: price,
            currency: {{Currency}}
            });
        }
    });
  });
</script>

3 Answers3

0

You can use split and join

  Query(".c-product-size__selected-option").text() != "") {
  var sku = jQuery(".product-info-stock-sku").find(".value").text();
  var price = jQuery("[data-price-type='finalPrice']").find(".price").text().substring(2);
  price = price.split(',').join('.')
  console.log("Added Product SKU: " + sku);
  console.log("Added Product Price: " + price);
kooskoos
  • 4,622
  • 1
  • 12
  • 29
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

Use replace:

value = price.replace(/,/g,'.')

 let price = "12,300"
 console.log(price.replace(/,/g,'.'))
kooskoos
  • 4,622
  • 1
  • 12
  • 29
0

You have to ways:

str.split(search).join(replacement)

str.replace(/search/g, "replacement");

Using /g with the string will replace all the strings.

Asif
  • 227
  • 3
  • 5