1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* * Add a referanse field to the checkout. */ function prefix_checkout_add_referanse_field( $fields ) { $fields['billing']['billing_referanse'] = array( 'label' => __( 'Referanse', 'woocommerce' ), 'placeholder' => '', 'required' => false, 'class' => array('form-row-wide'), 'clear' => true ); return $fields; } add_filter( 'woocommerce_checkout_fields' , 'prefix_checkout_add_referanse_field' ); /** * Display field referanse value on the order edit page. */ function prefix_order_display_referanse_field( $order ) { $referanse_meta_field = get_post_meta( $order->get_id(), '_billing_referanse', true ); if ( $referanse_meta_field ) echo '<p><strong>'. __('Referanse') .':</strong> ' . esc_attr( get_post_meta( $order->get_id(), '_billing_referanse', true ) ) . '</p>'; } add_action( 'woocommerce_admin_order_data_after_shipping_address', 'prefix_order_display_referanse_field', 10, 1 ); |
Monthly Archives: October 2017
How to modify the WooCommerce API orders response?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* * Add a referanse field to the Order API response. */ function prefix_wc_rest_prepare_order_object( $response, $object, $request ) { // Get the value $referanse_meta_field = ( $value = get_post_meta($object->get_id(), '_billing_referanse', true) ) ? $value : ''; $response->data['referanse'] = $referanse_meta_field; return $response; } add_filter( 'woocommerce_rest_prepare_shop_order_object', 'prefix_wc_rest_prepare_order_object', 10, 3 ); /* * Legacy API * Add a referanse field to the Order API response. */ function prefix_wc_api_order_response( $order_data ) { // Get the value $referanse_meta_field = ( $value = get_post_meta($order_data['id'], '_billing_referanse', true) ) ? $value : ''; $order_data['referanse'] = $referanse_meta_field; return $order_data; } add_filter( 'woocommerce_api_order_response', 'prefix_wc_api_order_response', 10, 1 ); |
How to modify the WordPress REST API posts response
The WordPress REST API is a great thing, but in most cases you need to extend it. For example, if you use Advanced Custom Fields and you need to get them via the API, then you need to modify the response. Here’s a quick example…