首页 > 解决方案 > 如何使用 add_action 向所有 Woocommerce 属性术语添加自定义字段

问题描述

在 Woocommerce 产品属性页面上,我对一个 Woocommerce 属性(称为myattribute)进行了以下操作。以下将自定义字段添加到myattribute条款的添加/编辑页面:

add_action( 'myattribute_add_form_fields', array( $this, 'add_new_field' ), 10, 2 );

add_action( 'created_myattribute', array( $this, 'save_new_field' ), 10, 2 );

add_action( 'myattribute_edit_form_fields', array( $this, 'update_new_field' ), 10, 2 );

add_action( 'edited_myattribute', array( $this, 'updated_new_field' ), 10, 2 );

有没有办法对所有属性做同样的事情,而不需要在 add_action 中命名它们?

原因是我有许多属性,并希望将自定义字段添加到与这些属性相关的所有术语中。

提前致谢。

标签: phpwordpresswoocommerceattributestaxonomy-terms

解决方案


这可能不是最好的解决方案,但它对我有用:

if (isset($_GET['taxonomy']) && !empty($_GET['taxonomy'])) {
    $taxonomy_name = $_GET['taxonomy'];

    if (strpos($taxonomy_name, 'pa_') !== false) {
        add_action( $taxonomy_name.'_add_form_fields', array ( $this, 'add_category_image' ), 10, 2 );
        add_action( 'created_'.$taxonomy_name, array ( $this, 'save_category_image' ), 10, 2 );
        add_action( $taxonomy_name.'_edit_form_fields', array ( $this, 'update_category_image' ), 10, 2 );
        add_action( 'edited_'.$taxonomy_name, array ( $this, 'updated_category_image' ), 10, 2 );
    }
}

我一直在寻找相同的解决方案,到目前为止,这个快速代码帮助我在任何产品属性编辑/添加视图上实现了自定义字段。


推荐阅读