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. #}
  10. {% block css %}
  11. <style type="text/css">
  12.     .datepicker-days th.dow:first-child,
  13.     .datepicker-days td:first-child {
  14.         color: #f00;
  15.     }
  16.     .datepicker-days th.dow:last-child,
  17.     .datepicker-days td:last-child {
  18.         color: #00f;
  19.     }
  20. </style>
  21. {% endblock %}
  22. {% block javascript %}
  23. <script src="{{ asset('../../plugin/ProductOption42/assets/js/jquery.plainmodal.min.js') }}"></script>
  24. <script>
  25. $(function() {
  26.     //デフォルトのデイトタイムピッカーが存在しない場合
  27.     if ($('[type="date"]').prop('type') !== 'date') {
  28.         $.getScript("{{ asset('assets/js/vendor/moment.min.js', 'admin') }}").done(function() {
  29.             $.getScript("{{ asset('assets/js/vendor/moment-with-locales.min.js', 'admin') }}").done(function() {
  30.                 $.getScript("{{ asset('assets/js/vendor/tempusdominus-bootstrap-4.min.js', 'admin') }}").done(function() {
  31.                     $('input[type=date]').datetimepicker({
  32.                         locale: '{{ eccube_config.locale }}',
  33.                         format: 'YYYY-MM-DD',
  34.                         useCurrent: false,
  35.                         buttons: {
  36.                             showToday: true,
  37.                             showClose: true
  38.                         }
  39.                     });
  40.                 });
  41.             });
  42.         });
  43.     }
  44. });
  45. var product_id;
  46. $(function() {
  47.     {% for ProductOption in ProductOptions %}
  48.     {% set Option = ProductOption.Option %}
  49.     {% set Product = ProductOption.Product %}
  50.         {% if Option.description_flg == constant('Plugin\\ProductOption42\\Entity\\OptionCategory::ON') %}
  51.             modal{{ Product.id }}_{{ Option.id }} = $('#option_description_{{ Product.id }}_{{ Option.id }}').plainModal();
  52.             $('#option_description_link_{{ Product.id }}_{{ Option.id }}').click(function() {
  53.                 modal{{ Product.id }}_{{ Option.id }}.plainModal('open');
  54.                 product_id = $(this).attr('data');
  55.                 return false;
  56.             });
  57.         {% endif %}
  58.     {% endfor %}
  59. });
  60. $(function() {
  61.     $("[id^=desc_btn_]").click(function(){
  62.         var form_id;
  63.         var id = $(this).attr('id').replace(/^desc_btn_/ig, '');
  64.         var ids = id.split('_');
  65.         if (eccube.hasOwnProperty('productsClassCategories')) {
  66.             form_id = product_id;
  67.         }else{
  68.             form_id = 1;
  69.         }
  70.         var $form = $("form[name=form"+form_id+"]");
  71.         func_submit($form,ids[0],ids[1]);
  72.         onOptionChange($form)
  73.     });
  74.     $("[name^=productoption]").change(function(){
  75.         $form = $(this).parents('form');
  76.         onOptionChange($form);
  77.     });
  78. });
  79. function func_submit($form,optionId, setValue) {
  80.     var $sele_option = $form.find("[name=productoption" + optionId + "]");
  81.     if($sele_option && $sele_option.length){
  82.         var kind = $sele_option.attr("type");
  83.         if(kind == 'radio'){
  84.             $sele_option.val([setValue]);
  85.         }else{
  86.             $sele_option.val(setValue);
  87.         }
  88.     }else{
  89.         var $sele_option = $form.find('[name="productoption' + optionId + '[]"]');
  90.         if($sele_option && $sele_option.length){
  91.             $sele_option.each(function(){
  92.                 if($(this).val() == setValue){
  93.                     $(this).prop('checked',true);
  94.                 }
  95.             });
  96.         }
  97.     }
  98.     $('#option_description_' + product_id + '_' + optionId).plainModal('close');
  99. }
  100. var optionPrice = {{ optionPrices|json_encode|raw }};
  101. var optionPoint = {{ optionPoints|json_encode|raw }};
  102. var optionWeight = {{ optionWeights|json_encode|raw }};
  103. var optionSize = {{ optionSizes|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 optionWeightTotal = 0;
  114.     var optionSizeTotal = 0;
  115.     var tax_rate = null;
  116.     var tax_rule = null;
  117.     var product_id = $form.find('input[name="product_id"]').val();
  118.     var $sele1 = $form.find('select[name=classcategory_id1]');
  119.     var $sele2 = $form.find('select[name=classcategory_id2]');
  120.     var classcat_id1 = $sele1.val() ? $sele1.val() : '__unselected';
  121.     var classcat_id2 = $sele2.val() ? $sele2.val() : '';
  122.     if (eccube.hasOwnProperty('productsClassCategories')) {
  123.         // 商品一覧時
  124.         classcat2 = eccube.productsClassCategories[product_id][classcat_id1]['#' + classcat_id2];
  125.     } else {
  126.         // 詳細表示時
  127.         classcat2 = eccube.classCategories[classcat_id1]['#' + classcat_id2];
  128.     }
  129.     $form.find("[id^=productoption]").each(function(){
  130.         var id = $(this).prop("id");
  131.         var name = $(this).attr("name");
  132.         var option_id = null;
  133.         if(name === undefined){
  134.             name = $(this).find("input").prop("id");
  135.         }
  136.         if(name === undefined)return;
  137.         option_id = name.replace(/productoption/ig,'');
  138.         option_id = option_id.replace(/_\d+/ig,'');
  139.         option_id = option_id.replace(/\[\]/ig,'');
  140.         if(id.match(/^productoption\d+$/)){
  141.             var kind = $(this).prop('tagName');
  142.             var type = $(this).attr('type');
  143.             var value = null;
  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.                         value = [];
  165.                         $(this).find('input[type="checkbox"]:checked').each(function(){
  166.                             value.push($(this).val());
  167.                         });
  168.                     }else{
  169.                         value = $form.find("input[name='productoption" + option_id + "']:checked").val();
  170.                     }
  171.                     break;
  172.             }
  173.             if($.isNumeric(value) || Array.isArray(value)){
  174.                 if(Array.isArray(value)){
  175.                     for(var key in value){
  176.                         optionPriceTotal += parseFloat(optionPrice[product_id][option_id][value[key]]);
  177.                         optionPointTotal += parseFloat(optionPoint[product_id][option_id][value[key]]);
  178.                         optionWeightTotal += parseFloat(optionWeight[product_id][option_id][value[key]]);
  179.                         optionSizeTotal += parseFloat(optionSize[product_id][option_id][value[key]]);
  180.                     }
  181.                 }else{
  182.                     optionPriceTotal += parseFloat(optionPrice[product_id][option_id][value]*multi);
  183.                     optionPointTotal += parseFloat(optionPoint[product_id][option_id][value]*multi);
  184.                     optionWeightTotal += parseFloat(optionWeight[product_id][option_id][value]*multi);
  185.                     optionSizeTotal += parseFloat(optionSize[product_id][option_id][value]*multi);
  186.                 }
  187.             }
  188.         }
  189.     });
  190.     var product_class_id = default_class_id[product_id];
  191.     if(classcat2 && classcat2.product_class_id !== ''){
  192.         product_class_id = classcat2.product_class_id;
  193.     }
  194.     var tax_rate = taxRules[product_class_id]['tax_rate'];
  195.     var tax_rule = taxRules[product_class_id]['tax_rule'];
  196.     var $option_price = $form.parent().find('#option_price_default').first();
  197.     $option_price.text(number_format(optionPriceTotal));
  198.     var $option_point = $form.parent().find('#option_price_inctax_default').first();
  199.     $option_point.text(number_format(optionPriceTotal + sfTax(optionPriceTotal, tax_rate, tax_rule)));
  200.     if($form.parent().find('#deliveryplus_weight_default').length){
  201.         var $deliveryplus_weight = $form.parent().find('#deliveryplus_weight_default').first();
  202.         var weight = $deliveryplus_weight.text();
  203.         if($.isNumeric(weight)){
  204.             $deliveryplus_weight.text((parseFloat(weight) + parseFloat(optionWeightTotal)).toFixed(2));
  205.         }
  206.     }
  207.     if($form.parent().find('#deliveryplus_size_default').length){
  208.         var $deliveryplus_size = $form.parent().find('#deliveryplus_size_default').first();
  209.         var size = $deliveryplus_size.text();
  210.         if($.isNumeric(size)){
  211.             $deliveryplus_size.text((parseFloat(size) + parseFloat(optionSizeTotal)).toFixed(2));
  212.         }
  213.     }
  214. }
  215. function number_format(num) {
  216.     return num.toString().replace(/([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,');
  217. }
  218. function sfTax(price, tax_rate, tax_rule) {
  219.     real_tax = tax_rate / 100;
  220.     ret = price * real_tax;
  221.     tax_rule = parseInt(tax_rule);
  222.     switch (tax_rule) {
  223.         // 四捨五入
  224.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::ROUND') }}:
  225.             $ret = Math.round(ret);
  226.             break;
  227.         // 切り捨て
  228.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::FLOOR') }}:
  229.             $ret = Math.floor(ret);
  230.             break;
  231.         // 切り上げ
  232.         case {{ constant('Eccube\\Entity\\Master\\RoundingType::CEIL') }}:
  233.             $ret = Math.ceil(ret);
  234.             break;
  235.         // デフォルト:切り上げ
  236.         default:
  237.             $ret = Math.round(ret);
  238.             break;
  239.     }
  240.     return $ret;
  241. }
  242. </script>
  243. {% endblock %}