[Support request] Failed to create mini cart shortcode

Home Forums Support [Support request] Failed to create mini cart shortcode

Home Forums Support Failed to create mini cart shortcode

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2369675
    xinghui

    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

    #2369775
    David
    Staff
    Customer Support

    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.

    #2369811
    xinghui

    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

    #2370869
    David
    Staff
    Customer Support

    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' );
    #2370895
    xinghui

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

    #2370979
    David
    Staff
    Customer Support

    You’re welcome

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.