douwen5690 2018-08-13 17:41
浏览 52
已采纳

如何在没有重新加载woocommerce页面的情况下添加到购物车?

I'm building a website and I want to show the options of the products with variations on the main and shop page.

I use this code to acomplished that:

/**
 * Replace add to cart button in the loop.
 */
function iconic_change_loop_add_to_cart() {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
}

add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );

/**
 * Use single add to cart button for variable products.
 */
function iconic_template_loop_add_to_cart() {
    global $product;

    if ( ! $product->is_type( 'variable' ) ) {
        woocommerce_template_loop_add_to_cart();
    return;
    }

remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );

    woocommerce_template_single_add_to_cart();
}

/**
 * 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="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
}

And works great, but if you add a product to the cart, also sends you to the page of that product. So I add this code, so the page just reloads.

function iconic_add_to_cart_form_action( $redirect ) {
    if ( ! is_archive() ) {
        return $redirect;
    }

    return '';
}
add_filter( 'woocommerce_add_to_cart_form_action', 'iconic_add_to_cart_form_action' );

Can someone help me? I want to add the product withou reloading or sending the clientes to another page.

Thanks!!

  • 写回答

1条回答 默认 最新

  • dongzhong5756 2018-08-13 18:35
    关注

    Just change your iconic_loop_variation_add_to_cart_button function to this as i added an ID for this add to cart button:

    /**
     * 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
    }
    

    and here is the 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');
    
          }
    
        });
    
     });
    });
    

    I Just tested with your code on my local host and it's working fine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合