WooCommerce产品自定义域

时间:2018-07-15 作者:MAYANK SONKAR

我在woocommerce产品>常规选项卡中添加了自定义字段。但我没有使用以下链接获取这些自定义字段的值:-http://yoursite.com/wp-json/wc/v2/products/

我的代码:-

//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 \'<div class="options_group">\' ;
                 //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 \'</div>\';
    }

    //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));
}
提前谢谢。

2 个回复
SO网友:Andrea Somovigo

您应该将自定义字段添加到产品API响应中,请尝试此操作(未测试)

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;
}

SO网友:MAYANK SONKAR

回答我的问题

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/

结束

相关推荐

Php致命错误:无法将WP_REST_RESPONSE类型的对象用作wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php中

我向WordPress添加了一个自定义端点,如下所示: add_action( \'rest_api_init\', function () { register_rest_route( \'menc/v1\', \'/crosscat/(?P[\\w-]+)/(?P[\\w-]+)\', array( \'methods\' => \'GET\', \'callback\' => \'dept_cat_api\',&#x