wordpress - WooCommerce - 订阅在编辑订阅中添加自定义字段
问题描述
我正在使用Wordpress WooCommerce和WooCommerce 订阅插件,它按照我的预期工作。
但是,我需要在编辑订阅页面中添加一个自定义字段,因此我按照下面的钩子和代码在编辑订阅页面中添加我的新字段。
add_action('woocommerce_admin_order_data_after_order_details', 'showWCSubscriptionCustomFields');
function showWCSubscriptionCustomFields($subscription) {
$currentPage = get_current_screen();
// If page is "Edit Subscription" page, then only show
if ($currentPage->action == 'add')
return;
// Getting all the users
$mindeskUsers = getAllUsers();
?>
<br class="clear" />
<p class="form-field form-field-wide">
<label for="mindesk_wc_subscriptions_var_client_user_id">Mindesk VAR Client User:</label>
<?php
$selectedUser = get_post_meta($subscription->get_id(), 'mindesk_wc_subscriptions_var_client_user_id', true);
echo getUsersListSelect('mindesk_wc_subscriptions_var_client_user_id', $selectedUser, $mindeskUsers, 'mindesk_select2');
?>
</p>
<?php
}
这对我来说很好......在这里我正在检查页面是否没有添加更多内容,并且仅当页面处于编辑模式时才会显示我的自定义字段。所以在编辑订阅页面中,它显示了我的自定义字段,我可以使用下面的钩子保存数据。
add_action('woocommerce_process_shop_order_meta', 'saveWCSubscriptionCustomFields');
function saveWCSubscriptionCustomFields($subscription_id) {
// wc_clean() and wc_sanitize_textarea() are WooCommerce sanitization functions
update_post_meta($subscription_id, 'mindesk_wc_subscriptions_var_client_user_id', wc_clean($_POST['mindesk_wc_subscriptions_var_client_user_id']));
}
现在我的查询是,当我进入编辑订单页面时,我也能够看到这个新字段。我不想在此页面中显示这个新的自定义字段。
我试图找到各种钩子并尝试了一些,但没有一个适合我。
有人可以指导我如何仅在编辑订阅页面中显示我的新自定义字段..以及如何隐藏编辑订单页面的这个新字段?
任何帮助或指导将不胜感激。
谢谢
解决方案
将此二类hide-if-admin
和添加mindesk-var
到您的p
标签中。检查下面的代码。
add_action('woocommerce_admin_order_data_after_order_details', 'showWCSubscriptionCustomFields');
function showWCSubscriptionCustomFields($subscription) {
$currentPage = get_current_screen();
// If page is "Edit Subscription" page, then only show
if ($currentPage->action == 'add')
return;
// Getting all the users
$mindeskUsers = getAllUsers();
?>
<br class="clear" />
<p class="form-field form-field-wide mindesk-var hide-if-admin">
<label for="mindesk_wc_subscriptions_var_client_user_id">Mindesk VAR Client User:</label>
<?php
$selectedUser = get_post_meta($subscription->get_id(), 'mindesk_wc_subscriptions_var_client_user_id', true);
echo getUsersListSelect('mindesk_wc_subscriptions_var_client_user_id', $selectedUser, $mindeskUsers, 'mindesk_select2');
?>
</p>
<?php
}
function hide_mindesk_var_client_user(){
?>
<style type="text/css">
.post-type-shop_order .mindesk-var.hide-if-admin{display: none;}
</style>
<?php
}
add_action( 'admin_footer', 'hide_mindesk_var_client_user', 10, 1 );
推荐阅读
- r - 小鼠的 pool.compare 为 lmerTest 模型提供“错误:类调用对象的无一目了然方法”
- python - Python 3.6 无法导入 gnuradio (3.7.9)
- c# - 如何在摘要中包含强类型泛型的“查看”参考?
- karate - 是否可以在要断言的请求中提取其中一个键值对的值
- excel - 每次运行我的 VBA 宏时,启用宏的工作簿的文件大小都会变大?
- neo4j - Neo4J - WHERE ALL 子句正在破坏节点上的匹配
- python - matplotlib 事件的退出递归
- jms - IBM MQ 获取带有法语符号的消息
- reactjs - 如何将翻译与组件一起使用以使其对单元测试有用
- javascript - Javascript - 如何生成带有哔哔声的 WAV 文件?