WooCommerce – změna pořadí polí v objednávce

Autoři softwaru pro automatické obchodování na burze quastic.com mě oslovili, abych dokončil jejich web. Součástí úprav byla optimalizace objednávkového procesu, podpora více měn a jazykových mutací, napojení na emailový nástroj GetResponse a spousta dalších úprav WooCommerce.

Jedním z požadavků byla změna pořadí polí na stránce s objednávkou. Na to se ve starších verzích WooCommerce používal filtr woocommerce_checkout_fields, kód vypadal tedy např.:


add_filter( 'woocommerce_checkout_fields', 'quastic_move_checkout_fields' );
  
function quastic_move_checkout_fields( $fields ) {
     
  // Billing: move these around in the order you'd like
     
  $fields2['billing']['billing_first_name'] = $fields['billing']['billing_first_name'];
  $fields2['billing']['billing_last_name']  = $fields['billing']['billing_last_name'];
  $fields2['billing']['billing_company']    = $fields['billing']['billing_company'];
  $fields2['billing']['billing_address_1']  = $fields['billing']['billing_address_1'];
  $fields2['billing']['billing_address_2']  = $fields['billing']['billing_address_2'];
  $fields2['billing']['billing_city']       = $fields['billing']['billing_city'];
  $fields2['billing']['billing_postcode']   = $fields['billing']['billing_postcode'];
  $fields2['billing']['billing_state']      = $fields['billing']['billing_state'];
  $fields2['billing']['billing_country']    = $fields['billing']['billing_country'];
  $fields2['billing']['billing_email']      = $fields['billing']['billing_email'];
  $fields2['billing']['billing_phone']      = $fields['billing']['billing_phone'];
     
  // Shipping: move these around in the order you'd like
     
  $fields2['shipping']['shipping_first_name'] = $fields['shipping']['shipping_first_name'];
  $fields2['shipping']['shipping_last_name']  = $fields['shipping']['shipping_last_name'];
  $fields2['shipping']['shipping_company']    = $fields['shipping']['shipping_company'];
  $fields2['shipping']['shipping_address_1']  = $fields['shipping']['shipping_address_1'];
  $fields2['shipping']['shipping_address_2']  = $fields['shipping']['shipping_address_2'];
  $fields2['shipping']['shipping_city']       = $fields['shipping']['shipping_city'];
  $fields2['shipping']['shipping_postcode']   = $fields['shipping']['shipping_postcode'];
  $fields2['shipping']['shipping_state']      = $fields['shipping']['shipping_state'];
  $fields2['shipping']['shipping_country']    = $fields['shipping']['shipping_country'];
     
  $checkout_fields = array_merge( $fields, $fields2);
  return $checkout_fields;
}

Ve Woocommerce 3.0 a novějších je ale na pořadí polí potřeba použít nový filtr, woocommerce_default_address_fields. Pro nové verze WooCommerce tedy kód je např.:


add_filter( 'woocommerce_default_address_fields', 'quastic_move_checkout_fields_woo_3' );
  
function quastic_move_checkout_fields_woo_3( $fields ) {
 
    // default priorities: 
    // 'first_name' - 10
    // 'last_name' - 20
    // 'company' - 30
    // 'country' - 40
    // 'address_1' - 50
    // 'address_2' - 60
    // 'city' - 70
    // 'state' - 80
    // 'postcode' - 90
  
  // e.g. move 'company' above 'first_name':
  // just assign priority less than 10
  $fields['company']['priority'] = 8;
 
  return $fields;
}

Přidat komentář