Woocommerce variation validation NOT working – add to cart
-
HELP!! I have spent hours trying to resolve this issue and I can’t find a solution.
I have products with variations. Without choosing a variation option, you can click “add to cart” button and the product gets added to the cart. (There is no alert/popup indicating that a variation needs to be chosen). Then a “generic” product gets added to the cart without the variation. I am unable to fulfill orders without it. Everything is set up properly for product attributes and variations on the back end.
This is the snippet from chrome dev tools for the page’s submit button when a variation is not chosen and yet it adds it still adds it to the cart.
(Note that the variation id value = 0 which I would assume means that it is “empty”<button type=”submit” class=”single_add_to_cart_button button alt”></button>
<input type=”hidden” name=”add-to-cart” value=”5963″>
<input type=”hidden” name=”product_id” value=”5963″>
<input type=”hidden” name=”variation_id” class=”variation_id” value=”0″>PLEASE HELP!!
Here is the validation filter function from the class-wc-form-handler.php
/**
* Handle adding variable products to the cart.
*
* @since 2.4.6 Split from add_to_cart_action.
* @throws Exception If add to cart fails.
* @param int $product_id Product ID to add to the cart.
* @return bool success or not
*/
private static function add_to_cart_handler_variable( $product_id ) {
$variation_id = empty( $_REQUEST[‘variation_id’] ) ? ” : absint( wp_unslash( $_REQUEST[‘variation_id’] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$quantity = empty( $_REQUEST[‘quantity’] ) ? 1 : wc_stock_amount( wp_unslash( $_REQUEST[‘quantity’] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$variations = array();foreach ( $_REQUEST as $key => $value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ‘attribute_’ !== substr( $key, 0, 10 ) ) {
continue;
}$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 && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variations ) ) {
wc_add_to_cart_message( array( $product_id => $quantity ), true );
return true;
}return false;
}The page I need help with: [log in to see the link]
- The topic ‘Woocommerce variation validation NOT working – add to cart’ is closed to new replies.