【问题标题】:How to get all WooCommerce product attributes taxonomies slugs and names如何获取所有 WooCommerce 产品属性分类法和名称
【发布时间】:2020-08-21 01:31:41
【问题描述】:

在 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 );

如何获取所有 WooCommerce 产品属性分类法 slug(和名称)?

这样我可以为所有现有的产品属性动态地创建这些自定义字段。

【问题讨论】:

    标签: php wordpress woocommerce plugins custom-taxonomy


    【解决方案1】:

    您可以使用以下方法获取产品属性分类slugs(和分类名称):

    1. 在带有wc_get_attribute_taxonomies() 专用功能的 WooCommerce 3.6 版之前:

       // 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 ));
      
    2. 自 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 ));
      

    官方文档:

    【讨论】:

      猜你喜欢
      • 2018-10-09
      • 2020-09-09
      • 1970-01-01
      • 2020-11-02
      • 2019-04-04
      • 2021-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多