1

In WooCommerce, I have enabled WooCommerce Subscriptions plugin and it's working fine as per my expectations.

However, I have a one custom requirement from client. I want to add custom field(s) while creating a product in Variable Subscriptions and Simple Subscriptions.

I have added custom field(s) in Variable Subscriptions using below code and it's working as per my expectations. Here is my code.

<?php

// Showing fields for variable subscriptions 
add_action('woocommerce_product_after_variable_attributes', 'show_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 3);

// Saving fields for variable subscriptions 
add_action('woocommerce_save_product_variation', 'save_WC_Product_Variable_Subscription_Variation_Custom_Fields', 10, 2);
    
function show_WC_Product_Variable_Subscription_Variation_Custom_Fields($loop, $variation_data, $variation) {
    woocommerce_wp_text_input(
        array(
            'id'            => "my_text_field{$loop}",
            'name'          => "my_text_field[{$loop}]",
            'value'         => get_post_meta($variation->ID, 'my_text_field', true),
            'label'         => __('Some label', 'woocommerce'),

        )
    );
}

function save_WC_Product_Variable_Subscription_Variation_Custom_Fields($variation_id, $loop) {

    if (empty($variation_id)) return;

    $text_field = $_POST['my_text_field'][$loop];
    update_post_meta($variation_id, 'my_text_field', esc_attr($text_field));
}

And here is how it looks like now working fine.

enter image description here

As you can see in screenshot above, the last field labeled as "Some label" .. my custom field added.

However, I want to add this same field which I set Simple Subscription. Here I mean.

enter image description here

As you can see, I want that same custom fields to show and save here as well ..

I have researched but yet not able to find any hook.

Can someone guide me please how can I achieve this.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mittul At TechnoBrave
  • 1,142
  • 3
  • 25
  • 70

1 Answers1

3

Updated:

To add a custom field to Admin product data settings > general tab on simple subscriptions:

// Showing custom fields on admin product settings "general" tab
add_action('woocommerce_product_options_general_product_data', 'add_admin_product_custom_fields', 10, 3);
function add_admin_product_custom_fields() {
    global $post;
    
    echo '<div class="product_custom_field show_if_simple show_if_subscription">';
    
    woocommerce_wp_text_input( array(
        'id'            => 'my_text_field',
        'name'          => 'my_text_field',
        'label'         => __('Some label', 'woocommerce'),
    ) );
    
    echo '</div>';
}

// Saving custom fields values from admin product settings
add_action('woocommerce_admin_process_product_object', 'save_admin_product_custom_fields_values');
function save_admin_product_custom_fields_values( $product ) {
    if ( isset($_POST['my_text_field']) ) {
        $product->update_meta_data( 'my_text_field', sanitize_text_field($_POST['my_text_field']) );
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


To display it on simple and variable subscriptions change:

echo '<div class="product_custom_field show_if_simple show_if_subscription">';

with:

echo '<div class="product_custom_field">';

Addition for a checkbox field:

// Showing custom fields on admin product settings "general" tab
add_action('woocommerce_product_options_general_product_data', 'add_admin_product_custom_checkbox_fields', 10, 3);
function add_admin_product_custom_checkbox_fields() {
    global $post;

    echo '<div class="product_custom_field show_if_simple show_if_subscription">';

    echo '<p><strong>' . __("Mindesk e-Commerce", 'woocommerce') . '</strong></p>';

    $value = get_post_meta($post->ID, 'mindesk_analytics_opt_out', true);

    woocommerce_wp_checkbox(array(
        'id'            => "mindesk_analytics_opt_out",
        'name'          => "mindesk_analytics_opt_out",
        'wrapper_class' => 'show_if_simple',
        'label'         => __('&nbsp; Analytics', 'woocommerce'),
        'value'         => $value,
    ) );

    echo '</div>';
}

// Saving custom fields values from admin product settings
add_action('woocommerce_admin_process_product_object', 'save_admin_product_custom_checkbox_fields_values');
function save_admin_product_custom_checkbox_fields_values( $product ) {
    $value = isset($_POST['mindesk_analytics_opt_out']) ? 'yes' : 'no';
    $product->update_meta_data('mindesk_analytics_opt_out', esc_attr($value) );
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • How you are showing the value field in `woocommerce_wp_text_input` i cant see `value` attribute here .. @LoicTheAztec – Mittul At TechnoBrave Feb 03 '21 at 11:38
  • I am not getting you here `To display it on simple and variable subscriptions change: ` what you mean by that .. as you previously answered my question here https://stackoverflow.com/a/66023181/6829420 – Mittul At TechnoBrave Feb 03 '21 at 11:44
  • I want that same field to show in "Simple Subscription" .. is it possible to show with only one hook or do i need to use different hooks which you mentioned in this answer and that answer .. – Mittul At TechnoBrave Feb 03 '21 at 11:45
  • @MittulAtTechnoBrave The function `woocommerce_wp_text_input()` handle that so no need to add 'value' attribute like for variations… You just need 'value' attribute for variations or in some other cases… – LoicTheAztec Feb 03 '21 at 11:53
  • @MittulAtTechnoBrave This is for simple subscriptions. – LoicTheAztec Feb 03 '21 at 11:54
  • @MittulAtTechnoBrave I have added an addition to handle a checkbox field for simple subscriptions – LoicTheAztec Feb 03 '21 at 12:08
  • I have one question though .. u have used here `update_meta_data` cant we use `update_post_meta` just like we used for variable subscriptions here https://stackoverflow.com/questions/66022677/woocommerce-variable-subscription-custom-field-checkbox-set-default-as-unchecked @LoicTheAztec ? – Mittul At TechnoBrave Feb 04 '21 at 05:14
  • by the way .. your code is showing even in `simple product` option as well .. i dont want to show there – Mittul At TechnoBrave Feb 04 '21 at 06:30
  • @MittulAtTechnoBrave Since WooCommerce 3 there are some new hooks and methods to be used instead of old WordPress way with `update_post_meta`, as woocommerce will migrate in future all custom posts and postmeta to custom tables… You can remove `show_if_simple` and keep `show_if_subscription` to only display that field on simple subscriptions. – LoicTheAztec Feb 04 '21 at 08:48