2014年8月21日木曜日

Magentoで顧客の名前の表示を日本に合わせる

Magentoについての説明は他のサイトにお任せして割愛します。

例として以下の名前の登録の場合(ミドルネームは省略します)、
姓:日本
名:太郎

顧客の名前表示は次のようになってしまうはずです。
デフォルトの状態:太郎 日本様など

ここで厄介なのが姓名が逆転して表示されてしまう。
グローバルなお店では問題ないかもしれませんが、
日本国内で商売する店なら「日本 太郎」と表示したいところ。

テンプレートごとにやってもいいと思います。
explode関数などでゴリゴリやるのもありです。

全部のテンプレートファイルをチェックするのも面倒ですし、
見落としが合ってもいやなので根本的な解決法を手探りで探しました。

とりあえず、対策した方法を紹介します。
もっとよい方法があったら教えてくださいm(_ _)m

【注意事項】
・ファイルパスなどは適宜に読み替えて下さいね。
・バックアップをとりましょう。
・自己責任でお願いします。
編集するファイル(code/core/Mage/Customer/Model/Customer.php)の236行目あたり。

【修正箇所】
・標準の国設定を取得するようにした(※日本とそれ以外の国で表示を変えたいため)
$countryCode = Mage::getStoreConfig('general/country/default');
・日本の場合とそれ以外の国で場合分け(※日本のコードはJPでした)
if ($countryCode === 'JP')
 特定の国の表示を変更したい場合はもっと分岐を増やせば対応できそう。
・名前の表示順序
できるだけもとのソースコードを流用したかったので汚いソースコードですが、
そのままif分にぶっこみました。

修正前と修正後の関数を下記に載せておきますので参考までに。

■修正前の元の関数
    public function getName()
    {
        $name = '';
        $config = Mage::getSingleton('eav/config');
        if ($config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
            $name .= $this->getPrefix() . ' ';
        }
        $name .= $this->getFirstname();
        if ($config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
            $name .= ' ' . $this->getMiddlename();
        }
        $name .=  ' ' . $this->getLastname();
        if ($config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
            $name .= ' ' . $this->getSuffix();
        }
        return $name;
    }

■修正後の関数
    public function getName()
    {
        $name = '';
        $config = Mage::getSingleton('eav/config');
        // Get store country
        $countryCode = Mage::getStoreConfig('general/country/default');
        // Japan
        if ($countryCode === 'JP') {
            $name .=  $this->getLastname();
            if ($config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
                $name .= ' ' . $this->getSuffix();
            }
            if ($config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
                $name .= ' ' . $this->getMiddlename();
            }
            if ($config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
                $name .= $this->getPrefix() . ' ';
            }
            $name .= ' ' .$this->getFirstname();
        }
        // Other
        else {
            if ($config->getAttribute('customer', 'prefix')->getIsVisible() && $this->getPrefix()) {
                $name .= $this->getPrefix() . ' ';
            }
            $name .= $this->getFirstname();
            if ($config->getAttribute('customer', 'middlename')->getIsVisible() && $this->getMiddlename()) {
                $name .= ' ' . $this->getMiddlename();
            }
            $name .=  ' ' . $this->getLastname();
            if ($config->getAttribute('customer', 'suffix')->getIsVisible() && $this->getSuffix()) {
                $name .= ' ' . $this->getSuffix();
            }
        }
        return $name;
    }

0 件のコメント:

コメントを投稿