Skip to content
Advertisement

How would I correctly parse this PHP?

I know the PHP part of this code is wrong but I can’t figure out how to write it correctly. Can someone give me a hand?

function add_last_nav_item($items) {
  return $items .= '<li><a class="cart-button" href="/cart">View cart (<?php echo WC()->cart->get_cart_contents_count(); ?>)</a></li>';
}
add_filter('wp_nav_menu_items','add_last_nav_item');

Advertisement

Answer

No need to <?php echo from PHP, concatenate the value you need (WC()->cart->get_cart_contents_count()). Change to:

function add_last_nav_item($items) {
  return $items .= '<li><a class="cart-button" href="/cart">View cart ('. WC()->cart->get_cart_contents_count() .')</a></li>';
}
add_filter('wp_nav_menu_items','add_last_nav_item');

https://www.php.net/manual/en/language.operators.string.php

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement