How to show decimals on the mini cart widget when using Woocommerce’s Storefront theme

Updated 1 month ago

If you are allowing decimals for the stock quantities of your products with ATUM, you may note that the Storefront cannot show them on the mini cart widget. 

So, to get the decimals quantities displayed, the solution would be to add this code in your child theme's functions.php:

/**
 * Replace the storefront minicart header.
 */
function replace_storefront_minicart_header() {

	if ( ! function_exists( 'storefront_header_cart' ) ) {
		return;
	}

	remove_action( 'storefront_header', 'storefront_header_cart', 60 );
	remove_filter( 'woocommerce_add_to_cart_fragments', 'storefront_cart_link_fragment' );
	add_action( 'storefront_header', 'custom_storefront_header_cart', 60 );
	add_filter( 'woocommerce_add_to_cart_fragments', 'custom_storefront_cart_link_fragment' );

}
add_action( 'init', 'replace_storefront_minicart_header' );

/**
 * Decimal quantity available for Storefront minicart header.
 */
function custom_storefront_header_cart() {
	if ( storefront_is_woocommerce_activated() ) {
		if ( is_cart() ) {
			$class = 'current-menu-item';
		} else {
			$class = '';
		}
		?>
		<ul id="site-header-cart" class="site-header-cart menu">
			<li class="<?php echo esc_attr( $class ); ?>">
				<?php if ( storefront_woo_cart_available() ) { ?>
					<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
						<?php /* translators: %d: number of items in cart */ ?>
						<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo wp_kses_data( sprintf( _n( '%s item', '%s items', WC()->cart->get_cart_contents_count(), 'storefront' ), (string) WC()->cart->get_cart_contents_count() ) ); ?></span>
					</a>
				<?php } ?>
			</li>
			<li>
				<?php the_widget( 'WC_Widget_Cart', 'title=' ); ?>
			</li>
		</ul>
		<?php
	}
}

/**
 * Cart Fragments
 * Ensure cart contents update when products are added to the cart via AJAX
 *
 * @param  array $fragments Fragments to refresh via AJAX.
 * @return array            Fragments to refresh via AJAX
 */
function custom_storefront_cart_link_fragment( $fragments ) {
	global $woocommerce;

	ob_start();
	if ( storefront_woo_cart_available() ) {
		?>
		<a class="cart-contents" href="<?php echo esc_url( wc_get_cart_url() ); ?>" title="<?php esc_attr_e( 'View your shopping cart', 'storefront' ); ?>">
			<?php /* translators: %d: number of items in cart */ ?>
			<?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?> <span class="count"><?php echo wp_kses_data( sprintf( _n( '%s item', '%s items', WC()->cart->get_cart_contents_count(), 'storefront' ), WC()->cart->get_cart_contents_count() ) ); ?></span>
		</a>
		<?php
	}
	$fragments['a.cart-contents'] = ob_get_clean();

	ob_start();
	storefront_handheld_footer_bar_cart_link();
	$fragments['a.footer-cart-contents'] = ob_get_clean();

	return $fragments;
}
Did this answer your question?