Shopping Cart in Header for Marketpress 3

Hi

Is it possible to put a shopping cart in the header for marketpress 3. I had it working with 2.9 lovely but the code is not working now with version 3. It was Ashok who got it working great last time

add_filter( 'wp_nav_menu_items', 'add_cart_link', 10, 2 );
function add_cart_link( $items, $args ) {
global $mp;
$items .= '<li class="menu_cart">'.mp_cart_link( false, false, 'Cart - <span>' . get_cart_value(false) . '</span>' ).'';
return $items;
}

add_action( 'wp_footer', 'add_cart_value' );
function add_cart_value() {
global $mp;
$cart = $mp->get_cart_cookie();
?>
<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').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_cart_value( $echo = true ) {
global $mp;
$totalCount = 0;
$cart = $mp->get_cart_cookie();

foreach( $cart as $c ){
$totalCount += count($c);
}

$selected_cart = $mp->get_cart_contents(true);

$totals = array();
foreach ($selected_cart as $bid => $cart) {
foreach ($cart as $product_id => $variations) {
foreach ($variations as $variation => $data) {
$price = $data['price'] * $data['quantity'];
$discount_price = $mp->coupon_value_product($coupon_code, $price, $product_id);
$totals[] = $discount_price;
}
}
}
$total = array_sum($totals);
if ( $mp->get_setting('tax->tax_inclusive') && $mp->get_setting('tax->tax_shipping') ) {
$total += array_sum($shipping_tax_prices) - array_sum($shipping_prices);
}

if( $echo ){
echo $totalCount . ' item(s) - £' . $total;;
die();
}else{
return $totalCount . ' item(s) - £' . $total;;
}
}

function get_current_cart_value() {
global $mp;
$totalCount = 0;
$cart = $mp->get_cart_cookie();

foreach( $cart as $c ){
$totalCount += count($c);
}

$selected_cart = $mp->get_cart_contents(true);

$totals = array();
foreach ($selected_cart as $bid => $cart) {
foreach ($cart as $product_id => $variations) {
foreach ($variations as $variation => $data) {
$price = $data['price'] * $data['quantity'];
$discount_price = $mp->coupon_value_product($coupon_code, $price, $product_id);
$totals[] = $discount_price;
}
}
}
$total = array_sum($totals);
if ( $mp->get_setting('tax->tax_inclusive') && $mp->get_setting('tax->tax_shipping') ) {
$total += array_sum($shipping_tax_prices) - array_sum($shipping_prices);
}

echo $totalCount . ' item(s) - £' . $total;
die();

}

  • Nathan Onn

    @Kieran

    Here you go:

    add_filter( 'wp_nav_menu_items', 'add_cart_link', 10, 2 );
    function add_cart_link( $items, $args ) {
        global $mp;
        $items .= '<li class="menu_cart">'.mp_cart_link( false, false, 'Cart - <span>' . get_cart_value(false) . '</span>' ).'';
        return $items;
    }
    
    add_action( 'wp_footer', 'add_cart_value' );
    function add_cart_value() {
        ?>
        <script type="text/javascript">
        jQuery(function($){
    
            // update cart upon ajax complete
            $(document).ajaxComplete( function( event, xhr, settings ) {
    
                var AjaxData = settings.data;
    
                if ( ( typeof AjaxData && AjaxData !== undefined ) && AjaxData.indexOf("cart_action=add_item") >= 0 ) {
                    $.post( '<?php echo admin_url( 'admin-ajax.php' ); ?>', { action: 'get_cart_value' }, function(response) {
                        $('.menu_cart a span').text(response);
                    });
                }
    
            });
    
        });
        </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_cart_value( $echo = true ) {
        global $mp_cart;
        $count = 0;
    
        if ( empty( $mp_cart ) ) {
            $mp_cart = MP_Cart::get_instance();
        }
    
        $items = $mp_cart->get_items();
    
        if ( !empty( $items ) ) {
            foreach ( $items as $id => $qty ) {
                $count += intval( $qty );
            }
        }
    
        if ( $echo ){
            echo $count . ' item(s) - £' . $mp_cart->product_total();
        } else{
            return $count . ' item(s) - £' . $mp_cart->product_total();
        }
    }
    
    function get_current_cart_value() {
    
        global $mp_cart;
        $count = 0;
    
        if ( empty( $mp_cart ) ) {
            $mp_cart = MP_Cart::get_instance();
        }
    
        $items = $mp_cart->get_items();
    
        if ( !empty( $items ) ) {
            foreach ( $items as $id => $qty ) {
                $count += intval( $qty );
            }
        }
    
        if (defined('DOING_AJAX') && DOING_AJAX) {
            echo $count . ' item(s) - £' . $mp_cart->product_total();
            exit;
        }  // end - doing_ajax
    
    }

    Try it out and let me know if it works.

  • Kieran

    Hi @Nathan

    That works perfectly many thanks!!

    For others using it I also added the following code to my theme style.css file to get a shopping cart icon in my header. Looks great just as it used to do! To be honest I really think this should be part of marketpress so happy for this thread to be moved to features if others want a shopping cart and image in the main menu header too.

    .menu_cart a {
    background: url(http://mysite.com/wp-content/uploads/2014/09/cart1-e1411662428797.png) no-repeat 0 50% !important;
    padding-left: 38px;
    }

    Thanks for the help
    Kieran