Out now! Check out the Poll results: Do you have a Shopify store?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Adding price to add to cart button in Impact theme

Solved

Adding price to add to cart button in Impact theme

TenPM
Tourist
3 0 1

Hi

I'm looking to add the price of a selected variant to the add to cart button.  We are using the Impact theme and also use recharge subscriptions

 

Url of the product is here - https://tenpm.co/products/the-night-drink

 

Any advice or help would be greatly appreciated!

Accepted Solutions (2)

Huptech-Web
Shopify Partner
1010 204 217

This is an accepted solution.

Hi @TenPM 
To achieve this, Add the below code to theme.liquid

 

<script>
window.addEventListener('load', function() {

    function updateAddToCartButton() {

        const selectedRadio = document.querySelector('.rc-radio__input:checked');
        
        if (selectedRadio) {

            const radioContainer = selectedRadio.closest('.rc-radio');

            const priceLabel = radioContainer.querySelector('.price-label');
            
            if (priceLabel) {

                const price = priceLabel.innerHTML;


                const addToCartButton = document.querySelector('.button[is="custom-button"] div');


                addToCartButton.innerHTML = "Add to cart"; 


                addToCartButton.innerHTML += ` - ${price}`;
            }
        }
    }


    updateAddToCartButton();


    document.querySelectorAll('.rc-radio').forEach(function(radio) {
        radio.addEventListener('click', function(event) {
            updateAddToCartButton();
        });
    });
});
</script>

 

 

Thank you

D.P.

 

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required

View solution in original post

Huptech-Web
Shopify Partner
1010 204 217

This is an accepted solution.

Hi @TenPM 
To achieve this where there are no variants add below code and also keep the previous code

<script>
window.addEventListener('load', function() {

    var salePriceElement = document.querySelector('sale-price');
    var regularPriceElement = document.querySelector('compare-at-price');
    

    var priceToDisplay;
    
    if (salePriceElement && salePriceElement.childNodes.length > 1) {
    
        priceToDisplay = salePriceElement.childNodes[salePriceElement.childNodes.length - 1].textContent.trim();
    } else if (regularPriceElement && regularPriceElement.childNodes.length > 1) {
    
        priceToDisplay = regularPriceElement.childNodes[regularPriceElement.childNodes.length - 1].textContent.trim();
    } else {
    
        priceToDisplay = 'Price not available';
    }


    var addToCartButton = document.querySelector('button[type="submit"].button.button--xl');


    if (addToCartButton) {
        var addToCartText = addToCartButton.querySelector('div').textContent.trim();
        addToCartButton.querySelector('div').textContent = `${addToCartText} - ${priceToDisplay}`;
    }
});
</script>
If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required

View solution in original post

Replies 4 (4)

Huptech-Web
Shopify Partner
1010 204 217

This is an accepted solution.

Hi @TenPM 
To achieve this, Add the below code to theme.liquid

 

<script>
window.addEventListener('load', function() {

    function updateAddToCartButton() {

        const selectedRadio = document.querySelector('.rc-radio__input:checked');
        
        if (selectedRadio) {

            const radioContainer = selectedRadio.closest('.rc-radio');

            const priceLabel = radioContainer.querySelector('.price-label');
            
            if (priceLabel) {

                const price = priceLabel.innerHTML;


                const addToCartButton = document.querySelector('.button[is="custom-button"] div');


                addToCartButton.innerHTML = "Add to cart"; 


                addToCartButton.innerHTML += ` - ${price}`;
            }
        }
    }


    updateAddToCartButton();


    document.querySelectorAll('.rc-radio').forEach(function(radio) {
        radio.addEventListener('click', function(event) {
            updateAddToCartButton();
        });
    });
});
</script>

 

 

Thank you

D.P.

 

If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required
TenPM
Tourist
3 0 1

Hi D.P

thank you so much, this worked perfectly on the product I requested it for.  Can the code be tweaked so that on product pages without a radio button selector (e.g. https://tenpm.co/products/the-night-cookie-pack) then the price of the product is displayed as well in the add to cart button?

Thanks

Huptech-Web
Shopify Partner
1010 204 217

This is an accepted solution.

Hi @TenPM 
To achieve this where there are no variants add below code and also keep the previous code

<script>
window.addEventListener('load', function() {

    var salePriceElement = document.querySelector('sale-price');
    var regularPriceElement = document.querySelector('compare-at-price');
    

    var priceToDisplay;
    
    if (salePriceElement && salePriceElement.childNodes.length > 1) {
    
        priceToDisplay = salePriceElement.childNodes[salePriceElement.childNodes.length - 1].textContent.trim();
    } else if (regularPriceElement && regularPriceElement.childNodes.length > 1) {
    
        priceToDisplay = regularPriceElement.childNodes[regularPriceElement.childNodes.length - 1].textContent.trim();
    } else {
    
        priceToDisplay = 'Price not available';
    }


    var addToCartButton = document.querySelector('button[type="submit"].button.button--xl');


    if (addToCartButton) {
        var addToCartText = addToCartButton.querySelector('div').textContent.trim();
        addToCartButton.querySelector('div').textContent = `${addToCartText} - ${priceToDisplay}`;
    }
});
</script>
If you found this response helpful, please do like and accept the solution. Thanks!
Need support with Customizing your Shopify store?
Feel free to contact me at info@huptechweb.com or Visit our website Huptech Web.
Instant Shortcode Builder: Integrate customizable UI features anywhere in your store - No coding knowledge required
TenPM
Tourist
3 0 1

Fantastic - thank you, works perfectly.