Created
June 27, 2018 18:06
-
-
Save robincornett/5ccd693db02da329ce8ed544e2ca72c2 to your computer and use it in GitHub Desktop.
Code snippet to add the WooCommerce cart quantity and total to a SuperSide Me custom cart button.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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