0

I am trying to create a shortcode - [Count] - for a Woocommerce shop that allows me to include the amount of items in the cart inline with standard text.

I have tried (in my functions.php file):

function itemCount() {
  global $woocommerce;

  $items = sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);

  return $items;
}

add_shortcode('Count', 'itemCount');

I've researched this a fair bit and I can't seem to work it out so any assistance is greatly appreciated.

  • Your code copied verbatim works fine, try enabling debug mode and see if that helps. On a side note, `global $woocommerce` is deprecated, use `WC()` in its place. – Anand Shah Feb 15 '16 at 17:17
  • Yeah, that looks right to me too. Keep in mind that it may not update if items are added to the cart via ajax. But it isn't clear if you are asking about that. – helgatheviking Feb 15 '16 at 19:19
  • Yeah that was the next thing I wanted to do - to update the shortcode via ajax, but I was thinking I would get the non-ajax version working first. That said, if I should go with ajax now, then yeah, lets do it (and I've never coded ajax before so a complete beginner). And thanks, Anand, I'll switch to `WC()` – RuettigerPStone Feb 15 '16 at 22:29

0 Answers0