Display cart item count in Woocommerce cart widget

2019-03-02 18:35发布

问题:

I am trying to display the count of the items in the cart at the bottom or top of the Woocommerce cart widget. It seems that by default behaviour the cart widget does not do this when in the sidebar.

I have researched SO and found the following answer, but it has a slightly different goal of adding individual subtotals. I have tried modifying it, by passing in WC()->cart->get_cart_contents_count() instead of WC()->cart->get_cart() to see if I can get the cart item count, but it's not displaying...any suggestions?

Example of what I am trying to do:

Code I have modified from the approved answer:

<?php
/**
 * Mini-cart
 *
 * Contains the markup for the mini-cart, used by the cart widget
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.1.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>

<?php do_action( 'woocommerce_before_mini_cart' ); ?>

<ul class="cart_list product_list_widget <?php echo $args['list_class']; ?>">

    <?php if ( WC()->cart->get_cart_contents_count() ) > 0 ) : ?>

        <?php
            foreach ( WC()->cart->get_cart_contents_count() as $cart_item_key => $cart_item ) {
                $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                $product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

                if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_widget_cart_item_visible', true, $cart_item, $cart_item_key ) ) {

                    $product_name  = apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key );
                    $thumbnail     = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
                    $product_price = apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                    echo "<p>".$product_price."</p>";
                    ?>
                    <li>
                    <?php if ( ! $_product->is_visible() ) { ?>
                        <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
                    <?php } else { ?>
                        <a href="<?php echo get_permalink( $product_id ); ?>">
                            <?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name; ?>
                        </a>
                    <?php } ?>
                        <?php echo WC()->cart->get_item_data( $cart_item ); ?>
                        <?php   $new_product_price_array = explode ( get_woocommerce_currency_symbol(), $product_price); 
                                $new_product_price = number_format((float)$new_product_price_array[1] * $cart_item['quantity'], 2, '.', '');

                        ?>
                        <?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s &times; %s=%s%s', $cart_item['quantity'], $product_price,get_woocommerce_currency_symbol(), $new_product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
                    </li>
                    <?php
                }
            }
        ?>

    <?php else : ?>

        <li class="empty"><?php _e( 'No products in the cart.', 'woocommerce' ); ?></li>

    <?php endif; ?>

</ul><!-- end product list -->

<?php if ( WC()->cart->get_cart_contents_count() ) > 0 ) : ?>

    <p class="total"><strong><?php _e( 'Subtotal', 'woocommerce' ); ?>:</strong> <?php echo WC()->cart->get_cart_subtotal(); ?></p>

    <?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>

    <p class="buttons">
        <a href="<?php echo WC()->cart->get_cart_url(); ?>" class="button wc-forward"><?php _e( 'View Cart', 'woocommerce' ); ?></a>
        <a href="<?php echo WC()->cart->get_checkout_url(); ?>" class="button checkout wc-forward"><?php _e( 'Checkout', 'woocommerce' ); ?></a>
    </p>

<?php endif; ?>

<?php do_action( 'woocommerce_after_mini_cart' ); ?>

回答1:

If you want to display the cart item count in the mini cart widget you can use hooks:

1) On top before minicart content:

add_action( 'woocommerce_before_mini_cart', 'minicart_count_after_content' );
function minicart_count_after_content() {
    $items_count = WC()->cart->get_cart_contents_count();
    $text_label  = _n( 'Item', 'Items', $items_count, 'woocommerce' );
    ?>
        <p class="total item-count"><strong><?php echo $text_label; ?>:</strong> <?php echo $items_count; ?></p>
    <?php
}

2) On the bottom before the buttons:

add_action( 'woocommerce_widget_shopping_cart_before_buttons', 'minicart_count_before_content' );
function minicart_count_before_content() {
    $items_count = WC()->cart->get_cart_contents_count();
    $text_label  = _n( 'Item', 'Items', $items_count, 'woocommerce' );
    ?>
        <p class="total item-count"><strong><?php echo $text_label; ?>:</strong> <?php echo $items_count; ?></p>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


WC()->cart->get_cart_contents_count(); will give you the total items count including quantities.

If you want to get the number of items in cart you will use instead:

$items_count = sizeof( WC()->cart->get_cart() );


回答2:

You aren't echoing WC()->cart->get_cart_contents_count() anywhere, so no, it won't display.

Wherever in the code you need to display the count, you'll need to use

echo WC()->cart->get_cart_contents_count();