Last active
June 26, 2019 09:28
-
-
Save JRyven/c05dee730979fdea85f751638d5a45b1 to your computer and use it in GitHub Desktop.
Adding a custom question to WooCommerce Checkout, Saving the answer to the Database, Retrieving the Answer on order Completion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WooCommerce Add Custom Checkout Fields | |
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); | |
function custom_override_checkout_fields( $fields ) { | |
// append to the billing questions | |
// we output a 'header' with CSS:before | |
$fields['billing']['custom_question_slug'] = array( | |
'type' => 'text', | |
'name' => 'custom_question', | |
'label' => __('Custom Question', 'woocommerce'), | |
'placeholder' => _x('Custom Question', 'placeholder', 'woocommerce'), | |
'required' => true, | |
'class' => array('form-row-wide custom_question_header'), | |
'clear' => true | |
); | |
return $fields; | |
} | |
// | |
// Save the order meta with hidden field value | |
// | |
add_action('woocommerce_checkout_update_order_meta', 'custom_question_order_meta'); | |
function custom_question_order_meta( $order_id ) { | |
if ($_POST['custom_question_slug']) update_post_meta( $order_id, 'custom_question_slug', esc_attr($_POST['custom_question_slug'])); | |
} | |
// | |
// WooCommerce create Member Post on checkout | |
// | |
add_action( 'woocommerce_order_status_completed', 'generate_rsc_entry' ); | |
function generate_rsc_entry($order_id) { | |
global $wpdb; | |
// Getting the order (object type) | |
$order = wc_get_order( $order_id ); | |
// Getting order items | |
$items = $order->get_items(); | |
$total_items_qty = 0; | |
// Iterating through each item (here we do it on first only) | |
$order_item_id=''; | |
foreach ( $items as $key => $item ) { | |
$total_items_qty += $item["qty"]; | |
$order_item_id=$key; | |
} | |
// Here's how to get stuff out of the $order | |
$customer_id = $order->customer_user; | |
$company = $order->billing_company; | |
// Here's how to get our custom information reliably | |
$custom_question = $wpdb->get_var( | |
$wpdb->prepare(' | |
SELECT `meta_value` | |
FROM `wp_postmeta` | |
WHERE `post_id` = %d | |
AND `meta_key` = %s | |
', | |
$order_id, | |
'custom_question_slug' | |
) | |
); | |
// Now we do something awesome! Like - make a custom post | |
$post_arr = array( | |
'post_title' => $company, | |
'post_content' => '', | |
'post_type' => 'member_rsc', | |
'post_status' => 'publish', | |
'post_author' => get_current_user_id(), | |
'meta_input' => array( | |
'rsc-phone' => $rsc_name, | |
'rsc-name' => $rsc_phone, | |
'member-id' => $customer_id, | |
), | |
); | |
wp_insert_post( $post_arr ); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment