cart->add_to_cart() with its full signature (which DOES support * variations), then returns the same response shape WooCommerce's own * endpoint returns (fragments, cart_hash) so existing WooCommerce fragment * refresh / mini-cart behavior keeps working unchanged. */ class ADNVG_Cart_Ajax { public function __construct() { add_action( 'wp_ajax_adnvg_add_to_cart', array( $this, 'handle_add_to_cart' ) ); add_action( 'wp_ajax_nopriv_adnvg_add_to_cart', array( $this, 'handle_add_to_cart' ) ); } public function handle_add_to_cart() { check_ajax_referer( 'adnvg_add_to_cart', 'nonce' ); if ( ! isset( $_POST['parent_id'], $_POST['variation_id'] ) ) { wp_send_json( array( 'error' => true ) ); } $parent_id = isset( $_POST['parent_id'] ) ? absint( wp_unslash( $_POST['parent_id'] ) ) : 0; $variation_id = isset( $_POST['variation_id'] ) ? absint( wp_unslash( $_POST['variation_id'] ) ) : 0; $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( sanitize_text_field( wp_unslash( $_POST['quantity'] ) ) ); $variation_attributes = array(); if ( isset( $_POST['attributes'] ) && is_array( $_POST['attributes'] ) ) { $raw_attributes = array_map( 'sanitize_text_field', wp_unslash( $_POST['attributes'] ) ); foreach ( $raw_attributes as $key => $value ) { // Keys already arrive as 'attribute_pa_color' etc. $variation_attributes[ sanitize_key( $key ) ] = $value; } } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- this is WooCommerce's own existing hook, invoked so our custom endpoint respects the same third-party add-to-cart validation rules a native WooCommerce add-to-cart request would. $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $parent_id, $quantity, $variation_id, $variation_attributes ); if ( $passed_validation ) { $cart_item_key = WC()->cart->add_to_cart( $parent_id, $quantity, $variation_id, $variation_attributes ); } else { $cart_item_key = false; } if ( $cart_item_key ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WooCommerce's own hook, fired so other plugins listening for a native add-to-cart still receive it from our endpoint too. do_action( 'woocommerce_ajax_added_to_cart', $parent_id ); if ( class_exists( 'WC_AJAX' ) && method_exists( 'WC_AJAX', 'get_refreshed_fragments' ) ) { // Reuse WooCommerce's own fragment-building — keeps mini-cart, // cart count, etc. in sync exactly like a native add would. WC_AJAX::get_refreshed_fragments(); return; // get_refreshed_fragments() already sends JSON + dies. } wp_send_json( array( 'error' => false ) ); } // Surface WooCommerce's own notice messages so failures are // diagnosable instead of a bare generic error. $debug_messages = array(); if ( function_exists( 'wc_get_notices' ) ) { $error_notices = wc_get_notices( 'error' ); foreach ( $error_notices as $notice ) { $debug_messages[] = is_array( $notice ) && isset( $notice['notice'] ) ? $notice['notice'] : $notice; } wc_clear_notices(); } wp_send_json( array( 'error' => true, 'product_url' => get_permalink( $parent_id ), 'debug' => $debug_messages, ) ); } }