2017-05-25 131 views
0

Hi WooCommerce ninjas!隐藏/更改WooCommerce管理员通知

我正在开发WooCommerce插件,每当我提交表单(保存更改按钮)在顶部显示更新通知'您的设置已保存。'。如何隐藏或更改此通知,我使用woocommerce_show_admin_notice过滤器,但不能在我的插件类中使用。以下是我的插件中的代码的一部分。任何想法钩谁会是有用的?

非常感谢!

<?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; 
    } 

回答

1

我看不到任何适当的钩子可以用来消除这种情况。

但是这两个似乎工作。

我的第一个想法是重新加载页面,以便设置文本在当时消失。像这样的东西。

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(); 
    } 
} 

然后我的第二个选择是如果你想改变文本,我们可以使用过滤器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; 
} 

请注意,此代码仅仅是一个示例,并且可用于贝宝设置。改变所需的一切。

+0

感谢您帮助@Reigel,实际上我使用css来隐藏无用部分,然后扩展了'display_notice'方法来绘制错误。 – htmlbrewery