ecforceのマイページトップで特殊な対応事例をまとめてみる(備忘録)

今回はecforceのマイページトップで特殊な対応事例をまとめていきます(随時更新予定です)。
特殊な事例というのは、下記のテーマガイドに載っていない内容になります。

EC Force(ECフォース) MALL GUIDE テーマガイド

例えば、お知らせ一覧を掲載してほしいなどといった要望が意外とあります。
そんなときに、下記のお知らせ一覧のテーマガイドを参考にすれば動的に反映できるのではないかと思いがちなのですが、実はこのliquidのコードをそのまま反映してもマイページトップでは使うことができない仕様みたいです。

EC Force(ECフォース) MALL GUIDE テーマガイド

こういったことが、お知らせ一覧の掲載以外でも数多くあるので、これまで経験してきてよく使う事例を紹介していきます。

下記に紹介するコードの注意点としては、実装するページのテーマガイドに掲載している最低限必要なコードを記載した上で使ってください。まっさらな状態のファイルに下記のコードを貼っても動きません。

会員ランクの出し分け

管理画面では、下記の場所になります。ここでランクの設定を終えている必要があります。今回はランク名をA、B、C、Dと設定しています。

続いてコードです。
current_customer.customer_rank.nameで上記画像の「ランク名」を取得できます。
あとはこのランク名がないときは「なし」と表示し、Aの場合は「A」のランクが表示されるといったように対応しています。

// spanタグは仮ですので、任意のタグを使用できます。

{% unless current_customer.customer_rank.name == 'A' or current_customer.customer_rank.name == 'B' or current_customer.customer_rank.name == 'C' or current_customer.customer_rank.name == 'D' %}
<span>なし</span>
{% endunless %}
{% if current_customer.customer_rank.name == 'A' %}
<span>A</span>
{% endif %}
{% if current_customer.customer_rank.name == 'B' %}
<span>B</span>
{% endif %}
{% if current_customer.customer_rank.name == 'C' %}
<span>C</span>
{% endif %}
{% if current_customer.customer_rank.name == 'D' %}
<span>D</span>
{% endif %}

お知らせ(ニュース)一覧を表示させる

お知らせ一覧は、よくトップページの更新欄にあるようなものだと思ってください。

お知らせ一覧の実装については、なかなか難しく理解できていない部分もありますので、コードを2パターン紹介します。

なぜ2パターンの紹介なのかというと、使っているテーマの影響によるのか実装できる場合とできない場合があったからです。原因が分かり次第、解説も交えていきたいと思っています。

パターン1

{% assign count = 0 %}
{% for category in information_categories %}
{% if category.slug == 'ニュースのカテゴリー名' %}  // ここでニュースカテゴリー管理のスラッグ名を指定
{% assign informations_sort = category.informations | sort: 'published_at' | reverse %}
{% for information in informations_sort | limit: 3 %} // ここで最大表示数を指定
<li>
  {{ information.published_at | date: '%Y年%m月%d日' }}<br>
  {{ information.title }}<br>
  <a href="/shop/information/{{ information.slug }}">詳細をみる</a>
</li>
{% assign count = count | plus: 1 %}
{% endfor%}
{% endif %}
{% endfor %}
{% if count == 0 %}
<li>
  現在のお知らせはありません。
</li>
{% endif %}

ニュースのカテゴリーで表示を制御しています。

category.slug == ‘ニュースのカテゴリー名’の「ニュースのカテゴリー名」は、管理画面の下記の画像の場所の文字列を記載してください。

limit: 3については、ニュースの最大表示数を制御できます。3の場合は最大3件表示されるようになっています。

パターン2

{% assign news_num = 0 %}
{% assign news_cat = '' %}
{% for category in information_categories %}
{% if category.slug == 'ニュースのカテゴリー名' %}
{% assign news_cat = category %}
{% for information in category.informations %}
{% unless information.state == 'inactive' %}
{% assign news_num = news_num | plus: 1 %}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
{% if news_num > 0 %}
<h2>
  ニュース一覧
</h2>
{% assign show_count = 0 %}
<ul>
  {% assign _informations = news_cat.informations | reverse %}
  {% for information in _informations %}
    {% unless information.state == 'inactive' %}
      {% assign show_count = show_count | plus: 1 %}
      <li>
        <a href="/shop/information/{{ information.slug }}">
          <p>{{ information.published_at | date: '%Y/%m/%d' }}</p>
          <p>{{ information.title }}</p>
        </a>
      </li>
    {% endunless %}
    {% if show_count >= 3 %}
      {% break %}
    {% endif %}
  {% endfor %}
  {% if show_count >= 3 %}
    <div>
      <a href="/shop/information_categories/ニュースのカテゴリー名">お知らせ一覧へ</a>
    </div>
  {% endif %}
</ul>
{% endif %}

こちらは変数でニュースの番号を0、カテゴリーを空欄にして、for文の中で、0が1ずつ増え、カテゴリーを設定したものに入れる処理が行われています。

assign _informations = news_cat.informations | reverseで逆順かつ_informationsがnews_cat.informationsに代入されるかたちになります。

あとはパターン1とほぼ同様の処理が行われています。

以上になりますが、自分でも難解でなんでこういう記述をする発想になるのかの理解ができていませんので、細かい解説はしません。

利用中の定期購入一覧を表示させる

マイページトップのテーマガイドには、通常商品の購入履歴一覧の実装方法は載っていますが、定期購入一覧の実装方法は載っていません。

定期注文一覧のテーマガイドを参考に実装しても反映されないため難解な場所だと思いました。
意外とクライアント側から要望がある部分ですので、下記にコードを紹介します。

<h2>{{ 'subs_orders.general.exist_subs_order.value' | t }}</h2>
<ul>
  {% if customer.subs_orders.size > 0 %}
  {% for subs_order in customer.subs_orders %}
  {% if subs_order.scheduled_to_be_delivered_at and subs_order.human_state_name == "有効" %}
  <li>
    <p>注文番号</p>
    <a href="/shop/customer/subs_orders/{{ subs_order.number }}">{{ order.number }}</a>
  
    <p>次回配送予定日</p>
    <time>{{ subs_order.scheduled_to_be_delivered_at | date: "%Y/%m/%d" }}</time><br>
    <a href="/shop/customer/subs_orders/{{ subs_order.number }}#baseinfo">配送スケジュールページへのリンク</a>

    <p>次回ご注文金額</p>
    <p>{{ subs_order.subtotal | number_to_currency: format: '%n %u' }}</p>
    
		// 下記はデフォルトのclassをつけたままにしています
    <div class="p-history_list__item__detail">
      {% for item in subs_order.order_items %}
      <div class="c-cart_item u-color__border--border">
        <a class="c-cart_item__img" href="/shop/products/{{ item.product.master.sku }}">
          {% if item.variant.thumbnails.first.url %}
          {% assign thumbnail = item.variant.thumbnails.first.url  %}
          {% elsif item.product.master.thumbnails.url %}
          {% assign thumbnail = item.product.master.thumbnails.url  %}
          {% elsif item.product.thumbnail.url %}
          {% assign thumbnail = item.product.thumbnail.url  %}
          {% else %}
          {% assign thumbnail = 'missing' | image_url  %}
          {% endif %}
          <div class="c-cart_item__img__inner" style="background-image:url('{{ thumbnail }}')"></div>
        </a>
        <div class="c-cart_item__info">
          <a class="c-cart_item__info__title u-text--subhead" href="/shop/products/{{ item.product.master.sku }}">
            {{ item.product.name }}</a>
          <div class="c-cart_item__info__variant u-text--body">
            {% assign option_cnt = 0 %}
            {% for option_value in item.variant.option_values %}
            {% unless option_cnt == 0 %}
             / 
            {% endunless %}
            {{ option_value.option_type.presentation }}:
            {{ option_value.presentation }}
            {% assign option_cnt = option_cnt | plus: 1 %}
            {% endfor %}
          </div>
        </div>
      </div>
      {% endfor %}
    </div>
  </li>
  {% endif %}
  {% endfor %}
  {% else %}
  <li class="p-history_list__item u-text--body">
    <p>
      {{ 'subs_orders.general.no_recurring_product.value' | t }}
    </p>
  </li>
  {% endif %}
</ul>

この実装でどういう見た目になるのかというと、CSSの調整ももちろんしなければならないのですが、イメージとしては下記の感じになります。

subs_ordersというのが定期商品扱いになります。
やっている事自体は、通常商品の購入履歴一覧とあまり変わらないと思いますので、解説は省略します。

人気(おすすめ)商品を表示させる

下記のコードになるのだが、自分でも理解できていない部分があるので、解説はできません。
sort: ‘option01’がどこから引っ張ってきているのかが読み解けなかったため、それが読み解ければ解説できそうです。

<h2>人気商品一覧</h2>
<ul>
  {% assign products = products | sort: 'option01'  %}
  {% for product in products limit:3 %}
  <li>
    {% if product.thumbnail.url %}
    {% assign src = product.thumbnail.url %}
    {% else %}
    {% assign src = 'missing' | image_url %}
    {% endif %}
    <img src="{{ src }}" alt="">
    <p>{{ product.name }}</p>
    <p>{{ product.first_price | number_to_currency: format: '%n' }}</p>
    <a href="/shop/products/{{ product.master.sku }}">商品詳細ページへ</a>
  </li>
  {% endfor %}
</ul>

以上になります。
まだまだ分からない部分が多いですが、備忘録として残しておくことで困っている方々の役に立てると幸いです。