php - Ajax 添加到购物车在 WordPress 5.6 中不起作用

标签 php jquery ajax wordpress woocommerce

此代码应该使用ajax将产品添加到产品页面中的woocommerce购物车,当wordpress 5.6发布时一切都可以购买,但当更新Jquery库时此代码不适用于变体产品。请帮助解决这个 Jquery 冲突。这个问题不会出现在简单的产品中,而且简单的产品添加到购物车时效果很好。

<?php

/**
 * JS for AJAX Add to Cart handling
 */
function ace_product_page_ajax_add_to_cart_js() {
    global $post;
    if('product' == $post->post_type) {
    ?>
    <script type="text/javascript" charset="UTF-8">
        jQuery(function($) {

            $('form.cart').on('submit', function(e) {
                e.preventDefault();

                var form = $(this);
                form.block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });

                var formData = new FormData(form.context);
                formData.append('add-to-cart', form.find('[name=add-to-cart]').val() );

                // Ajax action.
                $.ajax({
                    url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'ace_add_to_cart' ),
                    data: formData,
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    complete: function( response ) {
                        response = response.responseJSON;

                        if ( ! response ) {
                            return;
                        }

                        if ( response.error && response.product_url ) {
                            window.location = response.product_url;
                            return;
                        }

                        // Redirect to cart option
                        if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
                            window.location = wc_add_to_cart_params.cart_url;
                            return;
                        }

                        var $thisbutton = form.find('.single_add_to_cart_button'); //
//                      var $thisbutton = null; // uncomment this if you don't want the 'View cart' button

                        // Trigger event so themes can refresh other areas.
                        $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );

                        // Remove existing notices
                        $( 'form.cart .added_to_cart' ).remove();
                        $( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
                        // Add new notices
                        form.closest('.product').before(response.fragments.notices_html)

                        form.unblock();
                    }
                });
            });
        });
    </script><?php 
    }
}
add_action( 'wp_footer', 'ace_product_page_ajax_add_to_cart_js' );

/**
 * Add to cart handler.
 */
function ace_ajax_add_to_cart_handler() {
    WC_Form_Handler::add_to_cart_action();
    WC_AJAX::get_refreshed_fragments();
}
add_action( 'wc_ajax_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );
add_action( 'wc_ajax_nopriv_ace_add_to_cart', 'ace_ajax_add_to_cart_handler' );

// Remove WC Core add to cart handler to prevent double-add
remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 );

/**
 * Add fragments for notices.
 */
function ace_ajax_add_to_cart_add_fragments( $fragments ) {
    $all_notices  = WC()->session->get( 'wc_notices', array() );
    $notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );

    ob_start();
    foreach ( $notice_types as $notice_type ) {
        if ( wc_notice_count( $notice_type ) > 0 ) {
            wc_get_template( "notices/{$notice_type}.php", array(
                'notices' => array_filter( $all_notices[ $notice_type ] ),
            ) );
        }
    }
    $fragments['notices_html'] = ob_get_clean();

    wc_clear_notices();

    return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'ace_ajax_add_to_cart_add_fragments' );

最佳答案

通过更改以下行解决了问题:

 var formData = new FormData(form.context);

至:

 var formData = new FormData(this);

关于php - Ajax 添加到购物车在 WordPress 5.6 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65351874/

相关文章:

php - 使用 PHP/MySQL 替换表中的多个字符串

javascript - 如何将 slider 与刻度同步以使其响应?

javascript - 如何使用 Django 接收 ajax 请求?

javascript - 根据用户 ID 限制下拉选项

php - 无法运行查询 : SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

php - INSERT INTO 2表查询

php - ZendFramework 2.0.0beta4 服务管理器配置-子键区别

javascript - 创建iframe中的js对象

聚光灯后 jQuery 重置回 css

php - Apache 不会在轮询时提供服务