何か、DrupalでEコマースというと、いつの間にかUberCartが標準になってしまっていますよね。
どうせなら、ストアもコンテンツタイプのようにしてくれれば、マーケットプレイスに出来るのに・・・と、いつもの如く不満を漏らしておりますが。
さて、海外製の宿命として、チェックアウトのアドレスパネルの順番が、日本向きに出来ていなく、そのまま使うのはあまりかっこよくないですよね。
そんなわけで順番を変え、ついでに郵便番号から住所の自動入力をやってしまうためのテーマです。
いつも日本語ローカライズばかり使わせていただいているので、感謝を込めて :)
function theme_address_pane($form) {
drupal_set_html_head('<script src="'. check_url(url('http://ajaxzip3.googlecode.com/svn/trunk/ajaxzip3/ajaxzip3.js')) .'" type="text/javascript"></script>');
$req = '<span class="form-required">*</span>';
if (isset($form['copy_address'])) {
$output .= drupal_render($form['copy_address']);
}
$output .= '<div class="address-pane-table"><table>';
$fields = element_children($form);
$sorted = array('billing_address_select', 'billing_last_name', 'billing_first_name', 'billing_company',
'billing_postal_code', 'billing_country', 'billing_zone', 'billing_city', 'billing_street1', 'billing_street2', 'billing_phone');
$fields = array_intersect($sorted, $fields);
$form['billing_postal_code']['#attributes'] = array_merge(
$form['billing_postal_code']['#attributes'],
array('onKeyUp' => 'AjaxZip3.zip2addr("panes[billing][billing_postal_code]", "", "panes[billing][billing_zone]", "panes[billing][billing_city]");')
);
foreach ($fields as $field) {
if (substr($field, 0, 9) == 'delivery_' || substr($field, 0, 8) == 'billing_') {
$title = $form[$field]['#title'] .':';
unset($form[$field]['#title']);
if (substr($field, -7) == 'street1') {
$title = uc_get_field_name('street') .':';
}
elseif (substr($field, -7) == 'street2') {
$title = ' ';
}
$output .= '<tr><td class="field-label">';
if ($form[$field]['#required']) {
$output .= $req;
}
$output .= $title .'</td><td>'. drupal_render($form[$field]) .'</td></tr>';
}
}
$output .= '</table></div>';
foreach ($fields as $element) {
$output .= drupal_render($form[$element]);
}
return $output;
}
ただし、郵便番号~のスクリプトはnonSecureなため、セキュアな環境でご利用の場合は、すべてダウンロードしてきた方が良いですね。
しかし、このarray_intersetでソートしてしまうというのは中々良い方法のような気がする :p