是否有可能在类中将GET变量从筛选器中取出?

时间:2020-12-28 作者:GeekyOwl

我试图从过滤器中取出一个变量

最后是return apply_filters( \'woocommerce_account_menu_items\', $items, $endpoints );

function wc_get_account_menu_items() {
    $endpoints = array(
        \'orders\'          => get_option( \'woocommerce_myaccount_orders_endpoint\', \'orders\' ),
        \'downloads\'       => get_option( \'woocommerce_myaccount_downloads_endpoint\', \'downloads\' ),
        \'edit-address\'    => get_option( \'woocommerce_myaccount_edit_address_endpoint\', \'edit-address\' ),
        \'payment-methods\' => get_option( \'woocommerce_myaccount_payment_methods_endpoint\', \'payment-methods\' ),
        \'edit-account\'    => get_option( \'woocommerce_myaccount_edit_account_endpoint\', \'edit-account\' ),
        \'customer-logout\' => get_option( \'woocommerce_logout_endpoint\', \'customer-logout\' ),
    );

    $items = array(
        \'dashboard\'       => __( \'Dashboard\', \'woocommerce\' ),
        \'orders\'          => __( \'Orders\', \'woocommerce\' ),
        \'downloads\'       => __( \'Downloads\', \'woocommerce\' ),
        \'edit-address\'    => _n( \'Addresses\', \'Address\', (int) wc_shipping_enabled(), \'woocommerce\' ),
        \'payment-methods\' => __( \'Payment methods\', \'woocommerce\' ),
        \'edit-account\'    => __( \'Account details\', \'woocommerce\' ),
        \'customer-logout\' => __( \'Logout\', \'woocommerce\' ),
    );

    // Remove missing endpoints.
    foreach ( $endpoints as $endpoint_id => $endpoint ) {
        if ( empty( $endpoint ) ) {
            unset( $items[ $endpoint_id ] );
        }
    }

    // Check if payment gateways support add new payment methods.
    if ( isset( $items[\'payment-methods\'] ) ) {
        $support_payment_methods = false;
        foreach ( WC()->payment_gateways->get_available_payment_gateways() as $gateway ) {
            if ( $gateway->supports( \'add_payment_method\' ) || $gateway->supports( \'tokenization\' ) ) {
                $support_payment_methods = true;
                break;
            }
        }

        if ( ! $support_payment_methods ) {
            unset( $items[\'payment-methods\'] );
        }
    }

    return apply_filters( \'woocommerce_account_menu_items\', $items, $endpoints );
}
如果我有这样的课

Class MyClass{
   public function __construct(){
          add_filter ( \'woocommerce_account_menu_items\', array($this, \'wooquickmy_grab_endpoints\'), 10 , 1 );
        add_action( \'admin_menu\', array( $this,\'wooquickmy_options_page\' ));
  }

   public function wooquickmy_options_page(){
         //get the return value from there wooquickmy_grab_endpoints
    }

public function wooquickmy_grab_endpoints($endpoints){
        return $endpoints ;
    }
}
是否有可能在我创建的操作中获得该返回值?对于这种情况,最好的选择是什么?

请帮忙

谢谢

1 个回复
SO网友:shanebp

筛选器有2个参数,而不是一个。

尝试:

add_filter ( \'woocommerce_account_menu_items\', array($this, \'wooquickmy_grab_endpoints\'), 10 , 2 );

public function wooquickmy_grab_endpoints($items, $endpoints){
        // do something with $endpoints
        return $items;
    }

相关推荐

Apply filters on date format

我使用Wordpress主题,其中格式日期和时间直接编码为两种格式:<?php the_time(\'d M\'); ?> <?php the_time(\'H:i\'); ?> 我想要的:显示wordpress选项中定义的日期和时间我不想在子主题中创建修改过的模板,我更喜欢使用函数。我已经在函数中添加了php。php此代码有效:add_filter( \'the_time\', \'changer_date_format\', 10, 1 ); //o