Display cart item count in Woocommerce cart widget - php

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 × %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">
<?php _e( 'View Cart', 'woocommerce' ); ?>
<?php _e( 'Checkout', 'woocommerce' ); ?>
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>

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() );

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();

Related

A copy of the Woocommerce Cart on another page is not showing the correct prices. How can I fix this?

We created an additional page on our webshop for some kind of 'quotation'. On that page we'd like to load the cart. The contents of the 'quotation' is basically the same as the cart content.
We tried copying all of the WooCommerce code from the template file cart.php.
Everything is showing correctly, except for the prices. Our quotation cart is showing the basic prices that are set up in the WooCommerce back-end.
But here's our problem: When adding a product to the cart, there are additional price settings which we custom coded by using some hooks, like cart item data that's being added.
All of our custom code works perfectly fine when opening the regular WooCommerce cart, but when showing that exact Cart on another page, prices are 'reset' to the regular starting prices.
The reason we copied the code from the cart.php template file is the ability to change the structure with additional css/html.
Here you can find the shortcode & complete function of our 'quotation' cart:
<?php function toon_winkelmand_offerte() {
do_action( 'woocommerce_before_cart_table' );?>
<form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
<div class="offerte_wrapper">
<?php
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
?>
<div class="offerte_item">
<?php
$_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 );
$product_qty = $cart_item['quantity'];
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<div class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail; // PHPCS: XSS ok.
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
?>
</div>
<div class="product-name" data-title="<?php esc_attr_e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' ' );
?>
<span class="offerte_qty"><?php echo ' x ';
echo $product_qty;
?>
</span>
<?php
} else {
echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
?>
<span class="offerte_qty"><?php echo ' x ';
echo $product_qty;
?>
</span>
<?php
}
do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key );
echo wc_get_formatted_cart_item_data( $cart_item );
?>
<div class="product_prijs">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
<span class="prijs_per_stuk"><?php echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
echo ' ';
printf(esc_html__('per stuk', 'astra-child'));
// print_r($_product);
?>
</span>
</div>
</div>
<div class="product-remove">
<?php
echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
'woocommerce_cart_item_remove_link',
sprintf(
'<img src="https://kreatixlabs.be/rivanco/wp-content/uploads/2021/08/trash.png">',
esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
esc_html__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
),
$cart_item_key
);
?>
</div>
<?php
}
?>
</div>
<?php
}
?>
</div>
</form>
<?php
}
add_shortcode('toon_winkelmand_offerte', 'toon_winkelmand_offerte');?>

How to change woocommerce mini cart total (based on product name instead of quantity)?

as per title mention, right now the minicart is showing total based on the total quantity of the products, but not product name (ID). I assume it should be changed under "mini-cart.php" (correct me if I'm wrong). Can anyone tell me how and where to change this code? Thanks
refer here: https://shop.cheesang.com/wp-content/uploads/2019/09/sample.jpg
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
* will need to copy the new files to your theme to maintain compatibility. We try to do this.
* as little as possible, but it does happen. When this occurs the version of the template file will.
* be bumped and the readme will list any important changes.
*
* #see http://docs.woothemes.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php do_action( 'woocommerce_before_mini_cart' ); ?>
<ul class="cart_list products-list product_list_widget <?php echo $args['list_class']; ?>">
<?php if ( ! WC()->cart->is_empty() ) : ?>
<?php
foreach ( WC()->cart->get_cart() 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 );
?>
<li>
<?php if ( ! $_product->is_visible() ) : ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
<?php else : ?>
<a class="title" href="<?php echo esc_url( $_product->get_permalink( $cart_item ) ); ?>">
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
</a>
<?php endif; ?>
<?php echo WC()->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
<?php echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf( '<i class="fa fa-times-circle"></i>', esc_url( WC()->cart->get_remove_url( $cart_item_key ) ), __( 'Remove this item', 'snssimen' ) ), $cart_item_key ); ?>
</li>
<?php
}
}
?>
<?php else : ?>
<li class="empty"><?php _e( 'No products in the cart.', 'snssimen' ); ?></li>
<?php endif; ?>
</ul><!-- end product list -->
<?php if ( sizeof( WC()->cart->get_cart() ) > 0 ) : ?>
<p class="total"><?php _e( 'Total', 'snssimen' ); ?>: <strong><?php echo WC()->cart->get_cart_subtotal(); ?></strong></p>
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
<p class="buttons">
<?php _e( 'Go to Cart', 'snssimen' ); ?>
<?php _e( 'Checkout', 'snssimen' ); ?>
</p>
<span style="display:none" class="sns-cart-number">
<?php echo sizeof( WC()->cart->get_cart() ) ?>
<?php //echo $woocommerce->cart->cart_contents_count; ?>
</span>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>
add this code in functions.php file
//This function will display total qty ni mini-cart
function filter__woocommerce_add_to_cart_fragments($fragments) {
ob_start(); ?>
<div class="number bold">
<?php echo sprintf( '%d', WC()->cart->cart_contents_count ); ?>
</div>
<?php
$fragments['#minicart .number'] = ob_get_clean();
return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'filter__woocommerce_add_to_cart_fragments' );
And to show mini-cart in header add this code in header.php file
<div class="mini-cart">
<a id="minicart" href="<?php echo WC()->cart->get_cart_url(); ?>" class="cart icon red relative">
<div class="number boldd">
<?php echo sprintf('%d', WC()->cart->cart_contents_count); ?>
</div>
<div id="cartcontents">
<div class="widget_shopping_cart_content">
<?php woocommerce_mini_cart(); ?>
</div>
</div>
</a>
</div>

How to add cart totals

I have no idea of php so I was wondering if I can get help from you guys.
So basically this is my code for woocommerce minicart.
Right now when I click on a single item to buy twice the cart counter stays on 1, when instead I obviously want 2 as it is the total.
I am aware of the things saying here, but I can't seem to find where to put the code. I tried everything and I can't make those totals to show up.
If someone can tell me which part exactly should be changed I would very much appreciate it. Thanks in advance!
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you (the theme developer).
* will need to copy the new files to your theme to maintain compatibility. We try to do this.
* as little as possible, but it does happen. When this occurs the version of the template file will.
* be bumped and the readme will list any important changes.
*
* #see http://docs.woothemes.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php do_action( 'woocommerce_before_mini_cart' ); ?>
<ul class="cart_list products-list product_list_widget <?php echo $args['list_class']; ?>">
<?php if ( ! WC()->cart->is_empty() ) : ?>
<?php
foreach ( WC()->cart->get_cart() 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 );
?>
<li>
<?php if ( ! $_product->is_visible() ) : ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
<?php else : ?>
<a class="title" href="<?php echo esc_url( $_product->get_permalink( $cart_item ) ); ?>">
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
</a>
<?php endif; ?>
<?php echo WC()->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $product_price ) . '</span>', $cart_item, $cart_item_key ); ?>
<?php echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf( '<i class="fa fa-times-circle"></i>', esc_url( WC()->cart->get_remove_url( $cart_item_key ) ), __( 'Remove this item', 'snssimen' ) ), $cart_item_key ); ?>
</li>
<?php
}
}
?>
<?php else : ?>
<li class="empty"><?php _e( 'No products in the cart.', 'snssimen' ); ?></li>
<?php endif; ?>
</ul><!-- end product list -->
<?php if ( sizeof( WC()->cart->get_cart() ) > 0 ) : ?>
<p class="total"><?php _e( 'Total', 'snssimen' ); ?>: <strong><?php echo WC()->cart->get_cart_subtotal(); ?></strong></p>
<?php do_action( 'woocommerce_widget_shopping_cart_before_buttons' ); ?>
<p class="buttons">
<?php _e( 'Go to Cart', 'snssimen' ); ?>
<?php _e( 'Checkout', 'snssimen' ); ?>
</p>
<span style="display:none" class="sns-cart-number">
<?php echo sizeof( WC()->cart->get_cart() ) ?>
<?php //echo $woocommerce->cart->cart_contents_count; ?>
</span>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>

Displaying only last added item on cart page, if not logged in

In WooCommerce, my products are listed in category page with details as well as "Add to Cart" button. When user clicked on the button page refreshed and on the top success message with view cart button appeared.
when a user does it for multiple times(add more than one items):
If the user is logged in the cart page listed all the items with calculation.
but if the user is not logged in, the user only see the last item listed in cart page.
I need to list the cart added products for not logged in user..Can anyone have the solution for this...
Here is my loop/add-to-cart.php code:
echo apply_filters( 'woocommerce_loop_add_to_cart_link',
sprintf( '%s',
esc_url( $product->add_to_cart_url() ),
esc_attr( $product->id ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
esc_attr( $product->product_type ),
esc_html( $product->add_to_cart_text() )
),
$product );
Updated and checked the code (2)
Cart count works and cart page displays only last cart item for non logged users
1) When you are not logged in, only last cart item is displayed.
2) When you are logged in, all cart items are displayed.
This is possible editing the woocommerce cart.php template.
You will need first to copy to your active child theme (or theme) folder, the WooCommerce templates folder located in your woocommerce plugin folder (if not done yet) and rename that newly added folder woocommerce. See this related documentation.
Once done, you will open edit cart/cart.php file and replace the code by the following:
<?php
/**
* Cart Page
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.3.8
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_print_notices();
do_action( 'woocommerce_before_cart' ); ?>
<form action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' );
// Cart count
$the_cart = WC()->cart->get_cart_contents_count();
// initialising counter for the loop (out of it)
$count = 0;
// CHECKING JUST THE CART COUNT - TO BE REMOVED
echo '<p style="font-style:italic; color: grey; margin:10px;">(Debug Info only - Cart count: <strong>'. WC()->cart->get_cart_contents_count() . '</strong> item(s))</p>';
?>
<table class="shop_table shop_table_responsive cart" cellspacing="0">
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-thumbnail"> </th>
<th class="product-name"><?php _e( 'Product', 'woocommerce' ); ?></th>
<th class="product-price"><?php _e( 'Price', 'woocommerce' ); ?></th>
<th class="product-quantity"><?php _e( 'Quantity', 'woocommerce' ); ?></th>
<th class="product-subtotal"><?php _e( 'Total', 'woocommerce' ); ?></th>
</tr>
</thead>
<tbody>
<?php do_action( 'woocommerce_before_cart_contents' ); ?>
<?php
foreach ( WC()->cart->get_cart() 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( is_user_logged_in() || ( !is_user_logged_in() && $count == $cart_count) ):
// adding 1 to counter
$count++;
if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
<td class="product-remove">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( WC()->cart->get_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
</td>
<td class="product-thumbnail">
<?php
$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );
if ( ! $product_permalink ) {
echo $thumbnail;
} else {
printf( '%s', esc_url( $product_permalink ), $thumbnail );
}
?>
</td>
<td class="product-name" data-title="<?php _e( 'Product', 'woocommerce' ); ?>">
<?php
if ( ! $product_permalink ) {
echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . ' ';
} else {
echo apply_filters( 'woocommerce_cart_item_name', sprintf( '%s', esc_url( $product_permalink ), $_product->get_title() ), $cart_item, $cart_item_key );
}
// Meta data
echo WC()->cart->get_item_data( $cart_item );
// Backorder notification
if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
}
?>
</td>
<td class="product-price" data-title="<?php _e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
?>
</td>
<td class="product-quantity" data-title="<?php _e( 'Quantity', 'woocommerce' ); ?>">
<?php
if ( $_product->is_sold_individually() ) {
$product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
} else {
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(),
'min_value' => '0'
), $_product, false );
}
echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item );
?>
</td>
<td class="product-subtotal" data-title="<?php _e( 'Total', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
?>
</td>
</tr>
<?php
}
endif;
}
do_action( 'woocommerce_cart_contents' );
?>
<tr>
<td colspan="6" class="actions">
<?php if ( wc_coupons_enabled() ) { ?>
<div class="coupon">
<label for="coupon_code"><?php _e( 'Coupon:', 'woocommerce' ); ?></label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" /> <input type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply Coupon', 'woocommerce' ); ?>" />
<?php do_action( 'woocommerce_cart_coupon' ); ?>
</div>
<?php } ?>
<input type="submit" class="button" name="update_cart" value="<?php esc_attr_e( 'Update Cart', 'woocommerce' ); ?>" />
<?php do_action( 'woocommerce_cart_actions' ); ?>
<?php wp_nonce_field( 'woocommerce-cart' ); ?>
</td>
</tr>
<?php do_action( 'woocommerce_after_cart_contents' ); ?>
</tbody>
</table>
<?php do_action( 'woocommerce_after_cart_table' ); ?>
</form>
<div class="cart-collaterals">
<?php do_action( 'woocommerce_cart_collaterals' ); ?>
</div>
<?php do_action( 'woocommerce_after_cart' ); ?>
Then save.
As you will see on line 31 to 38 (outside the cat foreach loop), I have added some code to get the total cart items count and initialized a counter to zero. Also I have temporarily displayed the cart count (You can remove this line as it's only for testing).
I have add a conditional if statement that is going to do what you are expecting:
if( is_user_logged_in() || ( !is_user_logged_in() && $count == $cart_count) ):
Just after this condition I increment the $count variable (which is in the condition).
This is tested and works perfectly now…

Limit the number of products shown in mini cart widget

I am using WooCommerce mini cart widget and I want to limit the number of products shown in the mini cart. If a user adds 10 products to the cart, the mini cart widget shows all 10.
I would like to limit the number lets say to 5 products.
Is there a way to achieve this?
Thanks.
You can achieve this overriding cart/mini-cart.php WooCommerce template via your active theme, ( see this related docs: Template Structure + Overriding Templates via a Theme ).
Briefly (if not done yet), you will need to copy from woocommerce plugin folder, a subfolder named templates to your active child theme (or theme) and to rename it woocommerce..
After that you find inside that new woocommerce folder in cart subfolder a template named mini-cart.php.
Open/edit mini-cart.php template and replace the code by this to limit cart to 5 items:
<?php
/**
* Mini-cart
*
* Contains the markup for the mini-cart, used by the cart widget.
*
* This template can be overridden by copying it to yourtheme/woocommerce/cart/mini-cart.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.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->is_empty() ) : ?>
<?php
// Define HERE the number of items
$number_of_items = 5;
$loop_count = 0;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Limiting number of items displayed in mini cart
if($loop_count < $number_of_items) {
// The counter
$loop_count++;
$_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 );
$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
?>
<li class="<?php echo esc_attr( apply_filters( 'woocommerce_mini_cart_item_class', 'mini_cart_item', $cart_item, $cart_item_key ) ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url( WC()->cart->get_remove_url( $cart_item_key ) ),
__( 'Remove this item', 'woocommerce' ),
esc_attr( $product_id ),
esc_attr( $_product->get_sku() )
), $cart_item_key );
?>
<?php if ( ! $_product->is_visible() ) : ?>
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
<?php else : ?>
<a href="<?php echo esc_url( $product_permalink ); ?>">
<?php echo str_replace( array( 'http:', 'https:' ), '', $thumbnail ) . $product_name . ' '; ?>
</a>
<?php endif; ?>
<?php echo WC()->cart->get_item_data( $cart_item ); ?>
<?php echo apply_filters( 'woocommerce_widget_cart_item_quantity', '<span class="quantity">' . sprintf( '%s × %s', $cart_item['quantity'], $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->is_empty() ) : ?>
<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">
<?php _e( 'View Cart', 'woocommerce' ); ?>
<?php _e( 'Checkout', 'woocommerce' ); ?>
</p>
<?php endif; ?>
<?php do_action( 'woocommerce_after_mini_cart' ); ?>
Here we count the number of items in the foreach loop and we limit them to a specified number in variable $number_of_items.

Categories