Site logo

Archived topic

Failed to create mini cart shortcode

5 replies · Started by xinghui on October 11, 2022

Viewing posts 1–6 of 6

I want to call woocommerce's mini shopping cart, so I defined a mini shopping cart shortcode myself, I borrowed the code from the Internet, but it did not display correctly after creation? Is there any error please?
my image_url:https://i.postimg.cc/x8T1jXJ3/Lavb-mini-cart-testttcccccc.jpg

Hi there,

we can't really support custom development code.
But if you want to share the code you used to create the shortcode i can take a look for any obvious issues.

Thank you very much for your help, I use this code, but it doesn't seem to work

function custom_mini_cart() { 
    echo '<a href="#"> ';
    echo '<i class="fa fa-shopping-cart" aria-hidden="true"></i>';
    echo '<div class="basket-item-count">';
        echo '<span class="cart-items-count count">';
            echo WC()->cart->get_cart_contents_count();
        echo '</span>';
    echo '</div>';
    echo '</a>';
    echo '<ul class="dropdown-menu dropdown-menu-mini-cart">';
        echo '<li> <div class="widget_shopping_cart_content">';
                  woocommerce_mini_cart();
            echo '</div></li></ul>';

      }

       add_shortcode( '[custom-techno-mini-cart]', 'custom_mini_cart' );

this is the code link I refer to:https://www.codegrepper.com/code-examples/php/mini+cart+woocommerce+shortcode

I can't say if this will fix it, but i can see 2 issues.

1. Use of echo in a shortcode requires Object Buffering. See here:

https://docs.generatepress.com/article/creating-a-shortcode/

2. The add_shortcode name should not include the [ square brackets ]

Making those changes your code would look like:


function custom_mini_cart() { 
    ob_start();
    echo '<a href="#"> ';
    echo '<i class="fa fa-shopping-cart" aria-hidden="true"></i>';
    echo '<div class="basket-item-count">';
    echo '<span class="cart-items-count count">';
    echo WC()->cart->get_cart_contents_count();
    echo '</span>';
    echo '</div>';
    echo '</a>';
    echo '<ul class="dropdown-menu dropdown-menu-mini-cart">';
    echo '<li> <div class="widget_shopping_cart_content">';
    woocommerce_mini_cart();
    echo '</div></li></ul>';
    return ob_get_clean();
}

add_shortcode( 'custom-techno-mini-cart', 'custom_mini_cart' );

Thanks, maybe my function is wrong, I have found a plugin to replace it, still appreciate your kind help, have a nice day

You're welcome

This archived topic is closed to new replies.