【问题标题】:Error Updating Cart Item Quantity Using AJAX - WooCommerce使用 AJAX 更新购物车商品数量时出错 - WooCommerce
【发布时间】:2015-11-21 08:34:45
【问题描述】:

我正在尝试使用 AJAX 更新我的 WooCommerce 购物车中的商品数量。当购物车更新时,我希望总价格反映新价格。我基本上是从WooCommerce - auto update total price when quantity changed 复制的,但做了一些改动。

他们的代码中还有一些我不理解的东西,比如“rf_cart_params” - 似乎该变量是在他们提供的之外的某个地方定义的。

在我的functions.php中有

function enqueue_cart_qty_ajax() {

    wp_register_script( 'cart-qty-ajax-js', get_template_directory_uri() . '/js/cart-qty-ajax.js', array( 'jquery' ), '', true );
    wp_localize_script( 'cart-qty-ajax-js', 'cart_qty_ajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    wp_enqueue_script( 'cart-qty-ajax-js' );

}
add_action('wp_enqueue_scripts', 'enqueue_cart_qty_ajax');

function ajax_qty_cart() {
    check_ajax_referer( 'ajax_qty_cart', 'security' );

    // Skip product if no updated quantity was posted or no hash on WC_Cart
    if( !isset( $_POST['hash'] ) || !isset( $_POST['quantity'] ) ){
        exit;
    }

    $cart_item_key = $_POST['hash'];

    if( !isset( WC()->cart->get_cart()[ $cart_item_key ] ) ){
        exit;
    }

    $values = WC()->cart->get_cart()[ $cart_item_key ];

    $_product = $values['data'];

    // Sanitize
    $quantity = apply_filters( 'woocommerce_stock_amount_cart_item', apply_filters( 'woocommerce_stock_amount', preg_replace( "/[^0-9\.]/", '', filter_var($_POST['quantity'], FILTER_SANITIZE_NUMBER_INT)) ), $cart_item_key );

    if ( '' === $quantity || $quantity == $values['quantity'] )
        exit;

    // Update cart validation
    $passed_validation  = apply_filters( 'woocommerce_update_cart_validation', true, $cart_item_key, $values, $quantity );

    // is_sold_individually
    if ( $_product->is_sold_individually() && $quantity > 1 ) {
        wc_add_notice( sprintf( __( 'You can only have 1 %s in your cart.', 'woocommerce' ), $_product->get_title() ), 'error' );
        $passed_validation = false;
    }

    if ( $passed_validation ) {
        WC()->cart->set_quantity( $cart_item_key, $quantity, false );
    }

    // Recalc our totals
    WC()->cart->calculate_totals();
    woocommerce_cart_totals();
    exit;
}

add_action('wp_ajax_qty_cart', 'ajax_qty_cart');
add_action('wp_ajax_nopriv_qty_cart', 'ajax_qty_cart');

在 cart-qty-ajax.js 我有

jQuery( function( $ ) {

    // wc_cart_params is required to continue, ensure the object exists
    if ( typeof wc_cart_params === 'undefined' ) {
        return false;
    }

    $( document ).on( 'change', '.quantity, input[type=number]', function() {
        var qty = $( this ).val();
        var currentVal  = parseFloat( qty);

        //$( 'div.cart_totals' ).block({ message: null, overlayCSS: { background: '#fff url(' + wc_cart_params.ajax_loader_url + ') no-repeat center', backgroundSize: '16px 16px', opacity: 0.6 } });

        var item_hash = $( this ).attr( 'name' ).replace(/cart\[([\w]+)\]\[qty\]/g, "$1");
        var data = {
            action: 'qty_cart',
            security: cart_qty_ajax.ajax_qty_cart_nonce,
            quantity: currentVal,
            hash : item_hash 
        };

        $.post( cart_qty_ajax.ajax_url, data, function( response ) {

            console.log( data );
            $( 'div.cart_totals' ).replaceWith( response );
            $( 'body' ).trigger( 'qty_cart' );

        });
        return false;      

    });
});

数量按钮的HTML如下:

<td class="product-quantity">
    <div class="quantity">
        <input type="number" step="1" min="0"
         name="cart[2bcc28b6c27e9885a4735544844c57fc][qty]"
         value="4" title="Qty" class="input-text qty text" size="4">
    </div>
</td>

当我控制台记录我得到的数据时

Object {action: "qty_cart", security: undefined, quantity: 4, hash: "2bcc28b6c27e9885a4735544844c57fc"}

当我改变项目的数量时,输出的响应只是“-1”

有什么想法会出错吗?
提前致谢!

【问题讨论】:

  • 在本地化 ajax 调用之前尝试将您的脚本排入队列。 wp_register_script( 'cart-qty-ajax-js', get_template_directory_uri() . '/js/cart-qty-ajax.js', array( 'jquery' ), '', true ); wp_enqueue_script( 'cart-qty-ajax-js' ); wp_localize_script( 'cart-qty-ajax-js', 'cart_qty_ajax', array( 'ajax_url' =&gt; admin_url( 'admin-ajax.php' ) ) );
  • 感谢您的帮助,但不幸的是它似乎对结果没有任何影响。

标签: php jquery ajax wordpress woocommerce


【解决方案1】:

这行代码有问题

check_ajax_referer('ajax_qty_cart', 'security');

我不知道到底出了什么问题(试图为我自己的 ATM 解决这个问题),但如果您对此行发表评论 - 您将得到正确的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2019-10-08
    • 2016-10-09
    • 2018-07-29
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    相关资源
    最近更新 更多