wordpresswoocommercehook-woocommercehook-wordpress

Hide / Change WooCommerce admin notice


Hi WooCommerce ninjas!

I'm developing WooCommerce plugin and every time when I submit form (Save Changes button) on top shows update notice with 'Your settings have been saved.'. How I can hide or change this notice, I'm use woocommerce_show_admin_notice filter but don't work in my plugin class. Below is part of code from my plugin. Any ideas for hook who will be usefull?

Great thanks!

<?php 
class name_of_plugin extends WC_Payment_Gateway {
   public $error = '';

   // Setup our Gateway's id, description and other values
   function __construct() {
    $this->id = "name_of_plugin";
    $this->method_title = __( "Name", 'domain' );
    $this->method_description = __( "Gateway Plug-in for WooCommerce", 'domain' );
    $this->title = __( "TFS VPOS", 'domain' );
    $this->icon = null;
    $this->has_fields = false;
    $this->init_settings();
    $this->init_form_fields();
    $this->title = $this->get_option( 'title' );


    $this->testurl = 'https://example.com/payment/api';
    $this->liveurl = 'https://example.com/payment/api';


    // Save settings
    if ( is_admin()) {
        add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
    }

    // Disable Admin Notice
    add_filter( 'woocommerce_show_admin_notice', array( $this, 'shapeSpace_custom_admin_notice' ), 10, 2 );
    // add the filter 
    add_filter( 'woocommerce_add_error', array( $this, 'filter_woocommerce_add_notice_type' ), 10, 1 ); 

   } // End __construct()

  // display custom admin notice
  public function filter_woocommerce_add_notice_type($true, $notice ) {
    // magic happen here...
    return $true;
  }

Solution

  • I can't see any proper hook that can be used to remove this.

    But these 2 seems to work.

    My first thought is to reload the page so the settings text will be gone at the time. Something like this.

    add_action( 'woocommerce_sections_checkout', 'woocommerce_sections_checkout' );
    function woocommerce_sections_checkout() {
    
        if ( isset( $_GET['section'] ) && isset( $_REQUEST['_wpnonce'] ) && ( $_GET['section'] === 'paypal' ) 
            && wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
    
            WC_Admin_Settings::add_message( __( 'Your settings have been saved!', 'woocommerce' ) );
            wp_safe_redirect( wp_get_raw_referer() );
            exit();
        }
    }
    

    Then my second option is if you wanted to change the text, we can use the filter gettext.

    add_filter( 'gettext', 'woocommerce_save_settings_text', 20, 3 );
    function woocommerce_save_settings_text( $translated_text, $text, $domain ) {
    
        if ( ( $domain == 'woocommerce' ) && isset( $_GET['section'] ) && ( $_GET['section'] === 'paypal' ) ) {
            switch ( $translated_text ) {
                case 'Your settings have been saved.' :
                    $translated_text = __( 'Your awesome settings have been saved.', 'woocommerce' );
                    break;
            }
        }
        return $translated_text;
    }
    

    Please do note that this code is just an example and will work for paypal settings. Change everything that is needed.