【问题标题】:WooCommerce add admin custom field(s) to simple subscriptionsWooCommerce 将管理自定义字段添加到简单订阅
【发布时间】:2021-05-07 08:02:38
【问题描述】:

在 WooCommerce 中,我启用了 WooCommerce Subscriptions 插件,它运行良好,符合我的预期。

但是,我有一个来自客户的自定义要求。我想在 Variable SubscriptionsSimple Subscriptions 中创建产品时添加自定义字段。

我已使用以下代码在 Variable Subscriptions 中添加了自定义字段,它的工作方式符合我的预期。这是我的代码。

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

这就是它现在工作正常的样子。

正如您在上面的屏幕截图中看到的,最后一个字段标记为“某些标签”.. 我的自定义字段已添加。

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

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

我已经研究过,但还没有找到任何钩子。

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

【问题讨论】:

  • 请有机会获得对以下答案的反馈。

标签: php wordpress woocommerce hook-woocommerce woocommerce-subscriptions


【解决方案1】:

更新:

要将自定义字段添加到管理产品数据设置 > 常规选项卡 简单订阅

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

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


要在简单和可变的订阅更改上显示它:

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

与:

echo '<div class="product_custom_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) );
}

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

【讨论】:

  • 你如何在woocommerce_wp_text_input 中显示值字段我在这里看不到value 属性.. @LoicTheAztec
  • 我没有把你带到这里To display it on simple and variable subscriptions change: 你的意思是什么......正如你之前在这里回答我的问题stackoverflow.com/a/66023181/6829420
  • 我希望在“简单订阅”中显示相同的字段..是否可以只显示一个钩子,或者我需要使用您在这个答案和那个答案中提到的不同钩子..
  • @MittulAtTechnoBrave 函数woocommerce_wp_text_input() 处理,所以不需要像变化一样添加'value'属性......你只需要变化的'value'属性或在其他一些情况下......
  • @MittulAtTechnoBrave 这是用于简单订阅的。
猜你喜欢
  • 2021-07-01
  • 2016-10-12
  • 2016-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-17
  • 2021-02-22
相关资源
最近更新 更多