Updating WooCommerce cart after adding a variable product (AJAX)

I am overriding the AJAX function to add a product using WooCommerce so I can send attributes to the cart as well (which doesn’t work out of the box).

I followed the instructions on Update WooCommerce Cart After Adding Variable Product Via AJAX – which does what I want – but it doesn’t work completely.

Everything is good when I am logged out. But when I log into WordPress the PHP override doesn’t work/gets ignored. I get no errors and no product is added to the cart. I tried many different approaches, but nothing helps.

function ajax_add_to_cart_script() {
    $vars = array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );
    wp_enqueue_script( 'wc-variation-add-to-cart', get_template_directory_uri() . '/js/jquery.add-to-cart.js', array( 'jquery' ), null, true);
    wp_localize_script( 'wc-variation-add-to-cart', 'WC_VARIATION_ADD_TO_CART', $vars );
}

add_action( 'wp_enqueue_scripts', 'ajax_add_to_cart_script' );
add_action( 'wp_ajax_nopriv_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' );

function so_27270880_add_variation_to_cart() {
    ob_start();

    $product_id        = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
    $quantity          = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] );
    $variation_id      = isset( $_POST['variation_id'] ) ? absint( $_POST['variation_id'] ) : '';
    $variations        = ! empty( $_POST['variation'] ) ? (array) $_POST['variation'] : '';
    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

    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();

    } else {

        // If there was an error adding to the cart, redirect to the product page to show any errors
        $data = array(
            'error' => true,
            'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
        );

        wp_send_json( $data );

    }

    die();
}

I am making sure that add_action( 'wp_ajax_nopriv_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' ); is added before the snippet to make sure it gets fired, which works when I am logged out – but not when I am logged in.

Anyone got any clue why?

Ping @helgatheviking

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s