app/Plugin/ProductOption42/Resource/template/default/Product/option_js.twig line 1

Open in your IDE?
  1. {#
  2. * Plugin Name : ProductOption
  3. *
  4. * Copyright (C) BraTech Co., Ltd. All Rights Reserved.
  5. * http://www.bratech.co.jp/
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. 修正年月日:2025/07/24 k.saito
  10. 修正内容:function onOptionChange 内のcheckboxの処理が不十分。修正。
  11. #}
  12. {% block css %}
  13. <style type="text/css">
  14.     .datepicker-days th.dow:first-child,
  15.     .datepicker-days td:first-child {
  16.         color: #f00;
  17.     }
  18.     .datepicker-days th.dow:last-child,
  19.     .datepicker-days td:last-child {
  20.         color: #00f;
  21.     }
  22. </style>
  23. {% endblock %}
  24. {% block javascript %}
  25. <script src="{{ asset('../../plugin/ProductOption42/assets/js/jquery.plainmodal.min.js') }}"></script>
  26. <script>
  27. $(function() {
  28.     //デフォルトのデイトタイムピッカーが存在しない場合
  29.     if ($('[type="date"]').prop('type') !== 'date') {
  30.         $.getScript("{{ asset('assets/js/vendor/moment.min.js', 'admin') }}").done(function() {
  31.             $.getScript("{{ asset('assets/js/vendor/moment-with-locales.min.js', 'admin') }}").done(function() {
  32.                 $.getScript("{{ asset('assets/js/vendor/tempusdominus-bootstrap-4.min.js', 'admin') }}").done(function() {
  33.                     $('input[type=date]').datetimepicker({
  34.                         locale: '{{ eccube_config.locale }}',
  35.                         format: 'YYYY-MM-DD',
  36.                         useCurrent: false,
  37.                         buttons: {
  38.                             showToday: true,
  39.                             showClose: true
  40.                         }
  41.                     });
  42.                 });
  43.             });
  44.         });
  45.     }
  46. });
  47. var product_id;
  48. $(function() {
  49.     {% for ProductOption in ProductOptions %}
  50.     {% set Option = ProductOption.Option %}
  51.     {% set Product = ProductOption.Product %}
  52.         {% if Option.description_flg == constant('Plugin\\ProductOption42\\Entity\\OptionCategory::ON') %}
  53.             modal{{ Product.id }}_{{ Option.id }} = $('#option_description_{{ Product.id }}_{{ Option.id }}').plainModal();
  54.             $('#option_description_link_{{ Product.id }}_{{ Option.id }}').click(function() {
  55.                 modal{{ Product.id }}_{{ Option.id }}.plainModal('open');
  56.                 product_id = $(this).attr('data');
  57.                 return false;
  58.             });
  59.         {% endif %}
  60.     {% endfor %}
  61. });
  62. $(function() {
  63.     $("[id^=desc_btn_]").click(function(){
  64.         var form_id;
  65.         var id = $(this).attr('id').replace(/^desc_btn_/ig, '');
  66.         var ids = id.split('_');
  67.         if (eccube.hasOwnProperty('productsClassCategories')) {
  68.             form_id = product_id;
  69.         }else{
  70.             form_id = 1;
  71.         }
  72.         var $form = $("form[name=form"+form_id+"]");
  73.         func_submit($form,ids[0],ids[1]);
  74.         onOptionChange($form)
  75.     });
  76.     $("[name^=productoption]").change(function(){
  77.         $form = $(this).parents('form');
  78.         onOptionChange($form);
  79.     });
  80. });
  81. function func_submit($form,optionId, setValue) {
  82.     var $sele_option = $form.find("[name=productoption" + optionId + "]");
  83.     if($sele_option && $sele_option.length){
  84.         var kind = $sele_option.attr("type");
  85.         if(kind == 'radio'){
  86.             $sele_option.val([setValue]);
  87.         }else{
  88.             $sele_option.val(setValue);
  89.         }
  90.     }else{
  91.         var $sele_option = $form.find('[name="productoption' + optionId + '[]"]');
  92.         if($sele_option && $sele_option.length){
  93.             $sele_option.each(function(){
  94.                 if($(this).val() == setValue){
  95.                     $(this).prop('checked',true);
  96.                 }
  97.             });
  98.         }
  99.     }
  100.     $('#option_description_' + product_id + '_' + optionId).plainModal('close');
  101. }
  102. var optionPrice = {{ optionPrices|json_encode|raw }};
  103. var optionPoint = {{ optionPoints|json_encode|raw }};
  104. var optionMultiple = {{ optionMultiples|json_encode|raw }};
  105. var taxRules = {{ taxRules|json_encode|raw }};
  106. var default_class_id = {{ default_class_id|json_encode|raw }};
  107. function onOptionChange($form){
  108.     if(!$form.length){
  109.         return;
  110.     }
  111.     var optionPriceTotal = 0;
  112.     var optionPointTotal = 0;
  113.     var tax_rate = null;
  114.     var tax_rule = null;
  115.     var product_id = $form.find('input[name="product_id"]').val();
  116.     var $sele1 = $form.find('select[name=classcategory_id1]');
  117.     var $sele2 = $form.find('select[name=classcategory_id2]');
  118.     var classcat_id1 = $sele1.val() ? $sele1.val() : '__unselected';
  119.     var classcat_id2 = $sele2.val() ? $sele2.val() : '';
  120.     if (eccube.hasOwnProperty('productsClassCategories')) {
  121.         // 商品一覧時
  122.         classcat2 = eccube.productsClassCategories[product_id][classcat_id1]['#' + classcat_id2];
  123.     } else {
  124.         // 詳細表示時
  125.         classcat2 = eccube.classCategories[classcat_id1]['#' + classcat_id2];
  126.     }
  127.     $form.find("[id^=productoption]").each(function(){
  128.         var id = $(this).prop("id");
  129.         var name = $(this).attr("name");
  130.         var option_id = null;
  131.         if(name === undefined){
  132.             name = $(this).find("input").prop("id");
  133.         }
  134.         if(name === undefined)return;
  135.         option_id = name.replace(/productoption/ig,'');
  136.         option_id = option_id.replace(/_\d+/ig,'');
  137.         option_id = option_id.replace(/\[\]/ig,'');
  138.         if(id.match(/^productoption\d+$/)){
  139.             var kind = $(this).prop('tagName');
  140.             var type = $(this).attr('type');
  141.             var value = null;
  142.             // チェックボックス用配列
  143.             var chk_value = [];
  144.             var multi = 1;
  145.             switch(kind){
  146.                 case 'SELECT':
  147.                     value = $(this).val();
  148.                     break;
  149.                 case 'TEXTAREA':
  150.                 case 'INPUT':
  151.                     var text = $(this).val();
  152.                     if(text.length > 0){
  153.                         value = 0;
  154.                     }
  155.                     if(type == 'number'){
  156.                         if(optionMultiple[product_id][option_id]){
  157.                             multi = $(this).val();
  158.                             if(multi.length == 0)multi = 0;
  159.                         }
  160.                     }
  161.                     break;
  162.                 default:
  163.                     if($(this).find('input[type="checkbox"]').length > 0){
  164.                         chk_value = [];
  165.                         $(this).find('input[type="checkbox"]:checked').each(function(){
  166.                             chk_value.push($(this).val());
  167.                         });
  168.                     }else{
  169.                         value = $form.find("input[name='productoption" + option_id + "']:checked").val();
  170.                     }
  171.                     break;
  172.             }
  173.             // 配列(checkbox) と、それ以外を分けて処理
  174.             // 配列(checkbox)
  175.             if(Array.isArray(chk_value)){
  176.                 for(var key in chk_value){
  177.                     optionPriceTotal += parseFloat(optionPrice[product_id][option_id][chk_value[key]]);
  178.                     optionPointTotal += parseFloat(optionPoint[product_id][option_id][chk_value[key]]);
  179.                 }
  180.             }
  181.             // それ以外
  182.             if($.isNumeric(value)){
  183.                 optionPriceTotal += parseFloat(optionPrice[product_id][option_id][value]*multi);
  184.                 optionPointTotal += parseFloat(optionPoint[product_id][option_id][value]*multi);
  185.             }
  186.         }
  187.     });
  188.     if(classcat2){
  189.         var product_class_id = classcat2.product_class_id;
  190.     }else{
  191.         var product_class_id = default_class_id[product_id];
  192.     }
  193.     var tax_rate = taxRules[product_class_id]['tax_rate'];
  194.     var tax_rule = taxRules[product_class_id]['tax_rule'];
  195.     var $option_price = $form.parent().find('#option_price_default').first();
  196.     $option_price.text(number_format(optionPriceTotal));
  197.     var $option_point = $form.parent().find('#option_price_inctax_default').first();
  198.     $option_point.text(number_format(optionPriceTotal + sfTax(optionPriceTotal, tax_rate, tax_rule)));
  199. }
  200. function number_format(num) {
  201.     return num.toString().replace(/([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,');
  202. }
  203. function sfTax(price, tax_rate, tax_rule) {
  204.     real_tax = tax_rate / 100;
  205.     ret = price * real_tax;
  206.     tax_rule = parseInt(tax_rule);
  207.     switch (tax_rule) {
  208.         // 四捨五入
  209.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::ROUND') }}:
  210.             $ret = Math.round(ret);
  211.             break;
  212.         // 切り捨て
  213.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::FLOOR') }}:
  214.             $ret = Math.floor(ret);
  215.             break;
  216.         // 切り上げ
  217.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::CEIL') }}:
  218.             $ret = Math.ceil(ret);
  219.             break;
  220.         // デフォルト:切り上げ
  221.         default:
  222.             $ret = Math.round(ret);
  223.             break;
  224.     }
  225.     return $ret;
  226. }
  227. </script>
  228. {% endblock %}