Как получить все атрибуты продукта WooCommerce, ярлыки и названия таксономий - PullRequest
0 голосов
/ 06 мая 2020

В 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?

Таким образом я мог бы динамически создавать эти настраиваемые поля для всех существующих атрибутов продукта.

1 Ответ

0 голосов
/ 06 мая 2020

Вы можете получить таксономию атрибутов продукта ярлыки (и таксономию имена ), используя:

  1. До версии WooCommerce 3.6 с wc_get_attribute_taxonomies() выделенная функция:

    // Get an array of product attribute taxonomy slugs
    $attributes_tax_slugs = array_keys( wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ) );
    
    // Get an array of product attribute taxonomy 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 taxonomy slugs
    $attributes_tax_slugs = $attributes_tax_names = array_keys( wc_get_attribute_taxonomy_labels() );
    
    // Get an array of product attribute taxonomy names (starting with "pa_")
    $attributes_tax_names = array_filter( array_map( 'wc_attribute_taxonomy_name', $attribute_taxonomies ));
    

Официальная документация:

...