お客様にどのようにしてストアを知ったのかを尋ねる
「当店をどのように知りましたか?」フォームフィールドをカートページに追加することで、お客様にはストアをどのようにして知ったのかを尋ねることができます。
「当店をどのように知りましたか?」フォームフィールドを作成する
「当店をどのように知りましたか?」フォームフィールド作成するには、次の手順を実行してください。
管理画面から [オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
スニペットディレクトリーで、[新しいスニペットを追加する] をクリックします。
次の手順でスニペットを作成します。
新しい
hear-about-us.liquid
スニペットで、次のコードを貼り付けます。
<style>
#how-did-you-hear-about-us--label,
#how-did-you-hear-about-us-other--label {
font-weight: bold;
}
select#how-did-you-hear-about-us,
input#how-did-you-hear-about-us-other {
width: 100%;
padding: 8px 10px 8px 10px;
line-height: 1.2;
}
#how-did-you-hear-about-us,
#how-did-you-hear-about-us-other,
#how-did-you-hear-about-us--label,
#how-did-you-hear-about-us--error,
#how-did-you-hear-about-us-other--label,
#how-did-you-hear-about-us-other--error {
display:block;
margin-bottom: 5px;
}
#how-did-you-hear-about-us-other.error,
#how-did-you-hear-about-us.error {
border: 2px solid {{ settings.hau_error_color }};
}
#how-did-you-hear-about-us--error,
#how-did-you-hear-about-us-other--error {
color: {{ settings.hau_error_color }};
}
</style>
<div class="form-vertical">
<p>
<label id="how-did-you-hear-about-us--label" for="how-did-you-hear-about-us">How did you hear about us?</label>
<span id="how-did-you-hear-about-us--error" style="display:none">{{ settings.hau_error_message }}</span>
<select id="how-did-you-hear-about-us" name="attributes[how-did-you-hear-about-us]">
<option value=""{% if cart.attributes.how-did-you-hear-about-us == "" %} selected{% endif %}>Please make a selection</option>
{% assign optionsArray = settings.hau_form_options | split: ',' %}
{% for o in optionsArray %}
{% assign option = o | strip %}
<option value="{{ option }}"{% if cart.attributes.how-did-you-hear-about-us == option %} selected{% endif %}>{{ option }}</option>
{% endfor %}
<option value="Other"{% if cart.attributes.how-did-you-hear-about-us == "Other" %} selected{% endif %}>Other</option>
</select>
</p>
<p id="otherFormWrapper" style="{% unless cart.attributes.how-did-you-hear-about-us == 'Other' %}display:none{% endunless %}">
<label id="how-did-you-hear-about-us-other--label" for="how-did-you-hear-about-us-other">Other</label>
<span id="how-did-you-hear-about-us-other--error" style="display:none">{{ settings.hau_error_message_other }}</span>
<input id="how-did-you-hear-about-us-other" type="text" name="attributes[how-did-you-hear-about-us-other]" value="{{ cart.attributes.how-did-you-hear-about-us-other }}"/>
</p>
</div>
<script>
(function() {
document.addEventListener("DOMContentLoaded", initForm);
document.addEventListener("shopify:section:load", initForm);
function initForm(){
var formElement = document.querySelector('#how-did-you-hear-about-us');
var formError = document.querySelector('#how-did-you-hear-about-us--error');
var otherFormElement = document.querySelector('#how-did-you-hear-about-us-other');
var otherFormError = document.querySelector('#how-did-you-hear-about-us-other--error');
var otherFormWrapper = document.querySelector('#otherFormWrapper');
var checkoutButtons = document.querySelectorAll('[name="checkout"], [name="goto_pp"], [name="goto_gc"]');
function showOrHideForm(){
if (formElement.value == 'Other'){
otherFormWrapper.style.display = '';
} else {
otherFormWrapper.style.display = 'none';
}
}
function checkFormElement(){
if (formElement.value.length == 0){
formElement.classList.add('error');
formError.style.display = '';
} else {
if (formElement.classList.contains('error')){
formElement.classList.remove('error');
formError.style.display = 'none';
}
}
}
function checkOtherFormElement(){
if (otherFormElement.value.length == 0){
otherFormElement.classList.add('error');
otherFormError.style.display = '';
} else {
if (otherFormElement.classList.contains('error')){
otherFormElement.classList.remove('error');
otherFormError.style.display = 'none';
}
}
}
otherFormElement.addEventListener("input", function() {
{% if settings.hau_form_validation %}
checkOtherFormElement();
{% endif %}
});
formElement.addEventListener("change", function() {
showOrHideForm();
{% if settings.hau_form_validation %}
checkFormElement();
{% endif %}
});
checkoutButtons.forEach(function(element){
element.addEventListener("click", function(evt) {
{% if settings.hau_form_validation %}
var validated = true;
if (formElement.value.length == 0){
checkFormElement();
validated = false;
}
if (formElement.value == 'Other'){
if (otherFormElement.value.length == 0){
checkOtherFormElement();
validated = false;
}
}
if (!validated) {
evt.preventDefault();
evt.stopPropagation();
}
{% endif %}
});
});
}
})()
</script>
- [保存] をクリックします。
カートページにスニペットを追加する
カートページに「当店をどのように知りましたか?」スニペットを含めるには、次の手順を実行します。
[セクション] ディレクトリで、[
cart-template.liquid
] をクリックします。テーマに [cart-template.liquid
] がない場合は、[テンプレート] ディレクトリの [cart.liquid
] をクリックします。コードで
</form>
終了タグを探します。</form>
終了タグの上の新しい行に、次のコードを貼り付けます。
{% render 'hear-about-us' %}
- [保存] をクリックします。
構成のテーマ設定を追加する
[コンフィグ] ディレクトリーで、[
settings_schema.json
] をクリックします。コードで最初の閉じ中かっこ「
},
」を探します。閉じ中かっこ「},
」の下の新しい行に、次のコードを貼り付けます。
{
"name": "Hear About Us",
"settings": [
{
"type": "text",
"id": "hau_form_options",
"label": "Form options",
"default": "Facebook, Twitter, Google, Instagram, Youtube",
"info": "Separate each option with a comma"
},
{
"type": "header",
"content": "Form validation"
},
{
"type": "checkbox",
"id": "hau_form_validation",
"label": "Enable form validation",
"default": true
},
{
"type": "text",
"id": "hau_error_message",
"label": "Error message",
"info": "The error message that is displayed when no selection is made",
"default": "Please select an option below"
},
{
"type": "text",
"id": "hau_error_message_other",
"label": "Other field error message",
"info": "The error message that is displayed when there is no input in the 'Other' field",
"default": "Please fill the text field below"
},
{
"type": "header",
"content": "Error styling"
},
{
"type": "color",
"id": "hau_error_color",
"label": "Color",
"default": "#ff0000"
}
]
},
- [保存] をクリックします。
フォームフィールドを必須にする
お客様が「当店をどのように知りましたか?」フォームフィールドで、オプションを選択せずにチェックアウトに進んでしまうことを防ぐために、このフィールドを必須にすることができます。
[セクション] ディレクトリで、[
cart-template.liquid
] をクリックします。テーマに [cart-template.liquid
] がない場合は、[テンプレート] ディレクトリの [cart.liquid
] をクリックします。カートフォームコードで、次の
novalidate
属性を探します。
novalidate
-
novalidate
属性を次のコードに置き換えます。
{% unless settings.hau_form_validation %}novalidate{% endunless %}
[保存] をクリックします。
テーマエディタで、サイドバーの [テーマ設定] をクリックします。
[当店について知る] タブをクリックします。
[フォーム検証] で、[フォーム検証を有効にする] の設定が有効になっていることを確認します。
オプションを設定する
[当店をどのように知りましたか?] ドロップダウンメニューのオプションをカスタマイズするには、テーマエディタ内のテーマ設定を使用して、オプションを編集することができます。
テーマエディタで、[テーマ設定] をクリックします。
[当店について知る] タブをクリックします。
[フォームオプション] で、オプションをカンマで区切って追加または削除します。
[保存] をクリックします。
よくある質問
回収されたデータはどこに表示されますか?
管理画面の注文の [追加詳細] セクションに、回答が表示されます。
リストされたオプションに当てはまる回答がない場合、お客様は何か入力できますか?
はい、入力できます。お客様が [その他] を選択すると、回答を入力できるテキストボックスがドロップダウンメニューの下に表示されます。
新しい注文通知メールに回答を表示できますか?
はい。新しい注文通知メールにお客様の回答を表示するには、次の操作を実行します。
- 管理画面の [通知ページ] に移動し、[新しい注文] メールテンプレートに次のコードを追加します。
This customer heard about us through {% if attributes.how-did-you-hear-about-us-other == blank %}{{ attributes.how-did-you-hear-about-us }}{% else %}{{ attributes.how-did-you-hear-about-us-other }}{% endif %}.
- [保存] をクリックします。