0

I am using this code in the functions.php of my theme, in order to add cart information next to the cart menu item.

However, this code adds this information outside the <li> and I would like the information to be into the <li>.

Can you help me change this code in order to display the info within the <li>?

Thanks

/**
 * This function adds the WooCommerce or Easy Digital Downloads cart icons/items to the top_nav menu area as the last item.
 */
add_filter( 'wp_nav_menu_items', 'my_wp_nav_menu_items', 10, 2 );
function my_wp_nav_menu_items( $items, $args, $ajax = false ) {
    // Top Navigation Area Only
    if ( ( isset( $ajax ) && $ajax ) || ( property_exists( $args, 'theme_location' ) && $args->theme_location === 'primary' ) ) {
        // WooCommerce
        if ( class_exists( 'woocommerce' ) ) {
            $css_class = 'menu-item menu-item-type-cart menu-item-type-woocommerce-cart';
            // Is this the cart page?
            if ( is_cart() )
                $css_class .= ' current-menu-item';
            //$items .= '<li class="' . esc_attr( $css_class ) . '">';
                $items .= '<a class="cart-contents" href="' . esc_url( WC()->cart->get_cart_url() ) . '">';
                    // $items .= wp_kses_data( WC()->cart->get_cart_total() ) . ' - <span class="count">' .  wp_kses_data( sprintf( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count(), 'simple-shop' ), WC()->cart->get_cart_contents_count() ) ) . '</span>';
                $items .= ' (<span class="count">' .  wp_kses_data( sprintf( _n( '%d', '%d', WC()->cart->get_cart_contents_count(), 'simple-shop' ), WC()->cart->get_cart_contents_count() ) ) . '</span>)';
                $items .= '</a>';
            //$items .= '</li>';
        }
    }
    return $items;
}
/**
 * This function updates the Top Navigation WooCommerce cart link contents when an item is added via AJAX.
 */
add_filter( 'woocommerce_add_to_cart_fragments', 'my_woocommerce_add_to_cart_fragments' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
    // Add our fragment
    $fragments['li.menu-item-type-woocommerce-cart'] = my_wp_nav_menu_items( '', new stdClass(), true );
    return $fragments;
}

EDIT (new code)

 add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup($item) {

    if ( class_exists( 'woocommerce' ) ) {

        global $woocommerce;

        ?>
        <!--<pre><?php //var_dump( $woocommerce->cart->get_cart_contents_count()  ); ?></pre>-->        
        <?php


        if ( $item->url == esc_url( wc_get_cart_url() ) ) {


            //$item->title = 'MY BAG('. '<span class="count">' .  wp_kses_data( sprintf( _n( '%d', '%d', WC()->cart->get_cart_contents_count(), 'simple-shop' ), WC()->cart->get_cart_contents_count() ) ) . '</span>)';

            //$item->title = 'MY BAG('. '<span class="count">' .  wp_kses_data( sprintf( _n( '%d', '%d', $woocommerce->cart->cart_contents_count(), 'simple-shop' ), $woocommerce->cart->cart_contents_count() ) ) . '</span>)';


            $item->title = 'MY BAG('. '<span class="count">' .  $woocommerce->cart->get_cart_contents_count() . '</span>)';


            //$item->title = sprintf( _n( '%d', '%d', $woocommerce->cart->cart_contents_count();, 'simple-shop' ), $woocommerce->cart->cart_contents_count(); );


        }
    }
    return $item;
}
0

The items in the $fragments array will replace the html contents of the element you specify as the key. In your example

$fragments['li.menu-item-type-woocommerce-cart'] = 'some html';

some html will be inserted in the DOM element <li class="menu-item-type-woocommerce-cart">

Here is the Javascript:

$( 'li.menu-item-type-woocommerce-cart' ).replaceWith( 'some html' );

Is there already such an element in your menu?

  • Hi @ChristianLescuyer, I changed my code now. and it displays well. But I still have a php issue, because some Back Office page won't display now. – Louis Sep 11 '17 at 15:28
  • Hi @Louis, if you’re happy with my answer, just approve it. As for your back office pages, it’s difficult to analyze without more information. – Christian Lescuyer Sep 11 '17 at 19:14
0

I am posting the full solution I used (with two languages, to be placed in functions.php):

add_filter( 'wp_setup_nav_menu_item','my_item_setup' );
function my_item_setup($item) {

    if ( ! is_admin() ) {
        if ( class_exists( 'woocommerce' ) ) {

            global $woocommerce;

            if ( $item->url == esc_url( wc_get_cart_url() ) ) {

                if (is_null($woocommerce->cart)){

                } else {
                    if( get_locale() == 'fr_FR' ) {
                        $item->title = 'PANIER ('. '<span class="count">' .  $woocommerce->cart->get_cart_contents_count() . '</span>)';
                    } else {
                        $item->title = 'MY BAG ('. '<span class="count">' .  $woocommerce->cart->get_cart_contents_count() . '</span>)';
                    }
                }

            }
        }
    }
    return $item;
}


/**
 * This function updates the cart link contents when an item is added via AJAX.
 */
add_filter( 'woocommerce_add_to_cart_fragments', 'my_woocommerce_add_to_cart_fragments' );
function my_woocommerce_add_to_cart_fragments( $fragments ) {
    // Add our fragment
    $fragments['li.menu-item-type-woocommerce-cart'] = my_item_setup( '');
    return $fragments;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.