简体   繁体   English

添加 Ajax 添加到购物车按钮到首页

[英]Add Ajax Add To Cart Button to Front Page

I'm trying to add ajax to add to cart button on front page.我正在尝试添加 ajax 以添加到首页上的购物车按钮。

The setup is using Divi.该设置使用的是 Divi。 Divi's woo product module does not display Add-to-cart button. Divi 的 woo 产品模块不显示添加到购物车按钮。 I use the below to display add-to-cart button on front page.我使用下面的代码在首页上显示添加到购物车按钮。 That works but the only issue is the Ajax is not working on front page.这行得通,但唯一的问题是 Ajax 在首页上不起作用。 I've enabled "Enable AJAX add to basket buttons on archives" from Woocommerce settings.我已经从 Woocommerce 设置中启用了“启用 AJAX 添加到存档的购物篮按钮”。

add_action('template_redirect', 'work_only_on_front_page', 10);
   function work_only_on_front_page(){
    if ( is_front_page() ) {
 add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_add_to_cart', 10);
   }
   }

Below works well on other pages other than the front page.下面在首页以外的其他页面上效果很好。

(function ($) {

    $(document).on('click', '.single_add_to_cart_button', function (e) {
        e.preventDefault();

        var $thisbutton = $(this),
                $form = $thisbutton.closest('form.cart'),
                id = $thisbutton.val(),
                product_qty = $form.find('input[name=quantity]').val() || 1,
                product_id = $form.find('input[name=product_id]').val() || id,
                variation_id = $form.find('input[name=variation_id]').val() || 0;

        var data = {
            action: 'woocommerce_ajax_add_to_cart',
            product_id: product_id,
            product_sku: '',
            quantity: product_qty,
            variation_id: variation_id,
        };

        $(document.body).trigger('adding_to_cart', [$thisbutton, data]);

        $.ajax({
            type: 'post',
            url: wc_add_to_cart_params.ajax_url,
            data: data,
            beforeSend: function (response) {
                $thisbutton.removeClass('added').addClass('loading');
            },
            complete: function (response) {
                $thisbutton.addClass('added').removeClass('loading');
            },
            success: function (response) {

                if (response.error && response.product_url) {
                    window.location = response.product_url;
                    return;
                } else {
                    $(document.body).trigger('added_to_cart', [response.fragments, response.cart_hash, $thisbutton]);
                }
            },
        });

        return false;
    });
})(jQuery);
function woocommerce_ajax_add_to_cart_js() {
    if (is_product() || is_product_category() || is_shop() || is_front_page()) {
        wp_enqueue_script('woocommerce-ajax-add-to-cart', get_stylesheet_directory_uri() . '/assets/js/ajax-add-to-cart.js', array('jquery'), '', true);
    }
}
add_action('wp_enqueue_scripts', 'woocommerce_ajax_add_to_cart_js', 99);
add_action('wp_ajax_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');
add_action('wp_ajax_nopriv_woocommerce_ajax_add_to_cart', 'woocommerce_ajax_add_to_cart');
        
function woocommerce_ajax_add_to_cart() {

            $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));
            $quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']);
            $variation_id = absint($_POST['variation_id']);
            $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
            $product_status = get_post_status($product_id);

          if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) {

                do_action('woocommerce_ajax_added_to_cart', $product_id);

                if ('yes' === get_option('woocommerce_cart_redirect_after_add')) {
                    wc_add_to_cart_message(array($product_id => $quantity), true);
                }

                WC_AJAX :: get_refreshed_fragments();
            } else {

                $data = array(
                    'error' => true,
                    'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id));

                echo wp_send_json($data);
            }

            wp_die();
        }

  • Another thing to check is to ensure that the function woocommerce_ajax_add_to_cart_js() is being called and the script is being enqueued on the front page.另一件要检查的事情是确保调用 function woocommerce_ajax_add_to_cart_js() 并且脚本在首页上排队。
  • You can check the browser's console to see if there are any errors related to the Javascript code not being loaded.您可以检查浏览器的控制台,看看是否存在与未加载 Javascript 代码相关的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM