在结账时通过 URL 预填充自定义字段

时间:2021-07-26 20:28:18

标签: wordpress woocommerce

我的 function.php 文件中有允许我在结账时预填字段的代码。它适用于计费字段。我现在需要它来处理订单字段。我的 php 是有限的,所以我正在尝试这个。 $address_fields['order']['order_comments']['default'] = $order_comments; 处似乎出了点问题,但我不确定。有什么想法吗?

// Autofill checkout fields from URL
 
add_filter( 'woocommerce_checkout_fields' , 'prefill_billing_fields' );
 
function prefill_billing_fields ( $address_fields ) {
 
 
   // Get the data from the URL
 
   if ( isset( $_GET['fname'] ) || isset( $_GET['lname'] ) || isset( $_GET['email'] )|| isset( $_GET['order_comments'] ) || isset( $_GET['order_store'] )) 
   {
 
   // wp_die();
 
       $fname = isset( $_GET['fname'] ) ? esc_attr( $_GET['fname'] ) : '';
       $lname = isset( $_GET['lname'] ) ? esc_attr( $_GET['lname'] ) : '';
       $em   = isset( $_GET['email'] ) ? esc_attr( $_GET['email'] ) : '';
       $order_store   = isset( $_GET['order_store'] ) ? esc_attr( $_GET['order_store'] ) : '';
       $order_comments   = isset( $_GET['order_comments'] ) ? esc_attr( $_GET['order_comments'] ) : '';
       
 
       // First Name
 
      if( isset($_GET['fname']) && ! empty($_GET['fname']) ){
         if( isset( $address_fields['billing']['billing_first_name'] ) ){
             $address_fields['billing']['billing_first_name']['default'] = $fname;
         }
      }
 
      // Last Name
 
      if( isset($_GET['lname']) && ! empty($_GET['lname']) ){
          if( isset( $address_fields['billing']['billing_last_name'] ) ){
             $address_fields['billing']['billing_last_name']['default'] = $lname;
          }
      }
      // order_comments
 
      if( isset($_GET['order_comments']) && ! empty($_GET['order_comments']) ){
          if( isset( $address_fields['order']['order_comments'] ) ){
             $address_fields['order']['order_comments']['default'] = $order_comments;
          }
      }    
      // order_store
 
      if( isset($_GET['order_store']) && ! empty($_GET['order_store']) ){
          if( isset( $address_fields['order']['order_store'] ) ){
             $address_fields['order']['order_store']['default'] = $order_store;
          }
      }
 
      // Email
 
      if( isset($_GET['email']) && ! empty($_GET['email']) ){
          if(isset( $address_fields['billing']['billing_email'] )){
             $address_fields['billing']['billing_email']['default'] = $em;
          }
      }
   }
    
   return $address_fields;
}

0 个答案:

没有答案
相关问题