app/Plugin/SheebRental42/Resource/template/Parts/rental_information.twig line 1

Open in your IDE?
  1. {% set firstProductClassId = -1 %}
  2. {% if Product.hasProductClass -%}
  3. {% else %}
  4.     {% set firstProductClassId = Product.ProductClasses[0].id %}
  5. {% endif %}
  6. <div class="rental-info no-{{ count }}" data-count="{{ count }}" style="display:none;">
  7.     <div class="rental-content">
  8.         <p class="rental-header">レンタル情報</p>
  9.         {% if count == 1 %}
  10.             <div id="app_sheeb_rental42"></div>
  11.         {% endif %}
  12.         <dl>
  13.             <div id="basic-charge-term-{{ count }}">
  14.                 <dt>基本レンタル日数<br />
  15.                     <span>ご利用日は後ほどご注文手続き画面で選択いただけます</span>
  16.                 </dt>
  17.                 <dd></dd>
  18.             </div>
  19.             <div id="extension-fee-{{ count }}">
  20.                 <dt>延長料金<br />
  21.                     <span>基本レンタル日数 を超えるレンタルの場合に延長料金が手数料として加算されます</span>
  22.                 </dt>
  23.                 <dd><span class="price"></span><span> 税込(1日毎)</span></dd>
  24.             </div>
  25.         </dl>
  26.     </div>
  27. </div>
  28. {# 一度だけ実行したい処理 #}
  29. {% if count == 1 %}
  30.     <style>
  31.         .rental-info {
  32.             position: relative;
  33.             width: 100%;
  34.             font-size: 1.5rem;
  35.         }
  36.         .rental-header {
  37.             padding: 1rem;
  38.             background: grey;
  39.             color: white;
  40.             font-weight: bold;
  41.             letter-spacing: 0.2rem;
  42.         }
  43.         .rental-content {
  44.             padding: 1.5rem 0.5rem 0.5rem 0.5rem;
  45.         }
  46.         .rental-content dt {
  47.             padding: 0.5rem;
  48.             line-height: 1.2;
  49.         }
  50.         .rental-content dt span {
  51.             font-size: 1.2rem;
  52.             color: #a7acb0;
  53.             font-weight: 500;
  54.         }
  55.         .rental-content dd {
  56.             margin-top: -0.5rem;
  57.             padding-left: 0.5rem;
  58.         }
  59.         .rental-content dd span {
  60.             font-size: 1.2rem;
  61.         }
  62.         .rental-content dd span.price {
  63.             font-size: 1.5rem;
  64.             color: #DE5D50;
  65.         }
  66.     </style>
  67. {% endif %}
  68. {# 毎回実行したい処理 #}
  69. <script>
  70.     $(function() {
  71.         var selfCount = {{ count }};
  72.         var choices = {{ class_categories_as_json(Product)|raw }};
  73.         var rentals = {{ rentals|raw }};
  74.         var first_product_class_id = '{{ firstProductClassId }}';
  75.         var current_rental_settings = null;
  76.         // 読み込み時
  77.         setCurrentRentalSetting(first_product_class_id);
  78.         // 規格1選択時
  79.         $('select[name=classcategory_id1]')
  80.             .change(function() {
  81.                 if (!isSelfEvent(selfCount, this)) return;
  82.                 var $form = $(this).parents('form');
  83.                 onChangeClass($(this), $form.find('select[name=classcategory_id2]'));
  84.             });
  85.         // 規格2選択時
  86.         $('select[name=classcategory_id2]')
  87.             .change(function() {
  88.                 if (!isSelfEvent(selfCount, this)) return;
  89.                 var $form = $(this).parents('form');
  90.                 onChangeClass($form.find('select[name=classcategory_id1]'), $(this));
  91.             });
  92.         // 規格変更共通処理
  93.         function onChangeClass($select1, $select2) {
  94.             clearRentalSetting();
  95.             if (!$select1.length) return;
  96.             // 規格1のみの場合
  97.             if (!$select2.length) {
  98.                 var class1_id = $select1.val();
  99.                 var class2_id = '';
  100.                 setCurrentRentalSetting(
  101.                     getProductClassId(class1_id, class2_id)
  102.                 );
  103.             }
  104.             // 規格2ありの場合
  105.             else {
  106.                 var class1_id = $select1.val();
  107.                 var class2_id = $select2.val();
  108.                 // まだ規格2が選択されていなければ無視
  109.                 if (!class2_id) return;
  110.                 setCurrentRentalSetting(
  111.                     getProductClassId(class1_id, class2_id)
  112.                 );
  113.             }
  114.         }
  115.         // 選択した規格からproductClassIdを取得
  116.         function getProductClassId(class1_id, class2_id) {
  117.             if (!class1_id) return;
  118.             if (!choices[class1_id]) return;
  119.             if (!choices[class1_id]['#' + class2_id]) return;
  120.             if (!choices[class1_id]['#' + class2_id]['product_class_id']) return;
  121.             return choices[class1_id]['#' + class2_id]['product_class_id'];
  122.         }
  123.         // 選択中の規格のレンタル設定をクリア
  124.         function clearRentalSetting() {
  125.             current_rental_settings = null;
  126.             drawRentalSettings(current_rental_settings);
  127.         }
  128.         // 選択中の規格のレンタル設定をセット
  129.         function setCurrentRentalSetting(product_class_id) {
  130.             if (!product_class_id) return;
  131.             if (!rentals[product_class_id]) return;
  132.             current_rental_settings = rentals[product_class_id];
  133.             drawRentalSettings(current_rental_settings);
  134.         }
  135.         // ==============================
  136.         //          描画処理
  137.         // ==============================
  138.         // block count が自分のEventかどうかの判定
  139.         function isSelfEvent(selfCount, selectBoxThis) {
  140.             // 詳細画面なら常にtrue
  141.             if (!isListPage()) {
  142.                 return true;
  143.             }
  144.             $elements = $(selectBoxThis).parents('.ec-shelfGrid__item');
  145.             if ($elements.length === 0) {
  146.                 return false;
  147.             }
  148.             $elements = $($elements[0]).children('.rental-info');
  149.             if ($elements.length === 0) {
  150.                 return false;
  151.             }
  152.             $rental_info = $($($elements[0]));
  153.             return parseInt($rental_info.data('count')) === selfCount;
  154.         }
  155.         // 商品一覧画面かどうか ( falseなら詳細画面 )
  156.         function isListPage() {
  157.             return eccube.hasOwnProperty('productsClassCategories');
  158.         }
  159.         // レンタル設定を元に、設定を描画する
  160.         function drawRentalSettings(current_rental_settings) {
  161.             var $rental_info = $('.rental-info.no-{{ count }}');
  162.             if (!current_rental_settings) {
  163.                 $rental_info.hide();
  164.                 return;
  165.             }
  166.             updateBasicChargeTerm(current_rental_settings);
  167.             updateExtensionFee(current_rental_settings);
  168.             if (!isListPage()) {
  169.                 updateProductStockCalendar(current_rental_settings);
  170.             }
  171.             $rental_info.show();
  172.         }
  173.         function updateBasicChargeTerm(setting) {
  174.             var data = parseInt(setting.basic_charge_term);
  175.             var text = (data - 1) + '泊' + data + '日';
  176.             $('#basic-charge-term-{{ count }} dd').text(text);
  177.         }
  178.         function updateExtensionFee(setting) {
  179.             var target = $('#extension-fee-{{ count }}');
  180.             var data = setting.one_day_charge;
  181.             var text = '¥' + data;
  182.             if (data === 0.0) {
  183.                 target.hide();
  184.             } else {
  185.                 target.show();
  186.             }
  187.             $('#extension-fee-{{ count }} dd .price').text(text);
  188.         }
  189.         function updateProductStockCalendar(setting) {
  190.             currentCalendarSettings = setting.calendar;
  191.             $('#update-calendar').trigger('click');
  192.         }
  193.     });
  194.     //# sourceURL=rental_information_{{ count }}.js
  195. </script>