I'm trying to display the total number of items a customer has in their cart on the checkout page.
I've read in the documentation that I need to use get_cart_contents_count
but when I try this it outputs 1 no matter how many items I have.
NB: " get_cart_contents_count( )
Get number of items in the cart. Returns integer"
The code I have is:
add_action( 'woocommerce_before_checkout_billing_form', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
$_cartQty = count( WC()->cart->get_cart_contents_count( ) );
echo $_cartQty;
}
Any help would be appreciated as I thought I had been quite clever getting to this stage.
You have a count which is making it not work - try this:
function my_custom_checkout_field( $checkout ) {
$_cartQty = WC()->cart->get_cart_contents_count( );
echo $_cartQty;
}
If I was you I would change it to:
function my_custom_checkout_field( $checkout ) {
return WC()->cart->get_cart_contents_count();
}
I would never echo from a function like this - in general combining php and HTML like that is not any good.
I would <?= $class->my_custom_checkout_field() ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.