• Resolved niikk

    (@niikk)


    Hello

    We are trying to get the customer birthday into the WooCommerce built-in reports.
    So for that we created a custom field “Check Age” which is displayes at the checkout page. It’s only possible to order, if a customer is at least 21 years old. After a order is made by the customer, we store the Age info also in the order details.

    This is all working with the following code. Now we want to have that info in the WooCommerce built-in reports (WooCommerce/Customer) to see how old our customers are.

    So actually we only struggle with the last step. Maybe someone has a idea how to get this done. ??

    // Adding a custom checkout date field
    add_filter( 'woocommerce_billing_fields', 'add_birth_date_billing_field', 20, 1 );
    function add_birth_date_billing_field($billing_fields) {
    
        $billing_fields['billing_birth_date'] = array(
            'type'        => 'date',
            'label'       => __('Check Age'),
            'class'       => array('form-row-wide'),
            'priority'    => 25,
            'required'    => true,
            'clear'       => true,
        );
        return $billing_fields;
    }
    
    // Check customer age on checkout
    add_action('woocommerce_checkout_process', 'check_birth_date');
    function check_birth_date() {
        // Check billing city 2 field
        if( isset($_POST['billing_birth_date']) && ! empty($_POST['billing_birth_date']) ){
            // Get customer age from birthdate
            $age = date_diff(date_create($_POST['billing_birth_date']), date_create('now'))->y;
    
            // Checking age and display an error notice avoiding checkout
            if( $age < 18 ){
                wc_add_notice( __( "Sorry, you must be at least 21!" ), "error" );
    
            }
        }
    }
    
    /**
     * Update the order meta with field value
     */
    add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['billing_birth_date'] ) ) {
            update_post_meta( $order_id, 'billing_birth_date', sanitize_text_field( $_POST['billing_birth_date'] ) );
        }
    }
    
    /**
    * Display field value on the order edit page
    */
    
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
    
    echo '<p><strong>'.__('Check Age').':</strong> <br/>' . get_post_meta( $order->get_id(), 'billing_birth_date', true ) . '</p>';
    
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Save customer birthday in WooCommerce reports’ is closed to new replies.