• Hi

    I am trying to display the number of items in the cart in my main navigation menu.

    I added this to the functions.php of my child theme, but I can’t access Appearances/menu Back office anymore.

    Any idea what’s wrong ?
    I am stuck, as you can see I’ve tried several syntaxes.

    Just using $woocommerce->cart->cart_contents_count(); makes the appearances/menu page blank with an error 500.

    Thanks

    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;
    }
    • This topic was modified 7 years, 2 months ago by louisJJ.
    • This topic was modified 7 years, 2 months ago by louisJJ.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘display number of items in the menu, error 500 in Back Office’ is closed to new replies.