if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) {

In this condition WC()->cart->add_to_cart($product_id, $quantity, $variation_id) getting false while add to cart.

function woocommerce_ajax_add_to_cart() {

    $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 = absint($_POST['variation_id']);
    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
    $product_status = get_post_status($product_id);

    if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && 'publish' === $product_status) {

        do_action('woocommerce_ajax_added_to_cart', $product_id);

        if ('yes' === get_option('woocommerce_cart_redirect_after_add')) {
            wc_add_to_cart_message(array($product_id => $quantity), true);
        WC_AJAX:: get_refreshed_fragments();
    } else {
        $data = array(
            'error' => true,
            'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id),
            '$passed_validation'=> $passed_validation,
            'WC()->cart->add_to_cart($product_id, $quantity, $variation_id)' => 'PID: '.$product_id.' Qty: '.$quantity.' VID: '.$variation_id,
            '$product_status' => ('publish' === $product_status)

        echo wp_send_json($data);


enter image description here

I have print the Product Id , Qty and Variation Id but that condition getting false, i'm not sure why.

enter image description here

  • At first glance add_to_cart puts its errors in wc_add_notice. Can you capture those and return them in the AJAX result? (If not I'd temporarily edit add_to_cart to trace them out e.g. with error_log so you can at least see them)
    – Rup
    Sep 29, 2021 at 8:37
  • However if this is a breaking change in WooCommerce I'd report it to them directly and ask for their help e.g. on developer.woocommerce.com. But you should ideally debug this yourself first, and work out how to include the wc_add_notice notices in your error response for the future.
    – Rup
    Sep 29, 2021 at 8:38
  • It looks like it's just wc_get_notices(). So I'd add 'notices' => wc_get_notices(), (or maybe wc_get_notices( 'error' )) to your error $data array and see what that gives you.
    – Rup
    Sep 29, 2021 at 8:42
  • @Rup Thank you for the reply. It's not a notice. I have built a custom form to get variable product and adding to the cart using ajax. It's working already but After updating the WooCommerce new version. It's not working and "WC()->cart->add_to_cart($product_id, $quantity, $variation_id)" getting false so I can not add into the cart the product.
    – Ravi
    Sep 29, 2021 at 12:40
  • 1
    Glad you fixed! I’m sure we’ve all done something like that.
    – Rup
    Sep 30, 2021 at 11:14


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.