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