I am using a quick view plugin, (https://es.wordpress.org/plugins/woocommerce-lightbox/) and I am looking for a way to add products to my cart without redirecting or showing the loading icon when adding products to the cart in the quick view. In the following website it's working on the variation product in the shop page: http://dev-ak.com/woocommerce-dev/temp/shop/ But I would also like to have it work for the simple products in my quick view window.

I know how to change the redirect link to link back to the shop page, but that is not what I am looking for, it needs to be without reloading. I also tried a code that removes the redirect after adding a product to the cart but that didn't work.

For the example website the following codes were used:

/**
 * Customise variable add to cart button for loop.
 *
 * Remove qty selector and simplify.
 */
  function iconic_loop_variation_add_to_cart_button()
{
    global $product;

    ?>
    <div class="woocommerce-variation-add-to-cart variations_button">
        <button   type="submit" class="custom_add_to_cart single_add_to_cart_button button"><?php echo esc_html($product->single_add_to_cart_text()); ?></button>
        <input type="hidden" name="add-to-cart" value="<?php echo absint($product->get_id()); ?>" />
        <input type="hidden" name="product_id" value="<?php echo absint($product->get_id()); ?>" />
        <input type="hidden" name="variation_id" class="variation_id" value="0" />
    </div>
    <?php
}

Ajax code:

 jQuery(document).ready(function ($) {
  "use strict";

  $('.custom_add_to_cart').click(function (e) {
    e.preventDefault();
    var id = $(this).next().next().attr('value');
    var data = {
      product_id: id,
      quantity: 1
    };
    $(this).parent().addClass('loading');
                   $.post(wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint     %%', 'add_to_cart'), data, function (response) {

      if (!response) {
        return;
      }
      if (response.error) {
        alert("Custom Massage ");
        $('.custom_add_to_cart').parent().removeClass('loading');
        return;
      }
      if (response) {

        var url = woocommerce_params.wc_ajax_url;
        url = url.replace("%%endpoint%%", "get_refreshed_fragments");
        $.post(url, function (data, status) {
              $(".woocommerce.widget_shopping_cart").html(data.fragments["div.widget_shopping_cart_content"]);
          if (data.fragments) {
            jQuery.each(data.fragments, function (key, value) {

              jQuery(key).replaceWith(value);
            });
          }
          jQuery("body").trigger("wc_fragments_refreshed");
        });
        $('.custom_add_to_cart').parent().removeClass('loading');

      }

    });

 });
});

0 Answers