Search code examples
phpwordpresswoocommercecountcart

Get the line cart items count in Woocommerce


I'm trying to find a way of getting the unique cart count from a woocommerce basket. After looking at the documentation it only shows a way to get the total count

get_cart_contents_count( )

Does anyone know of a way to get the unique count. For example if I add 50 pieces of product A and 20 pieces of product B, I just want the cart count to be 2


Solution

  • This will make the trick (counting cart line items):

    echo '<p>Line cart items count: ' . count( WC()->cart_get_cart() ) . '</p>';
    

    Tested and works.


    For line order items count it will be (from the WC_Order $order object):

    echo '<p>Line order items count: ' . count( $order->get_items() ) . '</p>';