wordpress - 在 woocommerce REST API 中添加自定义分类术语

标签 wordpress woocommerce hook-woocommerce woocommerce-rest-api

我已经创建了自定义分类“品牌”并附加到 woocommerce 产品。不幸的是,它在 woocommerce REST API 响应中不可用。 如何将自定义分类术语附加到 woocommerce rest API。目前没有关于自定义分类法附件的文档。有没有钩子(Hook)或过滤器?

最佳答案

我使用此代码解决了问题。您将能够使用此方法获取和更新自定义分类法。基本上。您可以在 functions.php 文件或 plugin 中添加此代码。

步骤:1brands 替换为您的 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 注册 Taxonomy 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');
            
            
        }

关于wordpress - 在 woocommerce REST API 中添加自定义分类术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62664811/

相关文章:

mysql - wordpress 数据库返回错误

php - WooCommerce 2.6 自定义端点 404 未找到错误

php - 专门在 Woocommerce 3 中创建订单的 Hook

wordpress - 当 WooCommerce 购物车页面上的数量更新时拆分购物车项目

mysql - 获取 WooCommerce 中没有特定分类的产品列表

php - WordPress 画廊问题

php - WooCommerce - 添加到购物车没有变体 ID

javascript - WP Coda Slider - 不显示内容

Woocommerce REST API v2 : Issue with adding custom data to orders