WooCommerce将管理员自定义字段添加到简单订阅中 [英] WooCommerce add admin custom field(s) to simple subscriptions

查看:49
本文介绍了WooCommerce将管理员自定义字段添加到简单订阅中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我启用了

如您在上面的屏幕截图中所见,标记为某些标签"的最后一个字段是..添加了我的自定义字段.

但是,我想添加与我设置简单订阅相同的字段.我的意思是.

如您所见,我希望这些自定义字段也显示并保存在这里..

我已经进行了研究,但仍然找不到任何钩子.

有人可以指导我如何实现这一目标.

解决方案

已更新:

要将自定义字段添加到管理产品数据设置>关于简单订阅的常规 tab :

 //在管理产品设置常规"上显示自定义字段标签add_action('woocommerce_product_options_general_product_data','add_admin_product_custom_fields',10,3);函数add_admin_product_custom_fields(){全球$ post;回声'< div class ='product_custom_field show_if_simple show_if_subscription'>';woocommerce_wp_text_input(array('id'=>'my_text_field','名称'=>'my_text_field','标签'=>__(某些标签","woocommerce"),));回声'</div>';}//保存管理产品设置中的自定义字段值add_action('woocommerce_admin_process_product_object','save_admin_product_custom_fields_values');函数save_admin_product_custom_fields_values($ product){如果(isset($ _ POST ['my_text_field'])){$ product-> update_meta_data('my_text_field',sanitize_text_field($ _ POST ['my_text_field']));;}} 

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


要在简单和可变订阅更改时显示它,

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

具有:

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


添加复选框字段:

 //在管理产品设置常规"上显示自定义字段标签add_action('woocommerce_product_options_general_product_data','add_admin_product_custom_checkbox_fields',10,3);函数add_admin_product_custom_checkbox_fields(){全球$ post;回声'< div class ='product_custom_field show_if_simple show_if_subscription'>';回声'< 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",'名称'=>"mindesk_analytics_opt_out",'wrapper_class'=>'show_if_simple','标签'=>__('& nbsp; Analytics','woocommerce'),'值'=>$ value,));回声'</div>';}//保存管理产品设置中的自定义字段值add_action('woocommerce_admin_process_product_object','save_admin_product_custom_checkbox_fields_values');函数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));} 

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

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.

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.

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.

解决方案

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.

这篇关于WooCommerce将管理员自定义字段添加到简单订阅中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆