Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a woocommerce notice after form submit

I've got the following code which updates a VAT Numebr which is stored against the user meta. I was wondering if anyone knew how I could add Woocommerce Success message which says something along the lines of 'VAT Number Successfully Updated' once the form has been submitted and the page has refrwshed. With my current code, it works but the message only appears once I've refreshed the page after I've pressed save on the form.

<?php
        if(isset($_GET['vat_number'])) {
        $vat_number = $_GET['vat_number'];
        update_user_meta(get_current_user_id(), 'vat_number', $vat_number);
        $message = 'VAT Number successfully updated';
        $notice_type= success;
        wc_add_notice( $message, $notice_type ); 
        } ?>

        <?php $vat_number = get_user_meta( get_current_user_id(), 'vat_number', true ); ?>

        <form name="setprices" action="#" method="GET">

        <label for="lowPrice">Vat Number: </label>
        <input type="text" id="vat_number" name="vat_number" value="<?php echo $vat_number ?>"/>
        <input type="submit" value="update"/>
        </form>
like image 269
Adam Boustead Avatar asked Feb 18 '15 14:02

Adam Boustead


1 Answers

The wc_add_notice() function saves a notice for the next view. So that's normal behaviour. Your code should fire, save the notice and refresh or redirect the user.

like image 73
Niels van Renselaar Avatar answered Oct 26 '22 23:10

Niels van Renselaar