首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从cart中删除Woocommerce ajax并更新cart数据返回零

从cart中删除Woocommerce ajax并更新cart数据返回零
EN

Stack Overflow用户
提问于 2016-12-09 08:02:28
回答 1查看 3.2K关注 0票数 2

我试图从cart.Suppose中减去一个产品或删除一个产品,我在购物车中有三个相同的项目,我想要减去一个,但是我使用的ajax没有工作,我在控制台日志上返回零。

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

代码语言:javascript
复制
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码

代码语言:javascript
复制
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传递的变量哈希中,nonce,qty都可以。

here获取php代码

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-09 08:49:50

您应该使用下面的类似代码:

代码语言:javascript
复制
    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' );

而不是

代码语言:javascript
复制
    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' );

更新

代码语言:javascript
复制
       type : 'POST',
        url : cart_ajax.ajaxurl,
        data : {
            'hash'     : hash,
            'nonce'    : nonce,
            'quantity' : qty,
            action : 'the_foody_removefrom_cart_process',
        },
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41055712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档