Search code examples
wordpresswoocommercehook-woocommercewoocommerce-subscriptions

WooCommerce - Subscriptions Add custom field in Edit Subscription


I am using Wordpress WooCommerce and WooCommerce Subscriptions plugins and it's working as per my expectations.

However, I had a requirement to add a custom field in the Edit Subscription page hence I followed the below hook and code to add my new field in the Edit Subscription page.

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

}

This is working fine for me .. Here I am checking if the page is not in add more and it will show my custom field only when the page is in Edit mode. So in Edit Subscription page, its showing my custom field and I am able to save the data using below hook.

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

Now my query is, when I am going in to Edit Order page, then also I am able to see this new field. I do not want to show this new custom field in this page.

I have tried to find various hooks and tried a few but none of them working for me properly.

Can someone guide me how can I show my new custom field only in Edit Subscription -page .. and how can I hide this new field for Edit Order Page ?

Any help or guidance will be highly appreciated.

Thanks


Solution

  • Add this two-class hide-if-admin and mindesk-var to your p tag. check the below code.

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