5

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>

1 Answer 1

6

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.

4
  • Could you advise me on how to do this please Commented Feb 18, 2015 at 14:29
  • @Neil If I use wc_print_notice( $message, $notice_type ); it works as I need, but the message is not at the top of the page Commented Feb 18, 2015 at 14:32
  • Success is the default notice type so wc_add_notice($message) should be sufficient unless you are on a template that doesn't print WooCommerce notices. Commented Feb 18, 2015 at 16:01
  • 1
    Make sure your template has wc_print_notices(); somewhere in it in order to print the notices. Commented Feb 18, 2015 at 16:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.