[MARKETPRESS] issue with mu-plugin

I try to implement this on my site but this does not work:

https://premium.wpmudev.org/forums/topic/marketpress-mini-cart-as-the-menu-item-with-products-count

During the chat, we tested this on agent site and this gives an error.

Can you provide correct solution for this case, it will be awesome :slight_smile:

  • Dimitris
    • Support Star

    Hello there Darius,

    hope you’re doing well today! :slight_smile:

    I’ve already forwarded this to our Second Level Support guys for further investigation.

    Me or another colleague of mine will keep you posted here as soon as there’s any development.

    Your patience is highly appreciated.

    Thank you,

    Dimitris

  • Panos
    • SLS

    Hi Darius !

    That’s a bit old thread so that snippet is no longer working. You can try using the following :

    add_filter( 'wp_nav_menu_items', function( $nav_items, $menu_args ){

    // If we want to limit this menu item to appear to specific menus
    $add_to_menus = array( 'Menu one', 'Menu two' );

    if ( ! in_array( $menu_args->menu->name, $add_to_menus ) ) {
    return $nav_items;
    }

    $menu_title = '';
    $cart_items_count = 0;
    $cart_items = mp_cart()->get_items();

    foreach( $cart_items as $key => $quantity ) {
    $cart_items_count += $quantity;
    }

    $menu_title = "<span class="cart-items-count">{$cart_items_count}</span> items in cart";
    $nav_item = mp_cart_link( false, false, $menu_title );

    $nav_items .= "<li class="menu-item menu-item-cart">{$nav_item}</li>";
    return $nav_items;

    }, 20, 2 );

    add_action( 'wp_footer', function(){

    if ( ! mp_is_shop_page() ) {
    return;
    }

    ?>
    <script type="text/javascript">

    ($ => {

    MP_CartChange_Monitor = {

    init : function(){

    $(document).ajaxComplete(function( event, XMLHttpRequest, settings ) {

    const action = MP_CartChange_Monitor.get_action( 'action', settings.data ),
    menu_item = $( '.menu-item-cart span.cart-items-count' );
    let items_count = 0;
    if (
    'add_item' != action &&
    'update_item' != action &&
    'remove_item' != action
    ) {
    return;
    }

    if ( $( '#mp-floating-cart' ).length ) {
    items_count = $( '#mp-floating-cart .mp_mini_cart_count' ).text();
    } else {
    $( '.mp_cart_item_content-qty select.select2-offscreen' ).each(function(){
    items_count += parseInt( $(this).val() );
    });
    }

    menu_item.html( items_count );
    //console.log('action : ' , action );
    });
    },

    custom_time_slots : function() {
    let start_time = '6:00',
    end_time = '17:20';

    },
    get_action: function(name, url){
    if (!url){
    return '';
    }
    name = name.replace(/[[]]/g, "\$&");
    var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/+/g, " "));
    }
    };

    $(document).ready(function(){
    MP_CartChange_Monitor.init();
    });

    })(jQuery);

    </script>
    <?php
    } );

    You can specify on which menus do you want to show this in this line:

    $add_to_menus = array( 'Menu one', 'Menu two' );

    where you can add your menu titles separated with comas.

    If you wish to display it in all menus,then you can remove this part completely :

    // If we want to limit this menu item to appear to specific menus
    $add_to_menus = array( 'Menu one', 'Menu one' );

    if ( ! in_array( $menu_args->menu->name, $add_to_menus ) ) {
    return $nav_items;
    }

    Please give it a try and let us know how it goes :slight_smile:

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.