آموزش اضافه کردن فیلد شماره منزل به صفحه پرداخت ووکامرس

آموزش اضافه کردن فیلد شماره منزل به صفحه پرداخت ووکامرس

آموزش اضافه کردن فیلد شماره منزل به صفحه پرداخت ووکامرس

هنگام ثبت سفارش ، فیلدی با نام شماره منزل وجود ندارد . شاید شما دوست داشته باشید شماره منزل مشتریان خود را داشته باشید. براحتی میتوانید فیلدی با عنوان شماره منزل اضافه نمایید

برای انجام این کار در ووکامرس کافیست کد زیر را در functions.php قرار دهید

add_filter( 'woocommerce_checkout_fields' , 'a4fran3_add_field_and_reorder_fields' );
 
function a4fran3_add_field_and_reorder_fields( $fields ) {
 
    //a4fran3: Add New Fields
      
    $fields['billing']['billing_houseno'] = array(
    'label'     => __('House Number', 'woocommerce'),
    'placeholder'   => _x('House Number', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-last'),
    'clear'     => true
     );
 
    $fields['shipping']['shipping_houseno'] = array(
    'label'     => __('House Number', 'woocommerce'),
    'placeholder'   => _x('House Number', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-last'),
    'clear'     => true
     );
 
    // Remove Address_2 Fields
 
    unset($fields['billing']['billing_address_2']);
    unset($fields['shipping']['shipping_address_2']);
 
    // Make Address_1 Fields Half Width
 
    $fields['billing']['billing_address_1']['class'] = array('form-row-first');
    $fields['shipping']['shipping_address_1']['class'] = array('form-row-first');
 
    // Billing: Sort Fields
      
    $newfields['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
    $newfields['billing']['billing_last_name']  = $fields['billing']['billing_last_name'];
    $newfields['billing']['billing_company']    = $fields['billing']['billing_company'];
    $newfields['billing']['billing_email']      = $fields['billing']['billing_email'];
    $newfields['billing']['billing_phone']      = $fields['billing']['billing_phone'];
    $newfields['billing']['billing_country']    = $fields['billing']['billing_country'];
    $newfields['billing']['billing_address_1']  = $fields['billing']['billing_address_1'];  
    $newfields['billing']['billing_houseno']    = $fields['billing']['billing_houseno'];
    $newfields['billing']['billing_city']       = $fields['billing']['billing_city'];
    $newfields['billing']['billing_postcode']   = $fields['billing']['billing_postcode'];
    $newfields['billing']['billing_state']      = $fields['billing']['billing_state'];
   
    // Shipping: Sort Fields
      
    $newfields['shipping']['shipping_first_name'] = $fields['shipping']['shipping_first_name'];
    $newfields['shipping']['shipping_last_name']  = $fields['shipping']['shipping_last_name'];
    $newfields['shipping']['shipping_company']    = $fields['shipping']['shipping_company'];
    $newfields['shipping']['shipping_country']    = $fields['shipping']['shipping_country'];
    $newfields['shipping']['shipping_address_1']  = $fields['shipping']['shipping_address_1'];  
    $newfields['shipping']['shipping_houseno']    = $fields['shipping']['shipping_houseno'];
    $newfields['shipping']['shipping_city']       = $fields['shipping']['shipping_city'];
    $newfields['shipping']['shipping_state']      = $fields['shipping']['shipping_state'];
    $newfields['shipping']['shipping_postcode']   = $fields['shipping']['shipping_postcode'];
 
    $checkout_fields = array_merge( $fields, $newfields);
    return $checkout_fields;
}
 
// ------------------------------------
// Add Billing House # to Address Fields
 
add_filter( 'woocommerce_order_formatted_billing_address' , 'a4fran3_default_billing_address_fields', 10, 2 );
 
function a4fran3_default_billing_address_fields( $fields, $order ) {
$fields['billing_houseno'] = get_post_meta( $order->id, '_billing_houseno', true );
return $fields;
}
 
// ------------------------------------
// Add Shipping House # to Address Fields
 
add_filter( 'woocommerce_order_formatted_shipping_address' , 'a4fran3_default_shipping_address_fields', 10, 2 );
 
function a4fran3_default_shipping_address_fields( $fields, $order ) {
$fields['shipping_houseno'] = get_post_meta( $order->id, '_shipping_houseno', true );
return $fields;
}
 
// ------------------------------------
// Create 'replacements' for new Address Fields
 
add_filter( 'woocommerce_formatted_address_replacements', 'add_new_replacement_fields',10,2 );
 
function add_new_replacement_fields( $replacements, $address ) {
$replacements['{billing_houseno}'] = isset($address['billing_houseno']) ? $address['billing_houseno'] : '';
$replacements['{shipping_houseno}'] = isset($address['shipping_houseno']) ? $address['shipping_houseno'] : '';
return $replacements;
}
 
// ------------------------------------
// Show Shipping & Billing House # for different countries
 
add_filter( 'woocommerce_localisation_address_formats', 'a4fran3_new_address_formats' );
 
function a4fran3_new_address_formats( $formats ) {
$formats['IE'] = "{name}\n{company}\n{address_1}\n{billing_houseno}\n{shipping_houseno}\n{city}\n{state}\n{postcode}\n{country}";
$formats['UK'] = "{name}\n{company}\n{address_1}\n{billing_houseno}\n{shipping_houseno}\n{city}\n{state}\n{postcode}\n{country}";
return $formats;
}

 

لینک کوتاه:

https://a4fran3.ir/?p=17886

Subscribe
Notify of
guest
0 نظرات
Inline Feedbacks
مشاهده همه نظرات

مطالب تصادفی