{#
* Plugin Name : ProductOption
*
* Copyright (C) BraTech Co., Ltd. All Rights Reserved.
* http://www.bratech.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
#}
{% block css %}
<style type="text/css">
.datepicker-days th.dow:first-child,
.datepicker-days td:first-child {
color: #f00;
}
.datepicker-days th.dow:last-child,
.datepicker-days td:last-child {
color: #00f;
}
</style>
{% endblock %}
{% block javascript %}
<script src="{{ asset('../../plugin/ProductOption42/assets/js/jquery.plainmodal.min.js') }}"></script>
<script>
$(function() {
//デフォルトのデイトタイムピッカーが存在しない場合
if ($('[type="date"]').prop('type') !== 'date') {
$.getScript("{{ asset('assets/js/vendor/moment.min.js', 'admin') }}").done(function() {
$.getScript("{{ asset('assets/js/vendor/moment-with-locales.min.js', 'admin') }}").done(function() {
$.getScript("{{ asset('assets/js/vendor/tempusdominus-bootstrap-4.min.js', 'admin') }}").done(function() {
$('input[type=date]').datetimepicker({
locale: '{{ eccube_config.locale }}',
format: 'YYYY-MM-DD',
useCurrent: false,
buttons: {
showToday: true,
showClose: true
}
});
});
});
});
}
});
var product_id;
$(function() {
{% set prev_condition = '' %}
{% set count = 0 %}
{% set index = 0 %}
{% for ProductOption in Product.ProductOptions %}
{% set current_condition = '' %}
{% set Option = ProductOption.Option %}
{% set Product = ProductOption.Product %}
{% if '張地' in ProductOption.Option.backend_name %}
{% set current_condition = '張地' %}
{% elseif 'サイズ選択' in ProductOption.Option.backend_name %}
{% set current_condition = 'サイズ' %}
{% else %}
{# 他の条件を追加する場合は、ここに追加してください。 #}
{% endif %}
{% if prev_condition != current_condition %}
{# 最後のカウントの処理 ============================= #}
{% if prev_condition != '' %}
{# 最後の処理 #}
{% endif %}
{# 最初のカウントの処理 ============================= #}
{% set count = 0 %}
{% if (count == 0 and current_condition == '張地') or (count == 0 and current_condition == 'サイズ') %}
{# 最初はオプション全体のモーダルを出力する #}
modal{{ current_condition }} = $('#option_description_{{ current_condition }}').plainModal();
$('#option_description_link_{{ current_condition }}').click(function() {
modal{{ current_condition }}.plainModal('open');
product_id = $(this).attr('data');
return false;
});
{% endif %}
{% endif %}
{# 最初と最後以外の通常処理 ============================= #}
{% if current_condition == '張地' or current_condition == 'サイズ' %}
{# 張地/サイズの場合 #}
{% if Option.description_flg == constant('Plugin\\ProductOption42\\Entity\\OptionCategory::ON') %}
modal{{ Product.id }}_{{ Option.id }} = $('#option_description_{{ Product.id }}_{{ Option.id }}').plainModal();
$(document).on('click', '#option_description_link_{{ Product.id }}_{{ Option.id }}', function(e) {
e.preventDefault();
$('#option_description_{{ current_condition }}').plainModal('close');
// plainModalの 'close' イベントが発生した後に新しいモーダルを開く
$('#option_description_{{ current_condition }}').one('plainmodalclose', function() {
modal{{ Product.id }}_{{ Option.id }}.plainModal('open');
product_id = $('#option_description_link_{{ Product.id }}_{{ Option.id }}').attr('data');
});
});
{% endif %}
{# 戻るボタンで戻る #}
modal{{ current_condition }} = $('#option_description_{{ current_condition }}').plainModal();
$(document).on('click', '#option_back_link_{{ current_condition }}', function(e) {
e.preventDefault();
$('#option_description_{{ Product.id }}_{{ Option.id }}').plainModal('close');
$('#option_description_{{ Product.id }}_{{ Option.id }}').one('plainmodalclose', function() {
modal{{ current_condition }}.plainModal('open');
return false;
});
});
{% else %}
{# 張地/以外の画像オプション #}
{% if Option.description_flg == constant('Plugin\\ProductOption42\\Entity\\OptionCategory::ON') %}
modal{{ Product.id }}_{{ Option.id }} = $('#option_description_{{ Product.id }}_{{ Option.id }}').plainModal();
$(document).on('click', '#option_description_link_{{ Product.id }}_{{ Option.id }}', function(e) {
e.preventDefault();
modal{{ Product.id }}_{{ Option.id }}.plainModal('open');
product_id = $('#option_description_link_{{ Product.id }}_{{ Option.id }}').attr('data');
});
{% endif %}
{% endif %}
{% set prev_condition = current_condition %}
{% set count = count + 1 %}
{% set index = index + 1 %}
{% endfor %}
});
//初期値で全てのセレクトボックスを必須
$(document).ready(function() {
$("select.form-select").attr("required", true);
});
//オプション選択ボタンクリックイベント
$(function() {
$("[id^=desc_btn_]").click(function(){
/*optionが選択されたら選択項目を表示する 後で関数にする*/
//データを取得
var select_data = $(this).data('type');
var select_title = $(this).siblings('h3').text();
var select_img_src = $(this).siblings('img').attr('src');
var select_img_txt = $(this).siblings('p').text();
//表示する前に一度オプションを全て解除する
var targetElementSelect = $('[data-select-type="' + select_data + '"]');
targetElementSelect.children('select').prop("selectedIndex", 0);
//表示するエリアに選択した情報を表示
var targetElement = $('[data-type="' + select_data + '"]');
targetElement.fadeIn();
if(select_img_src){
targetElement.find('img').attr('src', select_img_src);
}else{
targetElement.find('img').hide();
}
targetElement.find('h3').text(select_title);
targetElement.find('p').text(select_img_txt);
//表示エリアの選択状態をtrueにする
$('.choice_display_area[data-type="' + select_data + '"]').attr('data-select-result', true);
//「リストから選択」ボタンを非表示
targetElement.siblings('.choice_display_area__top').children('a.option_description_link_brn').addClass('display_disable');
var form_id;
var id = $(this).attr('id').replace(/^desc_btn_/ig, '');
var ids = id.split('_');
if (eccube.hasOwnProperty('productsClassCategories')) {
form_id = product_id;
}else{
form_id = 1;
}
var $form = $("form[name=form"+form_id+"]");
func_submit($form,ids[0],ids[1]);
onOptionChange($form)
});
$("[name^=productoption]").change(function(){
$form = $(this).parents('form');
onOptionChange($form);
});
});
//選択クリアボタンイベント
$(function() {
$("a.option_delete_link_btn").click(function(){
var data_type = $(this).data('type');
//非表示にする前に一度オプションを全てリセット
var targetElementSelect = $('[data-select-type="' + data_type + '"]');
targetElementSelect.children('select').prop("selectedIndex", 0);
//表示エリアを非表示
if($(this).parent('.choice_display_area').data('type') == data_type){
$(this).parent('.choice_display_area').hide();
}
//選択状態をfalseにする
$('.choice_display_area[data-type="' + data_type + '"]').attr('data-select-result', false);
//モーダルボタンを表示
$('[data-btn-type="' + data_type + '"]').removeClass('display_disable');
//表示金額をリセット
$form = $(this).parents('form');
onOptionChange($form);
});
});
function func_submit($form,optionId, setValue) {
var $sele_option = $form.find("[name=productoption" + optionId + "]");
if($sele_option && $sele_option.length){
var kind = $sele_option.attr("type");
if(kind == 'radio'){
$sele_option.val([setValue]);
}else{
$sele_option.val(setValue);
}
}else{
var $sele_option = $form.find('[name="productoption' + optionId + '[]"]');
if($sele_option && $sele_option.length){
$sele_option.each(function(){
if($(this).val() == setValue){
$(this).prop('checked',true);
}
});
}
}
$('#option_description_' + product_id + '_' + optionId).plainModal('close');
}
var optionPrice = {{ optionPrices|json_encode|raw }};
var optionPoint = {{ optionPoints|json_encode|raw }};
var optionMultiple = {{ optionMultiples|json_encode|raw }};
var taxRules = {{ taxRules|json_encode|raw }};
var default_class_id = {{ default_class_id|json_encode|raw }};
function onOptionChange($form){
if(!$form.length){
return;
}
var optionPriceTotal = 0;
var optionPointTotal = 0;
var tax_rate = null;
var tax_rule = null;
var product_id = $form.find('input[name="product_id"]').val();
var $sele1 = $form.find('select[name=classcategory_id1]');
var $sele2 = $form.find('select[name=classcategory_id2]');
var classcat_id1 = $sele1.val() ? $sele1.val() : '__unselected';
var classcat_id2 = $sele2.val() ? $sele2.val() : '';
if (eccube.hasOwnProperty('productsClassCategories')) {
// 商品一覧時
classcat2 = eccube.productsClassCategories[product_id][classcat_id1]['#' + classcat_id2];
} else {
// 詳細表示時
classcat2 = eccube.classCategories[classcat_id1]['#' + classcat_id2];
}
$form.find("[id^=productoption]").each(function(){
var id = $(this).prop("id");
var name = $(this).attr("name");
var option_id = null;
if(name === undefined){
name = $(this).find("input").prop("id");
}
if(name === undefined)return;
option_id = name.replace(/productoption/ig,'');
option_id = option_id.replace(/_\d+/ig,'');
option_id = option_id.replace(/\[\]/ig,'');
if(id.match(/^productoption\d+$/)){
var kind = $(this).prop('tagName');
var type = $(this).attr('type');
var value = null;
var multi = 1;
switch(kind){
case 'SELECT':
value = $(this).val();
break;
case 'TEXTAREA':
case 'INPUT':
var text = $(this).val();
if(text.length > 0){
value = 0;
}
if(type == 'number'){
if(optionMultiple[product_id][option_id]){
multi = $(this).val();
if(multi.length == 0)multi = 0;
}
}
break;
default:
if($(this).find('input[type="checkbox"]').length > 0){
value = [];
$(this).find('input[type="checkbox"]:checked').each(function(){
value.push($(this).val());
});
}else{
value = $form.find("input[name='productoption" + option_id + "']:checked").val();
}
break;
}
if(value != null){
if(Array.isArray(value)){
for(var key in value){
optionPriceTotal += parseFloat(optionPrice[product_id][option_id][value[key]]);
optionPointTotal += parseFloat(optionPoint[product_id][option_id][value[key]]);
}
}else{
optionPriceTotal += parseFloat(optionPrice[product_id][option_id][value]*multi);
optionPointTotal += parseFloat(optionPoint[product_id][option_id][value]*multi);
}
}
}
});
if(classcat2){
var product_class_id = classcat2.product_class_id;
}else{
var product_class_id = default_class_id[product_id];
}
var tax_rate = taxRules[product_class_id]['tax_rate'];
var tax_rule = taxRules[product_class_id]['tax_rule'];
var $option_price = $form.parent().find('#option_price_default').first();
$option_price.text(number_format(optionPriceTotal));
var $option_point = $form.parent().find('#option_price_inctax_default').first();
$option_point.text(number_format(optionPriceTotal + sfTax(optionPriceTotal, tax_rate, tax_rule)));
//オプション金額を加算した表示金額を表示
var result_price = default_price() + (optionPriceTotal + sfTax(optionPriceTotal, tax_rate, tax_rule));
$('.ec-price__price').text('¥' + result_price.toLocaleString());
}
//商品のデフォルト金額を取得
function default_price() {
var jsonData = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerHTML);
var productPrice = jsonData.offers.price;
return productPrice;
}
function number_format(num) {
return num.toString().replace(/([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,');
}
function sfTax(price, tax_rate, tax_rule) {
real_tax = tax_rate / 100;
ret = price * real_tax;
tax_rule = parseInt(tax_rule);
switch (tax_rule) {
// 四捨五入
case {{ constant('Eccube\\Entity\\Master\\RoundingType::ROUND') }}:
$ret = Math.round(ret);
break;
// 切り捨て
case {{ constant('Eccube\\Entity\\Master\\RoundingType::FLOOR') }}:
$ret = Math.floor(ret);
break;
// 切り上げ
case {{ constant('Eccube\\Entity\\Master\\RoundingType::CEIL') }}:
$ret = Math.ceil(ret);
break;
// デフォルト:切り上げ
default:
$ret = Math.round(ret);
break;
}
return $ret;
}
</script>
{% endblock %}