Skip to content

Instantly share code, notes, and snippets.

@webdados
Last active April 2, 2024 10:55
Show Gist options
  • Save webdados/de05d48a99063ac25f6462b1dedba2ee to your computer and use it in GitHub Desktop.
Save webdados/de05d48a99063ac25f6462b1dedba2ee to your computer and use it in GitHub Desktop.
How to add custom fields to the Shop as Client PRO add-on ajax call - Classic Checkout
<?php
/* Add this to you (child-)theme functions.php file, on a (mu-)plugin or with a plugin like Code Snippets https://wordpress.org/plugins/code-snippets/ */
/* This only works with the classic checkout */
add_filter( 'shop_as_client_pro_customer_data', 'my_shop_as_client_pro_customer_data', 10, 3 );
function my_shop_as_client_pro_customer_data( $data, $customer = null, $order = null ) {
//Are we getting a "customer" object?
if ( $customer ) {
//Custom checkout field with id "billing_xpto"
$data['billing']['xpto'] = $customer->get_meta( 'billing_xpto' );
//Custom checkout field with id "shipping_xpto"
$data['shipping']['xpto'] = $customer->get_meta( 'shipping_xpto' );
//Custom field "whatever" loaded into the custom checkout with id "billing_whatever"
$data['billing']['whatever'] = $customer->get_meta( 'whatever' );
//Custom checkout field with id "field_id" that is neither from billing nor shipping
$data['custom']['field_id'] = $customer->get_meta( 'custom_field_meta' );
//Return the data
//Or are we getting a "order" object?
} elseif ( $order ) {
//Custom checkout field with id "billing_xpto" fetched from order meta field "_billing_xpto"
$data['billing']['xpto'] = $order->get_meta( '_billing_xpto' );
//Custom checkout field with id "field_id", from order, that is neither from billing nor shipping, fetched from order meta field "custom_field_meta"
$data['custom']['field_id'] = $order->get_meta( 'custom_field_meta' );
}
//Return the array
return $data;
}
@bantunesm
Copy link

Working as it should ! 😎 Obg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment