简体   繁体   中英

WooCommerce custom mini cart function/shortcode not working

I've been trying to implement the MiniCart in my woocommerce shop, yet without succes.

What I tried is making a custom function and shortcode, and call it in my header.

Here's the code I'm using in my theme functions:

function custom_mini_cart() {
    echo '<a href="#" class="dropdown-back" data-toggle="dropdown"> ';
        echo '<i class="fa fa-shopping-cart" aria-hidden="true"></i>';
        echo '<div class="basket-item-count" style="display: inline;">';
            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( '[nachtleven-mini-cart]', 'custom_mini_cart' );

So I want to use this to output the mini cart:

<?php echo do_shortcode('nachtleven-mini-cart'); ?>

Yet it always returns a string on the frontend: [custom-mini-cart] , so it seems like the shortcode isn't working. Am I missing something here? Even a simple echo in the function won't display.

I haven't had any problems with making and using shortcodes before, though this is the first time I'm building with WooCommerce.

Any help or tips would be greatly appreciated as I'm kinda overlooking the problem here.

Thanks =)

You add a wrong hook for the shortcode tag when you call add_shortcode( $tag , $func ); by adding name inside square brackets [].Your code should be link:

function custom_mini_cart() {
echo '<a href="#" class="dropdown-back" data-toggle="dropdown"> ';
    echo '<i class="fa fa-shopping-cart" aria-hidden="true"></i>';
    echo '<div class="basket-item-count" style="display: inline;">';
        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( 'nachtleven-mini-cart', 'custom_mini_cart' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM