【问题标题】:Create custom field into tag of attributes in woocommerce with add_action使用 add_action 在 woocommerce 中的属性标签中创建自定义字段
【发布时间】:2021-09-08 21:31:09
【问题描述】:

我想在 woocommerce 中的属性条款中创建一个自定义字段,使用 add_action(),我在网上搜索过,但在这个地方找不到任何钩子。

在这里我想添加我的自定义字段:

Woocommerce / 属性 / 配置术语 / 编辑术语

您知道我可以在这里使用哪些钩子来添加我的自定义字段吗? 还有一个钩子可以在表单发布后检查自定义字段?

这是我想要自定义字段的屏幕截图:

这里是我想要你使用的代码(我只需要正确的钩子(钩子1和钩子2):

function custom_field_categorie($term)
{
    $term_id = $term->term_id;
    $args = array
    (
        'id' => 'GRE_ID',
        'label' => __('ID genre'),
        'class' => '',
        'desc_tip' => true,
        'value' => get_term_meta($term_id, 'GRE_ID', true),
        'custom_attributes' => array('readonly' => 'readonly'),
    );
    woocommerce_wp_text_input($args);
}
add_action('hook1', 'custom_field_categorie', 10, 1);

function custom_field_categorie_save($term_id)
{
    if(!empty($_POST['GRE_ID']))
    {
        update_term_meta($term_id, 'GRE_ID', sanitize_text_field($_POST['GRE_ID']));
    }
}
add_action('hook2', 'custom_field_categorie_save', 10, 1);

感谢您的帮助

【问题讨论】:

  • 您好,请添加一些我们可以粘贴的内容,例如图片或描述您的问题的内容,而不仅仅是文字。另请向我们展示(代码)您已经尝试过的内容。
  • @Mr.Jo 感谢您的回答,我已经编辑了更多详细信息

标签: wordpress woocommerce hook


【解决方案1】:

给你。将此放入您的子主题的function.php 文件中。测试和工作:

add_action( 'product_tag_edit_form_fields', 'product_tag_edit_form_fields_action' );
function product_tag_edit_form_fields_action( WP_Term $term ) {
    $term_id = $term->term_id;

    if ( empty( $term_id ) ) {
        return;
    }

    $genre_id = get_term_meta( $term_id, 'GRE_ID', true );
    ?>
    <tr class="form-field form-required term-genre-wrap">
        <th scope="row"><label for="genre"><?= __( 'ID genre', 'your-lang-id' ) ?></label></th>
        <td><input name="genre" id="genre" type="text" value="<?= $genre_id ?>" size="40" readonly/></td>
    </tr>
    <?php
}

由于您最初将 readonly 添加到自定义参数中只是为了显示值,因此您不需要保存该字段中的值,因为在此表单中它永远不能设置为空。

查看最终结果:

【讨论】:

  • 感谢您的回答,但我已经尝试并且自定义字段没有出现在编辑区域中,您确定挂钩吗?谢谢
  • 我们谈论的是产品标签——对吧?我在答案中添加了一张图片。
  • 这可能是第 3 方插件的子类别吗?
  • 我已经禁用了我的所有插件,但没有任何改变,而且我在其他地方还有其他带有钩子的自定义字段,它可以工作,但在这里它似乎不起作用:/ 但我看到你在你的截图在正确的地方,所以我不知道为什么我没有看到它
  • 抱歉,我现在才看到它在“product_tag”中工作,但我想要属性“product_attributes”中的自定义字段,你知道这个地方的钩子吗?
猜你喜欢
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 2021-05-06
  • 1970-01-01
  • 2018-08-03
  • 2016-09-14
  • 2021-06-12
相关资源
最近更新 更多