はじめに
WordpressのECサイト化プラグイン「Welcart」を扱った案件で必要になったカスタマイズ方法についてまとめておきます。
カスタムフィールドを扱う方法
Welcartでのカスタムフィールドを扱う方法には公式サイトに記載があります。
- カスタムフィールドの表示をする。
少しわかりづらいところにあるのですが、まずはカスタムフィールドを表示するようにします。
右上に[表示オプション]を押下し、カスタムフィールドにチェックを入れます。
- カスタムフィールドを設定します。
カスタムフィールドの名前は「wccs_」から始める必要があるのに注意します。
表示されるファイルの場所
商品詳細ページ:welcart/wp-content/themes/welcart_default/wc_templates/wc_item_single.php
カスタムフィールド出力する関数があるファイル:wp-content/plugins/usc-e-shop/functions/template_func.php
以下カスタムフィールド出力している関数
function usces_get_item_custom( $post_id, $type = 'list', $out = '' ){
global $usces;
$cfields = $usces->get_post_custom($post_id);
switch( $type ){
case 'list':
$list = '';
$html = '<ul class="item_custom_field">'."\n";
foreach( $cfields as $key => $value ){
if( 'wccs_' == substr($key, 0, 5) ){
$list .= '<li>' . esc_html(substr($key, 5)) . ' : ' . nl2br(esc_html($value[0])) . '</li>'."\n";
}
}
if(empty($list)){
$html = '';
}else{
$html .= $list . '</ul>'."\n";
}
break;
case 'table':
$list = '';
$html = '<table class="item_custom_field">'."\n";
foreach($cfields as $key => $value){
if( 'wccs_' == substr($key, 0, 5) ){
$list .= '<tr><th>' . esc_html(substr($key, 5)) . '</th><td>' . nl2br(esc_html($value[0])) . '</td></tr>'."\n";
}
}
if(empty($list)){
$html = '';
}else{
$html .= $list . '</table>'."\n";
}
break;
case 'notag':
$list = '';
foreach($cfields as $key => $value){
if( 'wccs_' == substr($key, 0, 5) ){
$list .= esc_html(substr($key, 5)) . ' : ' . nl2br(esc_html($value[0])) . "\r\n";
}
}
if(empty($list)){
$html = '';
}else{
$html = $list;
}
break;
}
$html = apply_filters( 'usces_filter_item_custom', $html, $post_id, $type );
if( 'return' == $out){
return $html;
}else{
echo $html;
}
}
個人的な意見...。
今回welcartのプログラムを眺めているのですが、、、
ifの中で代入していたり、、、
引数で'return'という文字列を渡し、その文字列があるかどうかで、値を返すかどうかを決めているのは、
ちょっとプログラム設計的にどうなんだ。。。っていう感じがします...
参考リンク
i. カスタムフィールド @ Welcart オンライン・マニュアル|ECサイト構築プラグイン
https://www.welcart.com/documents/manual-2/%E6%96%B0%E8%A6%8F%E5%95%86%E5%93%81%E8%BF%BD%E5%8A%A0/i-%E3%82%AB%E3%82%B9%E3%82%BF%E3%83%A0%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89