0

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;
}
  • There is probably an error_log / php_errorlog file somewhere with the exact error. We'd need that before looking at code – Paul G. Sep 1 '17 at 15:19
  • the line not commented out above has ";"s where there shouldn't be... – rudtek Sep 1 '17 at 15:57
  • Thanks, I fixed the ';'. @PaulG I am going to try and find the error log. – Louis Sep 5 '17 at 9:54

Your Answer

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

Browse other questions tagged or ask your own question.