app/Plugin/CustomPrice/Resource/template/Product/detail.twig line 1

Open in your IDE?
  1. <script>
  2.     $(function () {
  3.         if ($("#custom_chamfer").length) {
  4.            
  5.             if ($("#custom_chamfer option:nth-child(2)").length) {
  6.                 $("#custom_chamfer option:nth-child(2)").prop('selected', true).trigger('change');
  7.             }      
  8.         }
  9.         
  10.     $('#custom_width, #custom_height').on('input', function () {
  11.         let frameWidth = $('.frame_block_wrap').width(); // 親要素の幅
  12.         let inputWidth = parseInt($('#custom_width').val()) || 0;
  13.         let inputHeight = parseInt($('#custom_height').val()) || 0;
  14.         let borderWidth = {{Product.minlength}};
  15.         let maxHeight = 350; // 最大高さを500pxとする
  16.         if (inputWidth > 0 && inputHeight > 0 && borderWidth >= 0) {
  17.             let gcd = function (a, b) {
  18.                 return b == 0 ? a : gcd(b, a % b);
  19.             };
  20.             let greatestCommonDivisor = gcd(inputWidth, inputHeight);
  21.             let aspectWidth = inputWidth / greatestCommonDivisor;
  22.             let aspectHeight = inputHeight / greatestCommonDivisor;
  23.             let heightBasedOnWidth = frameWidth * (aspectHeight / aspectWidth);
  24.              // 高さが最大高さを超える場合は縮小する
  25.              if (heightBasedOnWidth > maxHeight) {
  26.                 heightBasedOnWidth = maxHeight; // 高さを最大高さに設定
  27.                 frameWidth = maxHeight * (aspectWidth / aspectHeight); // 幅もアスペクト比に基づいて再計算
  28.             }
  29.             let borderRatioWidth = borderWidth / greatestCommonDivisor;
  30.             let borderBasedOnWidth = frameWidth * (borderRatioWidth / aspectWidth);
  31.             $('#frame_block').css({
  32.                 width: frameWidth + 'px', // 幅を100%に設定
  33.                 height: heightBasedOnWidth + 'px', // 高さをアスペクト比に基づいて設定
  34.                 "border-width": borderBasedOnWidth
  35.             });
  36.         } 
  37.     });
  38. });
  39. </script>
  40. <script>
  41.     $(function () {
  42.         const PRODUCT_PRICE = $("#add_price");
  43.         function numberFormat(num = 0) {
  44.             return num.toString().replace(/(\d+?)(?=(?:\d{3})+$)/g, x => {
  45.                 return x + ','
  46.             })
  47.         }
  48.         $('#form1').on('change keyup', 'input, select', function () {
  49.             // ここでフォームのデータを取得
  50.             var formData = $('#form1').serialize(); // フォームのデータをURLエンコードされた文字列で取得
  51.             // AJAXリクエストを送信
  52.             $.ajax({
  53.                 url: '/api/custom_price/product/{{Product.id}}', // サーバーエンドポイントURL
  54.                 type: 'GET', // メソッドタイプ
  55.                 dataType: 'json', // 期待するレスポンスの形式
  56.                 data: formData, // フォームデータ
  57.                 success: function (response) {
  58.                     //let formattedPrice = numberFormat(response.price);
  59.                     
  60.                     // PRODUCT_PRICE.html(`ご購入価格:<span class="txt_pink">${formattedPrice}円<span class="unit">(税込)</span></span>`);
  61.                     PRODUCT_PRICE.html(`
  62.                     <div class="price_s">
  63.                     本体価格:<span class="txt_pink txt_pink_s">${response.price}円<span class="unit unit_s">(税込)</span></span><br>
  64.                     面取り加工価格:<span class="txt_pink txt_pink_s">${response.chamfer_price}円<span class="unit unit_s">(税込)</span></span><br>
  65.                     取付金具価格:<span class="txt_pink txt_pink_s">${response.mounting_price}円<span class="unit unit_s">(税込)</span></span>
  66.                     </div>
  67.                     合計価格:<span class="txt_pink">${response.main_price}円<span class="unit">(税込)</span></span>
  68.                     `);
  69.                     $("#error-message").remove();
  70.                 },
  71.                 error: function (xhr, status, error) {
  72.                     // エラー処理
  73.                     let response = JSON.parse(xhr.responseText);
  74.                     $("#error-message").remove();
  75.                     if (response?.error) {
  76.                         // エラーメッセージを挿入
  77.                         PRODUCT_PRICE.html("");
  78.                         PRODUCT_PRICE.after(`<div id="error-message" style="color: red; font-size:13px;">${response.error}</div>`);
  79.                     }
  80.                 }
  81.             });
  82.         });
  83.     });
  84. </script>
  85. <style>
  86.     *{
  87.         box-sizing: border-box;
  88.     }
  89.     .frame_block_ttl{
  90.         color: #804040;
  91.         text-align: center;
  92.         margin-top: 40px;
  93.     }
  94.     .frame_block_wrap {
  95.         width: 90%;
  96.         margin: 0 auto;
  97.         margin-top: 20px;
  98.         overflow: hidden;
  99.         margin-bottom: 15px;
  100.         text-align: center;
  101.     }
  102.     #frame_block {
  103.         height: 200px;
  104.         border: 5px solid #804040;
  105.         margin: 0 auto;
  106.     }
  107.     .price_s{
  108.         font-size: 14px;
  109.     }
  110.     .item_detail .item_detail_info .goods_info span.txt_pink_s{
  111.         font-size: 17px;
  112.     }
  113.     .item_detail .item_detail_info .goods_info .unit.unit_s{
  114.         font-size: 14px;
  115.     }
  116. </style>