skip to Main Content

I am working on WooCommerce theme implementation I’ve designed mini cart sidepanel using jquery problem is when user clicks on add to cart on shop page its shows product is added, page is not loading & in minicart added items are not showing I need to refresh the page to see the added items. Please help into this.

I am designer I worked on WordPress theme implementation WooCommerce theme implementation is new to me. So please provide me the solution.

2

Answers


  1. Firstly, you can create the shortcode on function.php

    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( '[custom-techno-mini-cart]', 'custom_mini_cart' );

    After then, call the shortcode anywhere in pages or custom template pages.

    Like this,

    <?php echo do_shortcode(['custom-techno-mini-cart']); ?>

    Now, you can get your layout according to your requirement as mentioned in this.

    Also, you can add this function anywhere in pages,

    <?php echo woocommerce_mini_cart(); ?>

    Good Luck !

    Login or Signup to reply.
  2. You can use:

    jQuery(document.body).trigger('wc_fragment_refresh');
    

    In your on click add to cart function.


    If using ajax add to cart, use it like this:

    success: function( response ) {
      jQuery(document.body).trigger('wc_fragment_refresh');                              
    }
    

    Or inside PHP script after add to cart:

    WC()->cart->add_to_cart( $_POST['product_id'], $_POST['quantity'], $_POST['variation_id'], array(), array());
    
    <script> 
      jQuery(document.body).trigger('wc_fragment_refresh');
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search