Adding of custom cart item data via Ajax Add to Cart

Question

I’m using this plugin – Woocommerce PDF Vouchers – and it works great out of the box. However, after customizing my theme’s ‘add to cart’ to AJAXm it’s not displaying the custom data in both the mini cart and cart page. It works fine if i revert to the default add to cart though so there’s definitely smth wrong with my AJAX callback. This is what I’m using:

// JS
$add_to_cart.on(‘click’, function(e) {
    e.preventDefault();
    var var_id = $(this).siblings(‘.variation_id’).val();
    var prod_id = $(this).siblings(‘input[name=“product_id”]‘).val();
    var item = {};
    var $variation_form = $( this ).closest( ‘.variations_form’ );
    var variations = $variation_form.find( ‘select[name^=attribute]’ );
    var mini_cart = $(‘.mini-cart-menu-item’);
    var mini_cart_contents = $(‘.widget_shopping_cart’);
    var url = ajax_obj.ajax_url;
    var data = {
        action: ‘nerb_ajax_add_to_cart_woo’,
        ‘product_id’: prod_id,
        ‘variation_id’: var_id,
        ‘variation’: item
    }

    variations.each( function()  {
        var $this = $(this);
        attributeName = $this.attr( ‘name’ );
        attributevalue = $this.val();
        item[attributeName] = attributevalue;
    });

    $.post( url, data, function(response) {         
        if (response) {
            mini_cart.html(response.fragments['a.cart-contents']);
            mini_cart_contents.html(
             response.fragments['div.widget_shopping_cart_content']
            );
        }       
    });
})

//PHP AJAX Callback
function nerb_ajax_add_to_cart_woo() {
    $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
    $product = wc_get_product( $product_id );
    $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
    $variation_id = $_POST['variation_id'];
    $variations  = $_POST['variation'];
    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data );

    if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) ) {
        do_action( 'woocommerce_ajax_added_to_cart', $product_id );
        if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
            wc_add_to_cart_message( $product_id );
        }
        // Return fragments
        WC_AJAX::get_refreshed_fragments();
    } 
    die();  
}

add_action( 'wp_ajax_nerb_ajax_add_to_cart_woo', 'nerb_ajax_add_to_cart_woo' );
add_action( 'wp_ajax_nopriv_nerb_ajax_add_to_cart_woo', 'nerb_ajax_add_to_cart_woo' );

I believe it’s smth to do with ‘var item = {}’ in my JS function. I guess I could select the custom fields and assign the value to the variable but i think it doesnt make sense to have to re-do again if I add new custom fields in the future. So I’m not sure what values to assign to ‘var item’

0
Edmund Chan 2 years 2020-01-05T09:41:59-05:00 0 Answers 86 views 0

Leave an answer

Browse
Browse