0

I have added custom field in woocommerce Products>General Tab. But I am not getting those custom fields value in response using following link:- http://yoursite.com/wp-json/wc/v2/products/

My Code:-

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

Thanks in Advance.

1

You should ad the custom field to product API response, try this (untested)

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;
}
  • I tried but, It's not working. – MAYANK SONKAR Jul 16 '18 at 5:29
0

Answer to My Question

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

For Detailed Description http://v2.wp-api.org/extending/modifying/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.