0

I need to display a message to a customer when the quantity exceeds a certain calculation. I would like to make this fail validation and post a message. I can do so with add to cart validation, but I can never get a message to display in woocommerce blocks when putting a notice in, what is the proper way to do this?

The first function works great, but works on page load.

The second does nothing, but would need to run on AJAX

FYI, I am using woocommerce blocks in the gutenburg style WP editor for the cart and checkout pages

add_action('woocommerce_add_to_cart_validation', 'gb_wc_woocommerce_add_to_cart_validation', 10, 6);
function gb_wc_woocommerce_add_to_cart_validation($passed, $product_id, $quantity, $variation_id = 0, $variations = null, $cart_item_data = null) {

    //TODO check if this causes a cart stock fail, if so return false to prevent adding to cart

    wc_add_notice(sprintf(__('Returning true to allow this add to cart operation. Product:Variation ID passed is: %s:%s with quantity of: %s', 'woocommerce'), $product_id, $variation_id, $quantity), 'error');
    return true;
}



add_action('woocommerce_update_cart_validation', 'my_woocommerce_check_cart_items', 10, 0);
function my_woocommerce_check_cart_items_test() {
    
    wc_add_notice( sprintf( __( 'This is an update cart fail notice which never appears on the front end.', 'woocommerce' ), $_product->get_name() ), 'error' );
    return false; 
}

source: https://woocommerce.github.io/code-reference/files/woocommerce-includes-class-wc-form-handler.html#source-view.697

EDIT: I found this questions where an answer provides similar functionality, but their answer still doesn't post the error in my application. Perhaps there is an issue with woocommerce not displaying error messages?

Source: Limit the number of cart items in Woocommerce

// Checking and validating when updating cart item quantities when products are added to cart
add_filter( 'woocommerce_update_cart_validation', 'only_six_items_allowed_cart_update', 10, 4 );
function only_six_items_allowed_cart_update( $passed, $cart_item_key, $values, $updated_quantity ) {

    $cart_items_count = WC()->cart->get_cart_contents_count();
    $original_quantity = $values['quantity'];
    $total_count = $cart_items_count - $original_quantity + $updated_quantity;

    if( $cart_items_count > 6 || $total_count > 6 ){
        // Set to false
        $passed = false;
        // Display a message
         wc_add_notice( __( "You can’t have more than 6 items in cart", "woocommerce" ), "error" );
    }
    return $passed;
}

1
  • When using Cart and/or Checkout Blocks, most of all previous hooks doesn't work anymore. Note that Cart or Checkout Blocks only allows very few customizations. Commented Dec 8, 2024 at 12:39

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.