woocommerce_add_to_cart_validation

woocommerce_add_to_cart_validation

Appears in: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
Hook Type: filter
See hook in action

See hook in core

Displaying hooks found in version: woocommerce.9.8.1

apply_filters('woocommerce_add_to_cart_validation') is found 5 times:

  • /includes/class-wc-ajax.php line 469
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    $product_id        = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
    $product           = wc_get_product( $product_id );
    $quantity          = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_POST['quantity'] ) );
    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
    $product_status    = get_post_status( $product_id );
    $variation_id      = 0;
    $variation         = array();
     
    if ( $product && ProductType::VARIATION === $product->get_type() ) {
        $variation_id = $product_id;
  • /includes/class-wc-cart-session.php line 513
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
            $variations[ $meta->key ] = $meta->value;
        }
    }
     
    if ( ! apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data ) ) {
        continue;
    }
     
    // Add to cart directly.
    $cart_id          = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data );
    $product_data     = wc_get_product( $variation_id ? $variation_id : $product_id );
  • /includes/class-wc-form-handler.php line 869
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    * @return bool success or not
     */
    private static function add_to_cart_handler_simple( $product_id ) {
        $quantity          = empty( $_REQUEST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_REQUEST['quantity'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
     
        if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) ) {
            wc_add_to_cart_message( array( $product_id => $quantity ), true );
            return true;
        }
        return false;
  • /includes/class-wc-form-handler.php line 901
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    }
    $quantity_set = true;
     
    // Add to cart validation.
    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item, $quantity );
     
    // Suppress total recalculation until finished.
    remove_action( 'woocommerce_add_to_cart', array( WC()->cart, 'calculate_totals' ), 20, 0 );
     
    if ( $passed_validation && false !== WC()->cart->add_to_cart( $item, $quantity ) ) {
        $was_added_to_cart      = true;
  • /includes/class-wc-form-handler.php line 951
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
        $variations[ sanitize_title( wp_unslash( $key ) ) ] = wp_unslash( $value );
    }
     
    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations );
     
    if ( ! $passed_validation ) {
        return false;
    }
     
    // Prevent parent variable product from being added to cart.

See this hook used in plugins: