简体   繁体   中英

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM