admin管理员组文章数量:1122832
Is there a way to add a custom field to the add new attribute page or edit attribute in Woocommerce ?
Preferably with CMB2, but whatever works.
As I understand, this specific section of woocommerce is a taxonomy inception - it's a taxonomy that creates taxonomies :)
I looked under woocommerce/includes/class-wc-post-types.php but not sure what filter would do the trick (if any).
I've managed to add fields to the individual terms but I need a field for the attribute itself.
Is there a way to add a custom field to the add new attribute page or edit attribute in Woocommerce ?
Preferably with CMB2, but whatever works.
As I understand, this specific section of woocommerce is a taxonomy inception - it's a taxonomy that creates taxonomies :)
I looked under woocommerce/includes/class-wc-post-types.php but not sure what filter would do the trick (if any).
I've managed to add fields to the individual terms but I need a field for the attribute itself.
Share Improve this question asked Jul 14, 2020 at 23:29 MihaiMihai 1291 silver badge10 bronze badges 3- using 'woocommerce_after_add_to_cart_form' and 'woocommerce_after_edit_attribute_fields' actions, i've managed to output a field on the add / edit pages, don't know how to save / load the value from the database – Mihai Commented Jul 15, 2020 at 0:33
- Did my answer help or you needed more details? :) – Sally CJ Commented Jul 16, 2020 at 6:29
- 1 Yes, thanks! I ended up creating a function to generate multiple fields in case i needed, but basically doing what you suggested. – Mihai Commented Jul 19, 2020 at 22:55
1 Answer
Reset to default 13Yes, you can add custom fields to WooCommerce (product) attributes, which are WordPress custom taxonomies — e.g. an attribute named Color creates a taxonomy with the slug pa_color
.
But because the data are stored in the woocommerce_attribute_taxonomies
table which does not offer a field for custom data, you'll need to save your data somewhere else, e.g. the WordPress options table (wp_options
) like what I did in the examples below:
Add "My Field" to the attribute form:
function my_edit_wc_attribute_my_field() {
$id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0;
$value = $id ? get_option( "wc_attribute_my_field-$id" ) : '';
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="my-field">My Field</label>
</th>
<td>
<input name="my_field" id="my-field" value="<?php echo esc_attr( $value ); ?>" />
</td>
</tr>
<?php
}
add_action( 'woocommerce_after_add_attribute_fields', 'my_edit_wc_attribute_my_field' );
add_action( 'woocommerce_after_edit_attribute_fields', 'my_edit_wc_attribute_my_field' );
Set/save the "My Field" value, e.g. in the options table:
function my_save_wc_attribute_my_field( $id ) {
if ( is_admin() && isset( $_POST['my_field'] ) ) {
$option = "wc_attribute_my_field-$id";
update_option( $option, sanitize_text_field( $_POST['my_field'] ) );
}
}
add_action( 'woocommerce_attribute_added', 'my_save_wc_attribute_my_field' );
add_action( 'woocommerce_attribute_updated', 'my_save_wc_attribute_my_field' );
Delete the custom option when the attribute is deleted.
add_action( 'woocommerce_attribute_deleted', function ( $id ) {
delete_option( "wc_attribute_my_field-$id" );
} );
Then for example, on a taxonomy/term archive page, you can get the My Field value like so:
$term = get_queried_object();
$attr_id = wc_attribute_taxonomy_id_by_name( $term->taxonomy );
$my_field = get_option( "wc_attribute_my_field-$attr_id" );
本文标签: Add custom field to Woocommerce add new attributeedit page
版权声明:本文标题:Add custom field to Woocommerce add new attributeedit page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305847a1932735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论