Advertisement
majeedraza

WooCommerce - Add Shipping Phone

Apr 11th, 2021
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <?php
  2. // WooCommerce - Add Shipping Phone
  3. add_filter( 'woocommerce_checkout_fields', 'add_shipping_phone_to_checkout' );
  4. function add_shipping_phone_to_checkout( $fields ) {
  5.   $fields['shipping']['shipping_phone'] = array(
  6.     'label'        => 'Phone for delivery',
  7.     'placeholder'  => 'Phone for delivery',
  8.     'required'     => 1,
  9.     'type'         => 'tel',
  10.     'class'        => array( 'form-row-wide' ),
  11.     'validate'     => array( 'phone' ),
  12.     'priority'     => 100
  13.   );
  14.   return $fields;
  15. }
  16.  
  17. add_action( 'woocommerce_new_order', 'add_shipping_phone_to_order' );
  18. function add_shipping_phone_to_order( $order_id ) {
  19.   $shipping_phone = isset( $_POST['shipping_phone'] ) ? $_POST['shipping_phone'] : 'Not set';
  20.   add_post_meta( $order_id, 'shipping_phone', $shipping_phone, true );
  21. } // end function  
  22.  
  23. add_filter( 'woocommerce_email_order_meta_fields', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
  24. function custom_woocommerce_email_order_meta_fields( $fields, $sent_to_admin, $order ) {
  25.   $fields['shipping_phone'] = array(
  26.    'label' => 'Shipping Phone',
  27.    'value' => get_post_meta( $order->get_id(), 'shipping_phone', true ),
  28.   );
  29.   return $fields;
  30. }
  31.  
  32. add_action( 'woocommerce_admin_order_data_after_shipping_address', 'bbloomer_shipping_phone_checkout_display' );
  33. function bbloomer_shipping_phone_checkout_display( $order ){
  34.   print '<p><b>Shipping Phone:</b> ' . get_post_meta( $order->get_id(), 'shipping_phone', true ) . '</p>';
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement