【问题标题】:Woocommerce ajax remove from cart and update cart data returns zeroWoocommerce ajax 从购物车中删除并更新购物车数据返回零
【发布时间】:2017-04-24 15:42:19
【问题描述】:

我正在尝试减去一个产品或从购物车中删除一个产品。假设我在购物车中有 3 个相同的项目,我想减去一个,但我使用的 ajax 不起作用。我在控制台日志上返回零。我认为这个为零来自 wp die()。但它预计会返回一些数据。

下面是我在functions.php中的代码

add_action( 'wp_enqueue_scripts', 'the_foody_enqueue_cartajax_scripts' );
if ( ! function_exists( 'the_foody_enqueue_cartajax_scripts' ) )
{ 
    function the_foody_enqueue_cartajax_scripts() {
        wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/ajax-cart.js', array('jquery'), '1.0', true );
        wp_localize_script( 'ajax-script', 'cart_ajax', array( 'ajaxurl' =>   admin_url( 'admin-ajax.php' ) ) );
    }
}

function the_foody_removefrom_cart_process(){
    if( !wp_verify_nonce( $_POST['nonce'], 'foody_reservation_nonce' ) ){
       die();
    }
    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 );

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

    // Recalc our totals
    WC()->cart->calculate_totals();
    return woocommerce_cart_totals();
}
add_action( 'wp_ajax_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );    // If called from admin panel
add_action( 'wp_ajax_nopriv_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );

还有JS代码

function removefromCart( hash, nonce, qty ){
    $.ajax({
        action : 'foody_removefrom_cart',
        type : 'POST',
        url : cart_ajax.ajaxurl,
        data : {
            'hash'     : hash,
            'nonce'    : nonce,
            'quantity' : qty,
        },
        beforeSend : function(){
            // codes to execute
        },
        success : function(response){
            console.log(response);

        }
    })
}

但它在控制台中只返回零。

在js中传递变量hash,nonce,qty都可以。

从here获得php代码

【问题讨论】:

    标签: php ajax wordpress woocommerce


    【解决方案1】:

    你应该使用下面的代码:

        function removefromCart( hash, nonce, qty ){
            $.ajax({
    
                type : 'POST',
                url : cart_ajax.ajaxurl,
                data : {
                    'hash'     : hash,
                    'nonce'    : nonce,
                    'quantity' : qty,
                    action : 'the_foody_removefrom_cart_process',
                },
                beforeSend : function(){
                    // codes to execute
                },
                success : function(response){
                    console.log(response);
    
                }
            })
        }
    
    
        add_action( 'wp_ajax_the_foody_removefrom_cart_process', 'the_foody_removefrom_cart_process' );    // If called from admin panel
        add_action( 'wp_ajax_nopriv_the_foody_removefrom_cart_process', 'the_foody_removefrom_cart_process' );
    

    而不是

        add_action( 'wp_ajax_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );    // If called from admin panel
        add_action( 'wp_ajax_nopriv_foody_removefrom_cart', 'the_foody_removefrom_cart_process' );
    

    更新

           type : 'POST',
            url : cart_ajax.ajaxurl,
            data : {
                'hash'     : hash,
                'nonce'    : nonce,
                'quantity' : qty,
                action : 'the_foody_removefrom_cart_process',
            },
    

    【讨论】:

    • 什么都没变。不是流程的问题。之前也正常
    • action : 'the_foody_removefrom_cart_process',应该在数据中。
    猜你喜欢
    • 2017-01-29
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多