skip to Main Content

Hi I have custom fields for woocommerce checkout page.
I want to save it with html output database.
However, these are saved by cleaning html each time.
How can I save the html codes written to these fields to the database?

add_filter(`woocommerce_checkout_fields`, `burayi_ekle`);

function burayi_ekle($fields)
{
    $fields['billing']['billing_satis'] = [
        'type'     => 'textarea',
        'label'    => __('SATIŞ1', 'woocommerce'),
        'class'    => ['input-text'],
        'required' => true,
        'clear'    => false,
        'priority' => 120,
    ];

    $fields['billing']['billing_satis2'] = [
        'type'     => 'textarea',
        'label'    => __('SATIŞ2', 'woocommerce'),
        'class'    => ['input-text'],
        'required' => true,
        'clear'    => false,
        'priority' => 121,
    ];

    return $fields;

}

2

Answers


  1. Chosen as BEST ANSWER

    Okey . i fix it .

    This is my true code

    function save_extra_checkout_fields( $order_id ){
    
            $billing_satis = $_POST['billing_satis'];
            if(   ! empty($billing_satis) ){
                update_post_meta( $order_id, '_billing_satis', $billing_satis  );
            }
    
            $billing_satis2 = $_POST['billing_satis2'];
            if(   ! empty($billing_satis2) ){
                update_post_meta( $order_id, '_ billing_satis2', $billing_satis2 );
            }
        }
        add_action( 'woocommerce_checkout_update_order_meta', 'save_extra_checkout_fields', 10, 2 );
    

  2. You can save WC checkout Custom fields data along with HTML into database. The record would be saved in postmeta table for the post type of Order.

    Here you can try following code

    function save_extra_checkout_fields( $order_id, $posted ){
        if( isset( $posted['billing_satis'] ) ) {
            update_post_meta( $order_id, '_billing_satis', sanitize_text_field( $posted['billing_satis'] ) );
        }
        if( isset( $posted['billing_satis2'] ) ) {
            update_post_meta( $order_id, '_billing_satis2', $posted['billing_satis2'] );
        }
    }
    add_action( 'woocommerce_checkout_update_order_meta', 'save_extra_checkout_fields', 10, 2 );
    

    Don’t escape html when you add into database using update_post_meta.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search