Tubasa
1
お世話になっております。
表題の通りにmetafieldに入れる値に「\n」が入ってしまい、metafieldに格納できません。
{% assign value = "" %}
{% assign num = "" %}
{% for lineItems_item in order.lineItems %}
{% for customAttributes_item in lineItems_item.customAttributes %}
{% assign value = value | replace: '\n',"" | append: customAttributes_item.value | strip %}
{% endfor %}
{% endfor %}
{% assign values = value | strip | newline_to_br | split: '<br/ >' %}
{%- for num in values -%}
{%- if num contains "-" -%}
{{ num | strip | replace: '<br>',"" | strip_newlines | date: '%-d'}}
{% break %}
{%- endif -%}
{%- endfor -%}
エラー内容
Got error updating metafield: “Value must be an integer.” For value: "\n\n\n16\n "
概要としては商品ページにitem propatyを設定しています。
そちらに誕生日をdate形式で入力できるようにしています。
そちらの値の月、日などに分けてmetafieldに入れようと思っています。
ですが、上記に書いたとおりに「\n」が入ってしまい格納できません。
こちらを解決するためのコードをご存じの方がいらっしゃいましたらご教授をお願い致します。
宜しくお願い致します。
st_mh
2
本来、数値(整数)の値に改行が入ったことで発生したエラーのようです。
処理内容とエラー発生位置を完全に理解しきれていないのですが、処理途中で改行を含んでしまっているのかもしれません。
一旦captureで格納するのも手かもしれません。
{{ num | strip | replace: '
',"" | strip_newlines | date: '%-d'}}
↓↓↓
{%- # capture で格納し、余白等削除して出力 -%}
{% capture variable %}{{ num | strip | replace: '
',"" | strip_newlines | date: '%-d'}}{% endcapture %}
{{ variable | strip_newlines }}
出力結果が数字で構成されたstringならば、フィルタで数値化も出来ます。
(liquidに数値化フィルタは存在しないものの、0を足すと数値化します。)
{{ '22' }}
// stringの'22'が出力される
{{ '22' | plus: 0 }}
// 数値の22が出力される
尤もHTMLとして出力する分にはどちらでも同じです。
あくまで、liquid上で数値として処理させる場合に有用です。
1 Like
Tubasa
3
ありがとうございます。
{% assign value = "" %}
{% assign num = "" %}
{% for lineItems_item in order.lineItems %}
{% for customAttributes_item in lineItems_item.customAttributes %}
{%if customAttributes_item.key contains 'birthday'%}
{% assign value = customAttributes_item.value | replace: '\n',"" | strip %}
{%endif%}
{% endfor %}
{% endfor %}
{% assign values = value | strip | newline_to_br | split: '
' %}
{%- for num in values -%}
{%- if num contains "-" -%}
{% capture variable %}{{ num | strip | replace: '
',"" | strip_newlines | date: '%-m'}}{% endcapture %}
{% break %}
{%- endif -%}
{%- endfor -%}
{{ variable | strip_newlines | plus: 0 }}
上記のコードでためしてみました。
が
Got error updating metafield: “Value must be an integer.” For value: “\n\n\n\n 11”
で帰ってきました。
処理中に改行が入ってしまうというのはあっていると思いますが、
呼び出しの際にはstripで改行タグは消しているのに結果としては表示されている状態でした。
他にもコードを改変し手見ます。
解決したのでこちらで報告をさせていただきます。
{% assign value = "" %}
{% assign num = "" %}
こちらで変数を宣言していましたが、
{%- assign value = "" -%}
{%- assign num = "" -%}
と宣言することで解決しました。
{%- -%}
こちらで構築を行わないと、コード内の改行がそのまま出力されるとのことです。
1 Like