تابع ووکامرسی wc_get_product_terms دریافت فیلد های تاکسونومی محصول

تابع ووکامرسی wc_get_product_terms – دریافت فیلد های تاکسونومی محصول
Syntax – سینتکس
(array) wc_get_product_terms( (int) $product_id, (string) $taxonomy, (array) $args = array() );
Parameters – پارامتر ها (3)
- 1- $product_id (int)
- 2- $taxonomy (string)
- 3- $args (array)
Usage – نحوه استفاده
if ( !function_exists( 'wc_get_product_terms' ) ) {
require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-term-functions.php';
}
// The product id.
$product_id = -1;
// Taxonomy slug.
$taxonomy = '';
// Query arguments.
$args = array();
// NOTICE! Understand what this does before running.
$result = wc_get_product_terms($product_id, $taxonomy, $args);
Defined – محل تعریف
/includes/wc-term-functions.php
function wc_get_product_terms( $product_id, $taxonomy, $args = array() ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return array();
}
if ( empty( $args['orderby'] ) && taxonomy_is_product_attribute( $taxonomy ) ) {
$args['orderby'] = wc_attribute_orderby( $taxonomy );
}
// Support ordering by parent.
if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'name_num', 'parent' ) ) ) {
$fields = isset( $args['fields'] ) ? $args['fields'] : 'all';
$orderby = $args['orderby'];
// Unset for wp_get_post_terms.
unset( $args['orderby'] );
unset( $args['fields'] );
$terms = _wc_get_cached_product_terms( $product_id, $taxonomy, $args );
switch ( $orderby ) {
case 'name_num' :
usort( $terms, '_wc_get_product_terms_name_num_usort_callback' );
break;
case 'parent' :
usort( $terms, '_wc_get_product_terms_parent_usort_callback' );
break;
}
switch ( $fields ) {
case 'names' :
$terms = wp_list_pluck( $terms, 'name' );
break;
case 'ids' :
$terms = wp_list_pluck( $terms, 'term_id' );
break;
case 'slugs' :
$terms = wp_list_pluck( $terms, 'slug' );
break;
}
} elseif ( ! empty( $args['orderby'] ) && 'menu_order' === $args['orderby'] ) {
// wp_get_post_terms doesn't let us use custom sort order.
$args['include'] = wc_get_object_terms( $product_id, $taxonomy, 'term_id' );
if ( empty( $args['include'] ) ) {
$terms = array();
} else {
// This isn't needed for get_terms.
unset( $args['orderby'] );
// Set args for get_terms.
$args['menu_order'] = isset( $args['order'] ) ? $args['order'] : 'ASC';
$args['hide_empty'] = isset( $args['hide_empty'] ) ? $args['hide_empty'] : 0;
$args['fields'] = isset( $args['fields'] ) ? $args['fields'] : 'names';
// Ensure slugs is valid for get_terms - slugs isn't supported.
$args['fields'] = ( 'slugs' === $args['fields'] ) ? 'id=>slug' : $args['fields'];
$terms = get_terms( $taxonomy, $args );
}
} else {
$terms = _wc_get_cached_product_terms( $product_id, $taxonomy, $args );
}
return apply_filters( 'woocommerce_get_product_terms' , $terms, $product_id, $taxonomy, $args );
}
versions – نسخه ها
از نسخه : 3.0.2
نسخه فعلی : 3.0.6
دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2
ارسال نظر