My aiming result is like this:
With the current code its not showing the cart total in the progress bar. What am I doing wrong?
And how can I integrate those steps/stacks?
var carttotal = WC()->cart->get_cart_contents_total();
document.getElementById("cart-progress-bar").value = carttotal;
#cart-progress-bar {
background-color: #f9f9f9;
box-shadow: inset 0 1px 1px rgba(100, 100, 100, 0.1);
box-sizing: initial;
color: #fff;
font-size: 20px;
height: 30px;
position: relative;
text-align: center;
width: auto;
margin: 10px;
border:1px solid grey;
}
<progress id="cart-progress-bar" max="100" value="0"></progress>
2
Answers
The main mistake is in your Javascript code in the first line:
which should be instead:
And also CSS
width
that needs to be100%
.So the code will be (using jQuery and changed
width
property to100%
):Your example didn’t state exactly where you wanted this. I’ve added it to the cart page via the
woocommerce_before_cart_table
action hook. Which would look like this after adding some extra CSS styling.You could also add this to a template file of course without the
add_action
/function
mark up.Action hook:
CSS:
The mistake in your example was that you were trying to use PHP to create a value for a JavaScript variable. So to make that work you would either have to jump in and out of PHP:
Or choose to build your whole progress bar in PHP like in my example.