在Woocommerce的自定义“我的帐户”部分中获取$ _GET或$ _POST

时间:2018-11-27 11:22:22

标签: php wordpress post woocommerce account

我想在Woocommerce的“帐户”部分中加载输入表单。但是似乎它不是read形式,或者可以返回$_GET变量。

我添加了一个add_filter

add_filter('woocommerce_account_menu_items', 'flxz_recurring_link', 40);
function flxz_recurring_link($menu_links)
{
    $menu_links = array_slice($menu_links, 0, 5, true)
     + array('flx-subscription' => 'Subscription')
     + array_slice($menu_links, 5, null, true);
    return $menu_links;
}

然后调用端点

add_action('init', 'flxz_add_endpoint');
function flxz_add_endpoint()
{
   add_rewrite_endpoint('flx-subscription', EP_PAGES);
}

我想在这里生成一个表格

add_action('woocommerce_account_flx-subscription_endpoint', 'flxz_my_account_endpoint_content');
function flxz_my_account_endpoint_content()
{
    $user_info = get_userdata(get_current_user_id());
   var_dump( $_GET());
}

当我执行var_dump时,会引发此错误

Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\wp\wp-content\plugins\shop_recurring\index.php:42 Stack trace: #0 C:\xampp\htdocs\wp\wp-includes\class-wp-hook.php(286): flexzone_my_account_endpoint_content('') #1 C:\xampp\htdocs\wp\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #2 C:\xampp\htdocs\wp\wp-includes\plugin.php(453): WP_Hook->do_action(Array) #3 C:\xampp\htdocs\wp\wp-content\plugins\woocommerce\includes\wc-template-functions.php(2842): do_action('woocommerce_acc...', '') #4 C:\xampp\htdocs\wp\wp-includes\class-wp-hook.php(286): woocommerce_account_content('') #5 C:\xampp\htdocs\wp\wp-includes\class-wp-hook.php(310): WP_Hook->apply_filters('', Array) #6 C:\xampp\htdocs\wp\wp-includes\plugin.php(453): WP_Hook->do_action(Array) #7 C:\xampp\htdocs\wp\wp-content\plugins\woocommerce\templates\myaccount\my-account.php(34): do_action('woocommerce_acc...') #8 C:\xampp\htdocs\wp\wp-content\plugins\woocommerce\includes\wc-core-functions.php(208): include('C:\\xampp\\ht in C:\xampp\htdocs\wp\wp-content\plugins\shop_recurring\index.php on line 42

谢谢:)

enter image description here

<?php
/*
Plugin Name: Woocommerce Recurring
Plugin URI: https://localhost
Description: Recurring Plug-in for Wordpress
Version: 1.0
Author: Daan 
Author URI: https://localhost
License: MIT
 */

// Load Subscription link in Woocommerce
add_filter('woocommerce_account_menu_items', 'flxz_recurring_link', 40);
function flxz_recurring_link($menu_links)
{
    $menu_links = array_slice($menu_links, 0, 5, true)
     + array('flx-subscription' => 'Subscription')
     + array_slice($menu_links, 5, null, true);
    return $menu_links;
}

add_action('init', 'flxz_add_endpoint');
function flxz_add_endpoint()
{
   add_rewrite_endpoint('flx-subscription', EP_PAGES);
}

add_action('woocommerce_account_flx-subscription_endpoint', 'flxz_my_account_endpoint_content');
function flxz_my_account_endpoint_content()
{
    $user_info = get_userdata(get_current_user_id());
//    var_dump( $_GET());
print '
   <form action="" method="post"></form>
   <input type="text" name="Blablabla" id="Blablabla">
   </form>
   ';
}

1 个答案:

答案 0 :(得分:1)

您的代码中有很多错误……$ _POST可以正常工作。尝试以下重新访问的代码:

// Load Subscription link in Woocommerce
add_filter('woocommerce_account_menu_items', 'flxz_recurring_link', 40);
function flxz_recurring_link($menu_links)
{
    $menu_links = array_slice($menu_links, 0, 5, true)
     + array('flx-subscription' => 'Subscription')
     + array_slice($menu_links, 5, null, true);
    return $menu_links;
}

add_action('init', 'flxz_add_endpoint');
function flxz_add_endpoint()
{
   add_rewrite_endpoint('flx-subscription', EP_PAGES);
}

add_action('woocommerce_account_flx-subscription_endpoint', 'flxz_my_account_endpoint_content');
function flxz_my_account_endpoint_content()
{
    $user_info = get_userdata(get_current_user_id());

    echo '<form action="" method="post">
    <input type="text" name="blabla" id="Blablabla" value="'.$user_info->billing_email.'"><br><br>
    <input type="submit" class="button" value="Submit"></form>';

    echo '<br><p>' . ( isset($_POST['blabla']) ? 'Submited: ' . $_POST['blabla'] : 'Nothing is submited yet…' ) . '</p>';
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。