wordpress - 在 woocommerce REST API 中添加自定义分类术语
问题描述
我创建了自定义分类“品牌”并附加到 woocommerce 产品。不幸的是,它在 woocommerce REST API 响应中不可用。如何将自定义分类术语附加到 woocommerce rest API。目前没有关于自定义分类法附件的文档。有没有钩子或过滤器?
解决方案
我通过使用此代码解决了。您将能够使用此方法获取和更新自定义分类。基本上。您可以将此代码添加到functions.php
文件或plugin
.
步骤:1
替换brands
为您的taxonomy_name
.
步骤:2
如果您的分类有自定义字段,请替换custom_field_name
为您的。
分类代码:
add_action( 'init', 'create_brands_hierarchical_taxonomy', 0 );
//create a custom taxonomy name it topics for your posts
function create_brands_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' => _x( 'Brands', 'taxonomy general name' ),
'singular_name' => _x( 'Brand', 'taxonomy singular name' ),
'search_items' => __( 'Search Brands' ),
'all_items' => __( 'All Brands' ),
'parent_item' => __( 'Parent Brand' ),
'parent_item_colon' => __( 'Parent Brand:' ),
'edit_item' => __( 'Edit Brand' ),
'update_item' => __( 'Update Brand' ),
'add_new_item' => __( 'Add New Brand' ),
'new_item_name' => __( 'New Brand Name' ),
'menu_name' => __( 'Brands' ),
);
$capabilities = array(
'manage_terms' => 'manage_woocommerce',
'edit_terms' => 'manage_woocommerce',
'delete_terms' => 'manage_woocommerce',
'assign_terms' => 'manage_woocommerce',
);
// Now register the taxonomy
$args = array(
'labels' => $labels,
'show_in_rest' => true,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => false,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'capabilities' => $capabilities,
);
register_taxonomy( 'brands', array( 'product' ), $args );
register_taxonomy_for_object_type( 'brands', 'product' );
}
为 WC 注册分类 API
//Register taxonomy API for WC
add_action( 'rest_api_init', 'register_rest_field_for_custom_taxonomy_brands' );
function register_rest_field_for_custom_taxonomy_brands() {
register_rest_field('product', "brands", array(
'get_callback' => 'product_get_callback',
'update_callback' => 'product_update_callback',
'schema' => null,
));
}
//Get Taxonomy record in wc REST API
function product_get_callback($post, $attr, $request, $object_type)
{
$terms = array();
// Get terms
foreach (wp_get_post_terms( $post[ 'id' ],'brands') as $term) {
$terms[] = array(
'id' => $term->term_id,
'name' => $term->name,
'slug' => $term->slug,
'custom_field_name' => get_term_meta($term->term_id, 'custom_field_name', true)
);
}
return $terms;
}
//Update Taxonomy record in wc REST API
function product_update_callback($values, $post, $attr, $request, $object_type)
{
// Post ID
$postId = $post->get_id();
//Example: $values = [2,4,3];
// Set terms
wp_set_object_terms( $postId, $values , 'brands');
}
推荐阅读
- javascript - 如何在 php 中使用 while 循环在 div 中换行?
- amazon-web-services - 模板资源块中未解决的资源依赖项 [DefaultSchedule]
- matlab - 句柄类对象的索引数组
- laravel - 覆盖 laravel 中的查询
- excel - 日期是在两个日期之间还是在两个日期之间?
- vba - 将带有路径的excel文件中的列导入数组
- regex - Ant 模式匹配器以十进制开头并具有十进制值
- javascript - 替换字符串中的单引号
- mysql - 从 SQL 表中选择行并将这些特定行合并到另一个表
- ionic-framework - 应用程序打开时的离子推送通知