Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bappi-d-great/30ee0931dc0d478bb19e to your computer and use it in GitHub Desktop.
Save bappi-d-great/30ee0931dc0d478bb19e to your computer and use it in GitHub Desktop.
Add cart in menu with quantity and price
<?php
add_filter( 'wp_nav_menu_items', 'add_cart_link', 10, 2 );
function add_cart_link( $items, $args ) {
$cart = MP_Cart::get_instance();
$items .= '<li class="menu_cart">'.mp_cart_link( false, false, 'Cart <span class="mp_custom_cart_icon"></span> (<span class="mp_custom_cart_details"> ' . $cart->item_count( false, false ) . ' - £' . $cart->total( false ) . ' </span>)' ).'</li>';
return $items;
}
add_action( 'wp_footer', 'add_cart_value' );
function add_cart_value() {
?>
<script type="text/javascript">
jQuery(function($){
var data = {
'action': 'get_cart_value'
};
setInterval(function() {
$.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', data, function(response) {
$('.menu_cart a span.mp_custom_cart_details').text( ' ' + response + ' ' );
});
}, 5000);
});
</script>
<?php
}
add_action( 'wp_ajax_get_cart_value', 'get_current_cart_value' );
add_action( 'wp_ajax_nopriv_get_cart_value', 'get_current_cart_value' );
function get_current_cart_value() {
$cart = MP_Cart::get_instance();
echo $cart->item_count( false, false ) . ' - £' . $cart->total( false );
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment