Custom shortcode unable to be used in "Primary Text" field of cart modal

I have a cart modal in my header.

I have created a custom shortcode [cart-contents] to conditionally display cart count and total only if there is something in the cart.

An aside: pretty pointless to have dynamic content showing 0 $0.00 which is why I built the shortcode to begin with.

Anyway, I’ve added my shortcode to the “primary text” field of the cart modal, like this:

However, the shortcode doesn’t render:

Shortcode works fine if placed into a text element:

Any ideas on how to sort?

Hi @liquidedge,

Thank you for writing in, this has actually been on our feature request (hide cart element when empty) and we still trying to find ways to implement this effectively, for now please use the native dynamic shortcode.

{{dc:woocommerce:cart_items}} @ {{dc:woocommerce:cart_total}}

Thank you for your understanding,

Hi @friech,

Unfortunately I don’t think you have understood my request. I want a shortcode that I have written to output correctly when placed in the “Primary Text” field.

I do not want to use your dynamic content option, because as I stated, when there is nothing in the cart, it shows a zero value.

For anyone who may come across this, here’s the shortcode:

// Display count and value of cart
function sgc_cart_count(){
  global $woocommerce;

  $count = $woocommerce->cart->get_cart_contents_count();
  $total = $woocommerce->cart->get_cart_total();

  if ( $count == 1 ) {
    return "1 item @ " . $total;
  } elseif ( $count > 1 ) {
    return $count . " items @ " . $total;
  }

}
add_shortcode( 'cart-contents', 'sgc_cart_count' );

Thanks for your attention solving this.

@liquidedge,

Thank you for the clarification.
Cart Modal is specifically customize and only accepts dynamic shortcode. Your option right now is to use CONTENT AREA MODAL element instead. It accepts shortcode. See this: https://screencast-o-matic.com/watch/cqjQicON7X With that element you have freedom to design it how you want it.

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.