Tips for refreshing CSS styles within the Shopify ajax cart

A client's Shopify store requires an ajax cart, and I'm utilizing the liquid-ajax-cart plugin for this purpose. My current task involves integrating a progress bar that indicates how close a user is to qualifying for free shipping. This progress bar is defined by the .free-shipping-bar element within the .minicart__header.

For this particular store, free shipping kicks in once the cart total reaches $100 (as specified by the liquid variable free_shipping_threshold_price). Ideally, I want the progress bar to be completely filled when the cart total equals or exceeds $100.

If the cart total is below $100, I aim to fill the bar proportionally (e.g., 40% filled for $40, 50% for $50, etc.).

While the HTML section updates correctly upon clicking "add to cart," I'm facing challenges with the css ({% style %}) not updating. Is there a way to ensure that the css code also refreshes via ajax?

<div class="minicart-wrapper">
    <div class="minicart-overlay" data-ajax-cart-toggle-class-button="js-ajax-cart-open"></div>
    <div class="minicart" data-ajax-cart-section>
        <div class="minicart__header">
            <h3 class="title">{{ 'general.cart.title' | t }}</h3>
            <div class="free-shipping-bar"></div>           
            <button class="close-button" data-ajax-cart-toggle-class-button="js-ajax-cart-open" type="button"></button>
        </div>
        <div class="minicart__items">
            <!-- items code -->
        </div>
        <div class="minicart__footer">
            <!-- footer code -->
        </div>
    </div>
</div>

{%- liquid
    assign free_shipping_threshold_price = 10000
    assign free_shipping_percent = cart.total_price | times: 100 | divided_by: free_shipping_threshold_price
-%}

{% style %}
.free-shipping-bar {
    position: relative;
    width: 100%;
    height: 3px;
    background-color: #c4c4c4;
}

.free-shipping-bar:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: {{ free_shipping_percent }}%;
    background-color: #005030;
    max-width: 100%;
}

{% endstyle %}


{% schema %}
{
    "name": "Ajax Cart",
    "settings": [
   
    ]
} 
{% endschema %}

Answer №1

Based on the information provided in the documentation, only the content within the data-ajax-cart-section element will be refreshed. This means that any CSS styles that need to be updated should be placed inside this element:

{%- liquid
    assign free_shipping_threshold_price = 10000
    assign free_shipping_percent = cart.total_price | times: 100 | divided_by: free_shipping_threshold_price
-%}

<div class="minicart-wrapper">
    <div class="minicart-overlay" data-ajax-cart-toggle-class-button="js-ajax-cart-open"></div>
    <div class="minicart" data-ajax-cart-section>
        <style>
            .free-shipping-bar:after { 
                width: {{ free_shipping_percent }}% !important; 
            }
        </style>
        <!-- Mini-cart code -->
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using a JavaScript function to swap out the contents of a div and encountering some unexpected behavior

I am encountering an issue while running a code that generates content using a function. The problem arises when I try to change the content onclick, as none of the CSS is applied again after the change. It's quite perplexing. Below is a generalized ...

Variations in functionality within the Google Chrome browser

This is the content of my HTML document. <BODY> <button id="b1" onclick="load()">Play</button> <canvas id="c1" onclick="f_clicked(this.id,1)"> Your browser does not support the HTML5 canvas tag. </canvas> ...

Tips for positioning an element in the center of a page using Bootstrap both horizontally and vertically

[Pardon my English] I recently managed to center my "logo" both horizontally and vertically. However, I am looking to have my logo shrink a bit when resizing the browser. Any suggestions on how to achieve this using bootstrap? Index.html <section> ...

Is there a way to develop a search script that can efficiently sift through the object using the provided script or a similar one?

$(document).ready(function(){ $.ajax({ dataType: 'jsonp', //data in jsonp contentType: "application/json; charset=utf-8", url: 'http://live.nhle.com/GameData/RegularSeasonScoreboardv3.jsonp', ...

Positioning SVGs within a parent container while maintaining responsiveness

How can I anchor an SVG overlay with a circle in the bottom right corner while expanding the rest of the SVG to fill the remaining area of the container without distorting the circle? I've been able to position the SVG in the bottom right corner, but ...

Discover the Color's Value in Relation to a Different Color

When it comes to my CSS, I like to use .scss to make as many variables as possible. It's easy to create a variable for one color, like $primary-color. From there, I want to work with different shades of that color, which I can easily pinpoint using Ph ...

Utilizing JavaScript to trigger the :hover pseudo-class and implement event transfers

Consider this situation: You have a scenario where two images are stacked on top of each other. The image with the highest z-index is transparent and handles click events, similar to Google's Map API, while the image below is for visual representatio ...

Tips for adjusting the menu format to prevent text overlapping with the color bars

I'm having trouble adjusting the spacing between the text of the menu items and the color bars. I've tried various methods but can't seem to get it right in both Chrome and IE11. .smenu a { display: block; color: black; ...

Use jQuery to insert items into the text box

When I drag li elements into the textbox, they are arranged in the middle. How can I arrange these elements at the beginning? I am using Foundation. Is there a possible issue? This is my jQuery code: <script type="text/javascript"> $(function( ...

Is Height Responsiveness a feature?

I'm currently designing a custom responsive WordPress theme, but I've encountered a minor issue. On my page, I have multiple boxes arranged side by side. Each box is responsive in both height and width and contains an image with overlaid text. I ...

Displaying the content of a Bootstrap 4 row on top of the navbar

Just starting out with Bootstrap and recreated a page layout similar to the starter template on the Bootstrap website. However, I'm facing an issue where the row content overlaps the navbar, despite using the align-items-center utility class on the ro ...

Issue with random pages where global header.php is not showing a specific image

I'm currently revamping a website that was originally developed using codeigniter. The backend is quite chaotic with no clear identifiers for anything. I recently updated the banner in the header.php file, adjusting the style to suit the requirements. ...

Developing a search results page in MongoDB, NodeJS, and Express with the integration of Ajax for real-time result updates

Hey there! I've been working on a search form that is validated using boostrapvalidator. Once the form passes validation, I utilize Ajax to send the query to the server. The response from the server triggers rewriting part of the search screen using A ...

Issues with angular animations malfunctioning

I am a beginner in AngularJS and I am struggling to add animations to my text and button. There are no errors showing up in the console, but I can't figure out what I am doing wrong. I want to use different animations for my text and button. I have in ...

What steps do I need to take to successfully retrieve JSON data from jQuery's AJAX request in an ASP.NET application?

$.ajax({ url : "Handler1.ashx", type : "GET", cache : false, data : { type : "refresh", point: [x:1,y:2] }); The value for "type" can also be set to "POST". When working within the context of "Handler1.ashx", I have acc ...

various types of font colors within the text box

If I have a text box with the content "eg. 'one'" Can I set the font color of "eg." to color:black and "'one'" to color:red? Below is the HTML code: <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" Text="eg.-one"> ...

Issues with Gulp Autoprefixer Functionality

I'm struggling to make Autoprefixer work with Gulp. Despite using opacity, gradients, and transforms in my CSS, I can't see any vendor prefixes being added. However, everything else seems to be functioning correctly. Below is my gulp file: var ...

Executing PHP scripts using Ajax

Check out the code snippet below: <?php //echo $this->Html->css(array('bootstrap', 'mark', 'style')); echo $this->Html->script(array('timer','swfobject','bootstrap.min.js')); // ...

Converting an Ajax response to a string for comparison with a JavaScript string

How can I convert an Ajax response into a plain text string? I'm storing the Ajax response in a global variable but when I compare it with a JavaScript string, even if they are equal, it returns false. Here is the code snippet: function checkUsn(){ ...

Are you utilizing the .Ajax() method in JQuery?

I've been working on a project that involves creating a website where users fill out a form, the data is sent to a PHP script, and the results are displayed in a div. I have been facing some challenges with this for a while now. Below is the code I ha ...