To add extra billing and shipping fields to the YITH Subscription details, you will need to use a combination of actions and filters in your WordPress code. Here are the steps to do it:
1-First, you will need to add the fields to the checkout form. You can use the following code snippet in your functions.php file or in a custom plugin to add extra fields to the checkout form:
// Add extra fields to the checkout form
add_filter( 'woocommerce_checkout_fields' , 'add_extra_billing_shipping_fields' );
function add_extra_billing_shipping_fields( $fields ) {
// Add billing fields
$fields['billing']['billing_extra_field'] = array(
'type' => 'text',
'label' => __('Extra Field', 'woocommerce'),
'placeholder' => __('Enter extra information', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
// Add shipping fields
$fields['shipping']['shipping_extra_field'] = array(
'type' => 'text',
'label' => __('Extra Field', 'woocommerce'),
'placeholder' => __('Enter extra information', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
Once you have added the fields to the checkout form, you will need to save the field values to the subscription. You can use the following code snippet in your functions.php file or in a custom plugin to save the field values:
// Save extra fields to subscription
add_action( 'woocommerce_checkout_create_subscription', 'save_extra_billing_shipping_fields_to_subscription', 10, 4 );
function save_extra_billing_shipping_fields_to_subscription( $subscription, $order, $product_id, $args ) {
// Save billing fields
if ( isset( $args['billing_extra_field'] ) ) {
$subscription->update_meta_data( 'billing_extra_field', sanitize_text_field( $args['billing_extra_field'] ) );
}
// Save shipping fields
if ( isset( $args['shipping_extra_field'] ) ) {
$subscription->update_meta_data( 'shipping_extra_field', sanitize_text_field( $args['shipping_extra_field'] ) );
}
$subscription->save();
}
Finally, you will need to display the extra fields in the subscription details. You can use the following code snippet in your functions.php file or in a custom plugin to display the fields:
// Display extra fields in subscription details
add_action( 'woocommerce_subscription_details_after_order_table', 'display_extra_billing_shipping_fields_in_subscription_details' );
function display_extra_billing_shipping_fields_in_subscription_details( $subscription ) {
// Display billing fields
$billing_extra_field = $subscription->get_meta( 'billing_extra_field' );
if ( $billing_extra_field ) {
echo '<p><strong>' . __('Extra Field:', 'woocommerce') . '</strong> ' . esc_html( $billing_extra_field ) . '</p>';
}
// Display shipping fields
$shipping_extra_field = $subscription->get_meta( 'shipping_extra_field' );
if ( $shipping_extra_field ) {
echo '<p><strong>' . __('Extra Field:', 'woocommerce') . '</strong> ' . esc_html( $shipping_extra_field ) . '</p>';
}
}
With these steps, you should be able to add extra billing and shipping fields to the YITH Subscription details. Note that you may need to adjust the code to fit your specific needs