【问题标题】:WooCommerce - Subscriptions Add custom field in Edit SubscriptionWooCommerce - 订阅在编辑订阅中添加自定义字段
【发布时间】:2021-07-01 00:56:33
【问题描述】:

我正在使用Wordpress WooCommerceWooCommerce Subscriptions 插件,它的工作方式符合我的预期。

但是,我需要在 Edit Subscription 页面中添加一个自定义字段,因此我按照下面的钩子和代码在 Edit Subscription 中添加我的新字段页面。

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 中,它只会在页面处于编辑模式时显示我的自定义字段。所以在 Edit Subscription 页面中,它显示了我的自定义字段,我可以使用下面的钩子保存数据。

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']));
}

现在我的查询是,当我进入 Edit Order 页面时,我也可以看到这个新字段。我不想在此页面中显示这个新的自定义字段。

我试图找到各种钩子并尝试了一些,但没有一个适合我。

有人可以指导我如何仅在 Edit Subscription -page 中显示我的新自定义字段 .. 我如何隐藏 Edit Order Page 的这个新字段?

我们将不胜感激任何帮助或指导。

谢谢

【问题讨论】:

  • 可以通过 CSS 隐藏。
  • 感谢@Bhautik 的回复。我应该怎么做才能检查css?
  • 你想隐藏这个Mindesk VAR Client User:字段?
  • 是的,正是.. ..

标签: wordpress woocommerce hook-woocommerce woocommerce-subscriptions


【解决方案1】:

将这两个hide-if-adminmindesk-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 );

【讨论】:

  • 不..它不工作..它仍然显示在编辑订单页面中
  • 可以分享一下编辑订单页面的截图和代码吗?
  • 当然让我分享
  • 实际上它从两者中删除..编辑订阅页面以及编辑订单页面..
  • 我想在 Edit Subscription 页面中显示 .. 并且只在 Edit Orders 页面中隐藏
猜你喜欢
  • 2021-05-07
  • 2018-02-06
  • 1970-01-01
  • 2011-03-10
  • 2022-08-19
  • 2019-01-03
  • 2017-03-11
  • 2021-02-18
  • 1970-01-01
相关资源
最近更新 更多