首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >woocommerce产品定制字段

woocommerce产品定制字段
EN

WordPress Development用户
提问于 2018-07-15 05:37:39
回答 2查看 2.5K关注 0票数 1

我在woocommerce Products>General标签中添加了自定义字段。但是,我不会使用以下链接来响应这些自定义字段值:- http://yoursite.com/wp-json/wc/v2/products/

我的守则:-

代码语言:javascript
复制
//Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

//Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

    function woo_add_custom_general_fields() {

        global $woocommerce, $post;

        echo '' ;
                 //Purchasing Cost
                woocommerce_wp_text_input(
                    array(
                        'id' => 'purchasing_cost',
                        'placeholder' => 'Purchasing Cost',
                        'label' => __('Purchasing Cost', 'woocommerce'),
                        'type' => 'text',                       
                        'show_in_rest' => true,         
                        'rest_base' => 'products',
                        'rest_controller_class' => 'WP_REST_Products_Controller',
                    )
                );

                 //OverHead
                woocommerce_wp_text_input(
                    array(
                        'id' => 'overhead',
                        'placeholder' => 'Overhead',
                        'label' => __('Overhead', 'woocommerce'),
                        'type' => 'text',   
                        'show_in_rest' => true,
                        'rest_base' => 'products',
                        'rest_controller_class' => 'WP_REST_Products_Controller',
                    )
                );  
        echo '';
    }

    //To Save data
    function woo_add_custom_general_fields_save($post_id) {
            //Purchasing Cost
             $woocommerce_purchasing_cost_field = $_POST['purchasing_cost'];
                if (!empty($woocommerce_purchasing_cost_field))
                update_post_meta($post_id, 'purchasing_cost', esc_attr($woocommerce_purchasing_cost_field));

            //OverHead
             $woocommerce_overhead_field = $_POST['overhead'];
                if (!empty($woocommerce_overhead_field))
                update_post_meta($post_id, 'overhead', esc_attr($woocommerce_overhead_field));
}

提前谢谢。

EN

回答 2

WordPress Development用户

发布于 2018-07-15 09:28:07

您应该将自定义字段添加到product响应中,尝试以下操作(未经测试)

代码语言:javascript
复制
add_filter( 'woocommerce_rest_prepare_product', 'custom_products_api_data', 90, 2 );
function custom_products_api_data( $response, $post ) {

// retrieve a custom field and add it to API response
$response->data['purchasing_cost'] = get_post_meta( $post->ID, 'purchasing_cost', true );

return $response;
}
票数 2
EN

WordPress Development用户

发布于 2018-07-16 06:48:05

回答我的问题

代码语言:javascript
复制
add_action( 'rest_api_init', 'slug_register_purchasing' );

function slug_register_purchasing() {
        register_rest_field( 'product',
            'purchasing_cost',
            array(
                'get_callback'    => 'slug_get_purchasing_cost',
                'update_callback' => null,
                'schema'          => null,
            )
        );
    }

function slug_get_purchasing_cost( $object, $field_name, $request ) {
    return get_post_meta( $object[ 'id' ], $field_name, true );
}

add_action( 'rest_api_init', 'slug_register_overhead' );

function slug_register_overhead() {
    register_rest_field( 'product',
        'overhead',
        array(
            'get_callback'    => 'slug_get_overhead',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}



function slug_get_overhead( $object, $field_name, $request ) {
    return get_post_meta( $object[ 'id' ], $field_name, true );
}

有关详细说明,http://v2.wp-api.org/extending/modifying/

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/308591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档