You can explore how WooCommerce adds items to the cart through ajax directly in the code .... the callback is in includes/class-wc-ajax.php . WooCommerce already does this on product "loops" (product archives), so I donโt think you need to reinvent the wheel, and if their existing code doesnโt work for what you are trying to do, then you should be able to borrow heavily From him.
At the beginning of this file there is a loop with all the actions of Ajax WooCommerce, but we can track that adding to the cart is basically this:
add_action( 'wp_ajax_nopriv_woocommerce_add_to_cart', array( 'WC_AJAX', 'add_to_cart' ) );
And this callback is a little further down the file:
public static function add_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'] ); $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 ) ) { 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 ); }
If you want to see their addition to the basket ajax call, the JS for it is in assest/js/frontend/add-to-cart.js .
EDIT
Now that I know that you want to add an option, perhaps we can fine-tune the above.
First, I think you need to pass the AJAX URL to your script:
wp_enqueue_script( 'wc-variation-add-to-cart', 'source-to-script/your-script.js' ); $vars = array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ); wp_localize_script( 'wc-variation-add-to-cart', 'WC_VARIATION_ADD_TO_CART', $vars );
Then your AJAX call will look something like this:
jQuery.ajax({ url: WC_VARIATION_ADD_TO_CART.ajax_url, data: { "action" : "woocommerce_add_variation_to_cart", "product_id" : "124", "variation_id" : "125", "quantity" : 1, "variation" : { "size" : "xl", "color": "pink" }, }, type: "POST" });
Basically, to add a specific option, you will need a change identifier in addition to all its specific parameters.
And finally, a new callback for the woocommerce_add_variation_to_cart ajax action will look like this:
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, $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 ); }
Basically, I just copy the WooCommerce approach and add to the 2 variables needed to add options. Totally untested, but I hope this helps.