Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code snippet to add the WooCommerce cart quantity and total to a SuperSide Me custom cart button.
<?php
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment