Add WooCommerce Quantity/Total to a SuperSide Me Custom Cart Button

With the Menu Bar and custom buttons in SuperSide Me, it’s easy to add a button to take your users directly to their WooCommerce shopping cart. I had a user ask if it was possible to add the WooCommerce quantity and total information to the custom button as well. It’s totally possible! Here’s the code to make it happen (note that it checks for the specific label of the cart button–if yours is not “Cart”, change that to whatever yours actually is):

add_filter( 'supersideme_custom_buttons', 'prefix_add_woocommerce_quantity_total_button' );
/**
* Add the WooCommerce quantity and total to a shopping cart button.
*
* @param $buttons
*
* @return mixed
*/
function prefix_add_woocommerce_quantity_total_button( $buttons ) {
// First, check to make sure WooCommerce is running--if not, stop.
if ( ! function_exists( 'wc' ) ) {
return $buttons;
}
// Cycle through all the custom buttons and add the custom Woo data to the cart button.
foreach ( $buttons as $key => &$b ) {
// Change 'Cart' to whatever your button label is.
if ( 'Cart' === $b['label'] ) {
global $woocommerce;
$cart_contents_count = $woocommerce->cart->cart_contents_count;
$cart_total = $woocommerce->cart->get_cart_total();
// Remove the conditional to always show the quantity of items and cost.
if ( $cart_contents_count > 0 ) {
$b['text'] = ' <span class="woo-cart">' . $cart_contents_count . ' (' . $cart_total . ')</span>';
}
}
}
return $buttons;
}
view raw woocommerce.php hosted with ❤ by GitHub
Return to SuperSide Me
Bars Code Check chevron-down chevron-left chevron-right chevron-up Envelope Laptop Search Times Facebook GitHub Google Plus Instagram LinkedIn Pinterest Twitter WordPress Logo Wordpress Simple