Woocommerce mini cart updated by Ajax every second change










0















I have a local webshop with woocommerce and flatsome-child theme. I've created a mini-cart in header that works fine and added an ajax code to make it update.



Ajax code:



add_filter( 'woocommerce_add_to_cart_fragments', 'flatsome_add_to_cart_dropdown', 30, 1 );
function flatsome_add_to_cart_dropdown( $fragments )
global $woocommerce;
global $flatsome_opt;
ob_start();
?>
<div class="cart-inner">
<a href="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" class="cart-link">
<strong class="cart-name hide-for-small"><?php _e('Cart', 'woocommerce'); ?></strong>
<span class="cart-price hide-for-small">/ <?php wc_cart_totals_order_total_html(); ?></span>
<!-- cart icon -->
<div class="cart-icon">
<?php if ($flatsome_opt['custom_cart_icon']) ?>
<div class="custom-cart-inner">
<div class="custom-cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></div>
<img class="custom-cart-icon" alt="<?php _e('Cart', 'woocommerce'); ?>" src="<?php echo $flatsome_opt['custom_cart_icon']?>"/>
</div><!-- .custom-cart-inner -->
<?php else ?>
<strong><?php echo $woocommerce->cart->cart_contents_count; ?></strong>
<span class="cart-icon-handle"></span>
<?php ?>
</div><!-- end cart icon -->
</a>
<div class="nav-dropdown">
<div id="mini-cart-content" class="nav-dropdown-inner widget_shopping_cart widget_shopping_cart_content">
<?php
if (sizeof($woocommerce->cart->cart_contents)>0)
echo woocommerce_mini_cart();
else
echo '<p class="empty">'.__('No products in the cart.','woocommerce').'</p>';

?>
</div><!-- .nav-dropdown-inner -->
</div><!-- .nav-dropdown -->
</div><!-- .cart-inner -->

<?php
$fragments['.cart-inner'] = ob_get_clean();
return $fragments;



My actual header mini cart looks the same in body. The problem I'm facing is that all of this works fine but only on second change. So for example when you change quantity of the product to 15 the main cart updates but the mini cart stays unchanged. And if you then change the quantity again to 50 then both mini cart and main cart get updated. And then again if you change nothing happens but the second time it works. Could this be a cache issue, I've already tried disabling plugins but nothing seems to work. Anyone encountered something like this?



Note: the quantity change and count is custom made by a plugin , so could this cause the problem?










share|improve this question


























    0















    I have a local webshop with woocommerce and flatsome-child theme. I've created a mini-cart in header that works fine and added an ajax code to make it update.



    Ajax code:



    add_filter( 'woocommerce_add_to_cart_fragments', 'flatsome_add_to_cart_dropdown', 30, 1 );
    function flatsome_add_to_cart_dropdown( $fragments )
    global $woocommerce;
    global $flatsome_opt;
    ob_start();
    ?>
    <div class="cart-inner">
    <a href="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" class="cart-link">
    <strong class="cart-name hide-for-small"><?php _e('Cart', 'woocommerce'); ?></strong>
    <span class="cart-price hide-for-small">/ <?php wc_cart_totals_order_total_html(); ?></span>
    <!-- cart icon -->
    <div class="cart-icon">
    <?php if ($flatsome_opt['custom_cart_icon']) ?>
    <div class="custom-cart-inner">
    <div class="custom-cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></div>
    <img class="custom-cart-icon" alt="<?php _e('Cart', 'woocommerce'); ?>" src="<?php echo $flatsome_opt['custom_cart_icon']?>"/>
    </div><!-- .custom-cart-inner -->
    <?php else ?>
    <strong><?php echo $woocommerce->cart->cart_contents_count; ?></strong>
    <span class="cart-icon-handle"></span>
    <?php ?>
    </div><!-- end cart icon -->
    </a>
    <div class="nav-dropdown">
    <div id="mini-cart-content" class="nav-dropdown-inner widget_shopping_cart widget_shopping_cart_content">
    <?php
    if (sizeof($woocommerce->cart->cart_contents)>0)
    echo woocommerce_mini_cart();
    else
    echo '<p class="empty">'.__('No products in the cart.','woocommerce').'</p>';

    ?>
    </div><!-- .nav-dropdown-inner -->
    </div><!-- .nav-dropdown -->
    </div><!-- .cart-inner -->

    <?php
    $fragments['.cart-inner'] = ob_get_clean();
    return $fragments;



    My actual header mini cart looks the same in body. The problem I'm facing is that all of this works fine but only on second change. So for example when you change quantity of the product to 15 the main cart updates but the mini cart stays unchanged. And if you then change the quantity again to 50 then both mini cart and main cart get updated. And then again if you change nothing happens but the second time it works. Could this be a cache issue, I've already tried disabling plugins but nothing seems to work. Anyone encountered something like this?



    Note: the quantity change and count is custom made by a plugin , so could this cause the problem?










    share|improve this question
























      0












      0








      0








      I have a local webshop with woocommerce and flatsome-child theme. I've created a mini-cart in header that works fine and added an ajax code to make it update.



      Ajax code:



      add_filter( 'woocommerce_add_to_cart_fragments', 'flatsome_add_to_cart_dropdown', 30, 1 );
      function flatsome_add_to_cart_dropdown( $fragments )
      global $woocommerce;
      global $flatsome_opt;
      ob_start();
      ?>
      <div class="cart-inner">
      <a href="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" class="cart-link">
      <strong class="cart-name hide-for-small"><?php _e('Cart', 'woocommerce'); ?></strong>
      <span class="cart-price hide-for-small">/ <?php wc_cart_totals_order_total_html(); ?></span>
      <!-- cart icon -->
      <div class="cart-icon">
      <?php if ($flatsome_opt['custom_cart_icon']) ?>
      <div class="custom-cart-inner">
      <div class="custom-cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></div>
      <img class="custom-cart-icon" alt="<?php _e('Cart', 'woocommerce'); ?>" src="<?php echo $flatsome_opt['custom_cart_icon']?>"/>
      </div><!-- .custom-cart-inner -->
      <?php else ?>
      <strong><?php echo $woocommerce->cart->cart_contents_count; ?></strong>
      <span class="cart-icon-handle"></span>
      <?php ?>
      </div><!-- end cart icon -->
      </a>
      <div class="nav-dropdown">
      <div id="mini-cart-content" class="nav-dropdown-inner widget_shopping_cart widget_shopping_cart_content">
      <?php
      if (sizeof($woocommerce->cart->cart_contents)>0)
      echo woocommerce_mini_cart();
      else
      echo '<p class="empty">'.__('No products in the cart.','woocommerce').'</p>';

      ?>
      </div><!-- .nav-dropdown-inner -->
      </div><!-- .nav-dropdown -->
      </div><!-- .cart-inner -->

      <?php
      $fragments['.cart-inner'] = ob_get_clean();
      return $fragments;



      My actual header mini cart looks the same in body. The problem I'm facing is that all of this works fine but only on second change. So for example when you change quantity of the product to 15 the main cart updates but the mini cart stays unchanged. And if you then change the quantity again to 50 then both mini cart and main cart get updated. And then again if you change nothing happens but the second time it works. Could this be a cache issue, I've already tried disabling plugins but nothing seems to work. Anyone encountered something like this?



      Note: the quantity change and count is custom made by a plugin , so could this cause the problem?










      share|improve this question














      I have a local webshop with woocommerce and flatsome-child theme. I've created a mini-cart in header that works fine and added an ajax code to make it update.



      Ajax code:



      add_filter( 'woocommerce_add_to_cart_fragments', 'flatsome_add_to_cart_dropdown', 30, 1 );
      function flatsome_add_to_cart_dropdown( $fragments )
      global $woocommerce;
      global $flatsome_opt;
      ob_start();
      ?>
      <div class="cart-inner">
      <a href="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" class="cart-link">
      <strong class="cart-name hide-for-small"><?php _e('Cart', 'woocommerce'); ?></strong>
      <span class="cart-price hide-for-small">/ <?php wc_cart_totals_order_total_html(); ?></span>
      <!-- cart icon -->
      <div class="cart-icon">
      <?php if ($flatsome_opt['custom_cart_icon']) ?>
      <div class="custom-cart-inner">
      <div class="custom-cart-count"><?php echo $woocommerce->cart->cart_contents_count; ?></div>
      <img class="custom-cart-icon" alt="<?php _e('Cart', 'woocommerce'); ?>" src="<?php echo $flatsome_opt['custom_cart_icon']?>"/>
      </div><!-- .custom-cart-inner -->
      <?php else ?>
      <strong><?php echo $woocommerce->cart->cart_contents_count; ?></strong>
      <span class="cart-icon-handle"></span>
      <?php ?>
      </div><!-- end cart icon -->
      </a>
      <div class="nav-dropdown">
      <div id="mini-cart-content" class="nav-dropdown-inner widget_shopping_cart widget_shopping_cart_content">
      <?php
      if (sizeof($woocommerce->cart->cart_contents)>0)
      echo woocommerce_mini_cart();
      else
      echo '<p class="empty">'.__('No products in the cart.','woocommerce').'</p>';

      ?>
      </div><!-- .nav-dropdown-inner -->
      </div><!-- .nav-dropdown -->
      </div><!-- .cart-inner -->

      <?php
      $fragments['.cart-inner'] = ob_get_clean();
      return $fragments;



      My actual header mini cart looks the same in body. The problem I'm facing is that all of this works fine but only on second change. So for example when you change quantity of the product to 15 the main cart updates but the mini cart stays unchanged. And if you then change the quantity again to 50 then both mini cart and main cart get updated. And then again if you change nothing happens but the second time it works. Could this be a cache issue, I've already tried disabling plugins but nothing seems to work. Anyone encountered something like this?



      Note: the quantity change and count is custom made by a plugin , so could this cause the problem?







      php ajax wordpress woocommerce minicart






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 9:34









      IvanIvan

      11




      11






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277870%2fwoocommerce-mini-cart-updated-by-ajax-every-second-change%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277870%2fwoocommerce-mini-cart-updated-by-ajax-every-second-change%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus