Ami*_*our 2 php wordpress plugins custom-taxonomy woocommerce
在 WooCommerce 中,我尝试将自定义字段作为插件添加到所有产品属性中……
在下面的代码中,我可以向一个分类法添加自定义字段:
function pippin_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'pippin' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','pippin' ); ?></p>
</div>
<?php
}
add_action( 'pa_flavor_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function pippin_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'pippin' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter a value for this field','pippin' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'pa_flavor_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );
Run Code Online (Sandbox Code Playgroud)
如何获取所有 WooCommerce 产品属性分类法(和名称)?
通过这种方式,我可以为所有现有产品属性动态创建这些自定义字段。
您可以使用以下方法获取产品属性分类slug(和分类名称):
在 WooCommerce 3.6 版之前具有wc_get_attribute_taxonomies()专用功能:
// Get an array of product attribute taxonomies slugs
$attributes_tax_slugs = array_keys( wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ) );
// Get an array of product attribute taxonomies names (starting with "pa_")
$attributes_tax_names = array_filter( array_map( 'wc_attribute_taxonomy_name', $attribute_taxonomies ));
Run Code Online (Sandbox Code Playgroud)
自 WooCommerce 3.6+ 版起,具有wc_get_attribute_taxonomy_labels()专用功能:
// Get an array of product attribute taxonomies slugs
$attributes_tax_slugs = array_keys( wc_get_attribute_taxonomy_labels() );
// Get an array of product attribute taxonomies names (starting with "pa_")
$attributes_tax_names = array_filter( array_map( 'wc_attribute_taxonomy_name', $attribute_taxonomies ));
Run Code Online (Sandbox Code Playgroud)
官方文档:
wc-attribute-functions.php核心文件wp_list_pluck()功能array_keys(),array_filter()和array_map()| 归档时间: |
|
| 查看次数: |
2812 次 |
| 最近记录: |