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?
JavaScript
x
2
1
var carttotal = WC()->cart->get_cart_contents_total();
2
document.getElementById("cart-progress-bar").value = carttotal;
JavaScript
1
13
13
1
#cart-progress-bar {
2
background-color: #f9f9f9;
3
box-shadow: inset 0 1px 1px rgba(100, 100, 100, 0.1);
4
box-sizing: initial;
5
color: #fff;
6
font-size: 20px;
7
height: 30px;
8
position: relative;
9
text-align: center;
10
width: auto;
11
margin: 10px;
12
border:1px solid grey;
13
}
JavaScript
1
1
1
<progress id="cart-progress-bar" max="100" value="0"></progress>
Advertisement
Answer
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:
JavaScript
1
22
22
1
add_action( 'woocommerce_before_cart_table', 'cart_discount_progress_bar' );
2
function cart_discount_progress_bar() {
3
4
// Create progress bar
5
$cart_total = WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax();
6
printf( '<progress id="cart-progress-bar" max="100" value="%s"></progress>', $cart_total );
7
8
// Create steps
9
$steps = array(
10
0 => '',
11
30 => __( '3% discount', 'discount-bar'),
12
50 => __( '8% discount', 'discount-bar'),
13
100 => __( '10% discount', 'discount-bar'),
14
);
15
echo '<div id="cart-progress-steps">';
16
foreach ( $steps as $step => $discount ) {
17
printf('<div class="step step-%1$s" style="left:%1$s%%">%2$s<span class="discount">%3$s</span></div>', $step, wc_price( $step ), $discount );
18
}
19
echo '</div>';
20
21
}
22
CSS:
JavaScript
1
54
54
1
.woocommerce-cart #cart-progress-bar {
2
border-radius:0;
3
border: 1px solid #eaeaea;
4
margin-top: 1em;
5
color: cornflowerblue;
6
background: floralwhite;
7
}
8
9
.woocommerce-cart #cart-progress-bar::-webkit-progress-value {
10
background: cornflowerblue;
11
}
12
13
.woocommerce-cart #cart-progress-bar::-webkit-progress-bar {
14
background: floralwhite;
15
}
16
17
.woocommerce-cart #cart-progress-steps {
18
margin-bottom: 3em;
19
}
20
21
.woocommerce-cart #cart-progress-bar,
22
.woocommerce-cart #cart-progress-steps {
23
width: 100%;
24
position: relative;
25
height: 20px;
26
box-sizing: border-box;
27
}
28
29
.woocommerce-cart #cart-progress-steps .step {
30
position: absolute;
31
width: 70px;
32
height: 20px;
33
font-size: 14px;
34
text-align: center;
35
transform: translateX(-50%);
36
}
37
38
.woocommerce-cart #cart-progress-steps .step .discount {
39
font-size: 12px;
40
line-height: 11px;
41
display: block;
42
}
43
44
.woocommerce-cart #cart-progress-steps .step:before {
45
content: '';
46
display: block;
47
position: absolute;
48
width: 40px;
49
height: 18px;
50
border-left: 2px solid black;
51
left: 50%;
52
top: -26px;
53
}
54
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:
JavaScript
1
2
1
var carttotal = <?php echo WC()->cart->get_cart_contents_total() + WC()->cart->get_cart_contents_tax(); ?>;
2
Or choose to build your whole progress bar in PHP like in my example.