0

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

Community
  • 1
  • 1
chrismaaz
  • 127
  • 4
  • 12
  • When logged in, navigate to WooCommerce -> System Status -> Tools, scroll down and find "Template Debug Mode", ensure that its not enabled. – Anand Shah Oct 28 '15 at 10:12
  • Also, when I output the data return in the console from the jQuery AJAX call I get returned "0" when I am logged in. When I am not I get returned the HTML that **WC_AJAX::get_refreshed_fragments();** outputs the HTML I have defined in **woocommerce_header_add_to_cart_fragment( $fragments )** – chrismaaz Oct 28 '15 at 14:56

1 Answers1

0

An update, it seems to work if I add the following to my functions.php

add_action( 'wp_ajax_nopriv_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' );
add_action( 'wp_ajax_woocommerce_add_variation_to_cart', 'so_27270880_add_variation_to_cart' );

Meaning, that one is for logged in admins and one is for normal users. Not sure why I need both because every example I have seen with this kind of extension/override only mentions the wp_ajax_nopriv_$function

Hope this helps anyone who have the same problem as me. And if anyone could explain to me why this is needed, I would be happy to hear it.

chrismaaz
  • 127
  • 4
  • 12