app/template/default/Block/onetouch_javascript_function.twig line 1

Open in your IDE?
  1. <script>
  2.     document.addEventListener('DOMContentLoaded', function() {
  3.         function parsePrice(price) {
  4.                 return parseInt(price.replace(',', ''))
  5.             }
  6.             {% if Product is defined %}
  7.                 $('.open-one-touch').on('click', function(event){
  8.                     const customer = "{{ Customer }}";
  9.                     const postal_code = "{{ Customer.postal_code ?? '' }}"
  10.                     if(!customer){
  11.                         window.location.replace("{{ url('shopping_login') }}")
  12.                         $('.background_popup').css('display','none');
  13.                     }
  14.                     else if(postal_code == "0000000"){      
  15.                             let product_id = $('#product_id_change').val();              
  16.                             window.location.replace(`{{ url('mypage_change') }}?product=${product_id}`)
  17.                             $('.background_popup').css('display','none');
  18.                             console.log("pref is null");
  19.                     }else{
  20.                         const formatPrice = Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' });
  21.                         loadingPayment();
  22.                         $('.content').text('注文確認中');
  23.                         $form = $('#form1');
  24.                         var stripe_payment_method_id = "{{ PaymentDefault ? PaymentDefault.id : null }}"
  25.                         var stripe_customer_id = "{{ StripeCustomer ? StripeCustomer.getStripeCustomerId : null }}"
  26.                         let paymentData = $form.serialize()
  27.                         /// hotfix 20240122 quantity
  28.                         
  29.                         var newQuantity = document.getElementById("quantity").value;
  30.                         var params = paymentData.split("&");
  31.                         for (var i = 0; i < params.length; i++) {
  32.                             if (params[i].startsWith("quantity=")) {
  33.                                 params[i] = "quantity=" + newQuantity;
  34.                             }
  35.                         }
  36.                         paymentData = params.join("&");
  37.                         let isloading = false
  38.                         paymentData += `&is_one_touch_payment=1`
  39.                         $.ajax({
  40.                             url: $('.open-one-touch').attr('data-url'),
  41.                             type: $form.attr('method'),
  42.                             data: paymentData,
  43.                             success: function (data) {
  44.                                 if (data.done) {
  45.                                     let order = data.Order
  46.                                     let ordertId = order.order_id;
  47.                                     let shipping = order.shipping;
  48.                                     $('.modal_onetouch_payment').attr("data-form",'1');
  49.                                     $('#ec-price__price').text(formatPrice.format(order.price));
  50.                                     $('#ec-productTitle').text(order.product_name);
  51.                                     $('.product-image-order').attr('src',order.main_image);
  52.                                     if (order.discount != 0) {
  53.                                         $('.has-discount').show();
  54.                                         $('#ec-price__price').text(formatPrice.format(order.price - (order.price * order.discount_rate)/100));
  55.                                         $('#discount_rate').text("初回 " + order.discount_rate + " %OFF");
  56.                                         $('#price').text(formatPrice.format(order.price));
  57.                                         $('#ec-price__tax-point').text("(Pt" + order.pt + ")");
  58.                                     }else{
  59.                                         $('.has-discount').hide();
  60.                                     }
  61.                                     $('#onetouch_discount').text(formatPrice.format( 880 + parsePrice(order.pt_family_benefits) ));
  62.                                     $('#bg_detail_discount').text(formatPrice.format( 880 + parsePrice(order.pt_family_benefits) ));
  63.                                     
  64.                                     $('#pt_normal').text(parseInt(order.pt) );
  65.                                     $('#pt_family_benefits').text(parseInt(order.pt) + parsePrice(order.pt_family_benefits) );
  66.                                     $('#bg_detail_bonus').text(parsePrice(order.pt_family_benefits));
  67.                                     $('#bg_detail_bonus_y').text(formatPrice.format( parsePrice(order.pt_family_benefits) ));
  68.                                     $('.normal-bonus-value').text(order.pt);
  69.                                     $('.kg-bonus-value').text(order.pt_family_benefits);
  70.                                     let full_price = order.subtotal - order.discount;
  71.                                     $('#productRole_totalPayment').text(formatPrice.format(order.payment_total));
  72.                                     $('#productRole_totalPayment').attr("total",order.payment_total);
  73.                                     $('#productRole_totalPayment').attr("delivery",order.delivery_fee_total);
  74.                                     $('#full_total').text(formatPrice.format(full_price));
  75.                                     $('#delivery_total').text(formatPrice.format(order.delivery_fee_total));
  76.                                     $('#payment_total').text(formatPrice.format(order.payment_total));
  77.                                     $('#add_pt').text(order.add_point);
  78.                                     /// hotfix 20240122 quantity
  79.                                     if(order.sale_type)
  80.                                         $('.promo_code_box').css("display","none")
  81.                                     else
  82.                                         $('.promo_code_box').css("display","block")
  83.                                     {% if Product.regular_product_id == 522 or Product.regular_product_id == 814 %}
  84.                                         $('.no-member-info').css("display", "none");
  85.                                     {% endif %}
  86.                                     $('#productRole_quantity').text(order.quantity);
  87.                                     if(shipping.name01 != 'ゲスト'){
  88.                                         $('.ec-addressCustomer').text(shipping.name01+shipping.name02+' ('+shipping.kana01+shipping.kana02+') 様'+' 〒'+shipping.postal_code+ ' ' + shipping.pref +shipping.addr01+shipping.addr02+' '+shipping.phone_number);
  89.                                         $('.show_update_shipping').attr("data-id", shipping.id)
  90.                                     }else{
  91.                                         $('.ec-addressCustomer').text("固定お届け先がありません。")
  92.                                     }
  93.                                     $('#order_id').val(ordertId);
  94.                                     $('#order_id').attr("data-regular",'0');
  95.                                     showModal();
  96.                                     closeLoading();
  97.                                     closePopup()
  98.                                     isloading = false
  99.                                 } else {
  100.                                     closePopup()
  101.                                     showError();
  102.                                     window.location.replace("{{ url('shopping_error') }}")
  103.                                 }
  104.                                 
  105.                             },
  106.                             error: function () {
  107.                                 showError();
  108.                                 window.location.replace("{{ url('shopping_error') }}")
  109.                             }
  110.                         })
  111.                         $('.background_popup').css('display','block');
  112.                         /// hotfix 20240122 quantity
  113.                         let quantity = newQuantity;
  114.                     }
  115.                         
  116.                 })
  117.                 const regular_product_id = "{{ Product.regular_product_id ?? 'null' }}";
  118.                 const regular_variant = "{{ Product.is_variant ? 1 : 0}}";
  119.                 if (regular_product_id != 'null' || regular_variant == 1) {
  120.                     $('.open-one-touch-related-product').on('click', function(event){
  121.                     const customer = "{{ Customer }}";
  122.                     const postal_code = "{{ Customer.postal_code ?? '' }}"
  123.                     if(!customer){
  124.                         window.location.replace("{{ url('shopping_login') }}")
  125.                         $('.background_popup').css('display','none');
  126.                     }else if(postal_code == '0000000'){
  127.                         let regular_product_id = this.getAttribute('data-id');     
  128.                         let product_id = $('#product_id_change').val();             
  129.                         window.location.replace(`{{ url('mypage_change') }}?product=${product_id}&regular_product_id=${regular_product_id}`)
  130.                         $('.background_popup').css('display','none');
  131.                         console.log("pref is null");
  132.                     }
  133.                     else{
  134.                         const formatPrice = Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' });
  135.                         loadingPayment();
  136.                         $('.content').text('注文確認中');
  137.                         $form = $('#form2');
  138.                         var stripe_payment_method_id = "{{ PaymentDefault ? PaymentDefault.id : null }}"
  139.                         var stripe_customer_id = "{{ StripeCustomer ? StripeCustomer.getStripeCustomerId : null }}"
  140.                         if($('#product_relate_id')){
  141.                             let product_id = $('#product_relate_id').val()
  142.                         }
  143.                         let formToString = $form.serialize()
  144.                         let paymentId = $(this).attr('data-id');
  145.                         let urlForm;
  146.                         let newQuantity;
  147.                         var params = formToString.split("&");   
  148.                         var paymentData;    
  149.                         if(regular_variant == 1){
  150.                             const url =  '{{ url('one_touch_payment', {id:Product.regular_product_id ?? 1}) }}';
  151.                             const newUrl = url.split('/', 5).join('/') + '/' + paymentId;
  152.                             urlForm = newUrl;
  153.                             newQuantity = document.querySelector(".variant_regular_"+ paymentId +" #regular-quantity").value;
  154.                             params = params.filter(param => !param.startsWith("quantity=") && !param.startsWith("direct_shopping="));
  155.                             paymentData = params.join("&");
  156.                             paymentData += `&quantity=`+ newQuantity;
  157.                         }else{
  158.                             urlForm = '{{ url('one_touch_payment', {id:Product.regular_product_id ?? 1}) }}'
  159.                             newQuantity = document.getElementById("{{ Product.regular_product_id ? 'regular-quantity' : 'quantity' }}").value;
  160.                             for (var i = 0; i < params.length; i++) {
  161.                                 if (params[i].startsWith("quantity=")) {
  162.                                     params[i] = "quantity=" + newQuantity;
  163.                                     break;
  164.                                 }
  165.                             }
  166.                             paymentData = params.join("&");
  167.                         }
  168.                         /// hotfix 20240122 quantity
  169.                         let isloading = false
  170.                         
  171.                         paymentData += `&is_one_touch_payment=1&direct_shopping=1`;
  172.                         
  173.                         $.ajax({
  174.                             url: urlForm,
  175.                             type: $form.attr('method'),
  176.                             data: paymentData,
  177.                             success: function (data) {
  178.                                 if (data.done) {
  179.                                     $('.modal_onetouch_payment').attr("data-form",'2');
  180.                                     let order = data.Order;
  181.                                     let ordertId = order.order_id;
  182.                                     let shipping = order.shipping;
  183.                                     $('#ec-price__price').text(formatPrice.format(order.price));
  184.                                     $('#ec-productTitle').text(order.product_name);
  185.                                     $('.product-image-order').attr('src',order.main_image);
  186.                                     if (order.discount != 0) {
  187.                                         $('#ec-price__price').text(formatPrice.format(order.price - (order.price * order.discount_rate)/100));
  188.                                         $('.has-discount').show();
  189.                                         $('#discount_rate').text("初回 " + order.discount_rate + " %OFF");
  190.                                         $('#price').text(formatPrice.format(order.price));
  191.                                         $('#ec-price__tax-point').text("(Pt" + order.pt + ")");
  192.                                     } else {
  193.                                         $('.has-discount').hide();
  194.                                     }
  195.                                 
  196.                                     $('#onetouch_discount').text(formatPrice.format( 880 + parsePrice(order.pt_family_benefits) ));
  197.                                     $('#bg_detail_discount').text(formatPrice.format( 880 + parsePrice(order.pt_family_benefits) ));
  198.                                     $('#pt_normal').text(parseInt(order.pt) );
  199.                                     $('#pt_family_benefits').text(parseInt(order.pt) + parsePrice(order.pt_family_benefits) );
  200.                                     $('#bg_detail_bonus').text(parsePrice(order.pt_family_benefits) );
  201.                                     $('#bg_detail_bonus_y').text(formatPrice.format( parsePrice(order.pt_family_benefits) ));
  202.                                     $('.normal-bonus-value').text(order.pt);
  203.                                     $('.kg-bonus-value').text(order.pt_family_benefits);
  204.                                     $('#productRole_totalPayment').text(formatPrice.format(order.payment_total));
  205.                                     $('#productRole_totalPayment').attr("total",order.payment_total);
  206.                                     $('#productRole_totalPayment').attr("delivery",order.delivery_fee_total);
  207.                                     let full_price = order.subtotal - order.discount;
  208.                                     $('#full_total').text(formatPrice.format(full_price));
  209.                                     $('#delivery_total').text(formatPrice.format(order.delivery_fee_total));
  210.                                     $('#payment_total').text(formatPrice.format(order.payment_total));
  211.                                     $('#add_pt').text(order.add_point);
  212.                                     $('#productRole_quantity').text(order.quantity);
  213.                                     if(order.sale_type)
  214.                                         $('.promo_code_box').css("display","none")
  215.                                     else
  216.                                         $('.promo_code_box').css("display","block")
  217.                                     {% if Product.regular_product_id == 522 or Product.regular_product_id == 814 %}
  218.                                         $('.no-member-info').css("display", "none");
  219.                                     {% endif %}         
  220.                                     if(shipping.name01 != 'ゲスト'){
  221.                                         $('.ec-addressCustomer').text(shipping.name01+shipping.name02+' ('+shipping.kana01+shipping.kana02+') 様'+' 〒'+shipping.postal_code+ ' ' +shipping.addr01+shipping.addr01+' '+shipping.phone_number);
  222.                                         $('.show_update_shipping').attr("data-id", shipping.id)
  223.                                     }else{
  224.                                         $('.ec-addressCustomer').text("固定お届け先がありません。")
  225.                                     }
  226.                                     $('#order_id').val(ordertId);
  227.                                     $('#order_id').attr("data-regular",'1');
  228.                                     showModal();
  229.                                     closeLoading();
  230.                                     closePopup()
  231.                                     isloading = false
  232.                                 } else {
  233.                                     closePopup()
  234.                                     showError();
  235.                                     window.location.replace("{{ url('shopping_error') }}")
  236.                                 }
  237.                             },
  238.                             error: function () {
  239.                                 showError();
  240.                                 window.location.replace("{{ url('shopping_error') }}")
  241.                             }
  242.                         })
  243.                         $('.background_popup').css('display','block');
  244.                         let quantity = document.getElementById("quantity").value;
  245.                         } 
  246.                     })
  247.                 }
  248.                 
  249.             {% endif %}
  250.             
  251.             
  252.             $('.open-one-touch-ambassador-yearly').on('click', function(event){
  253.                 const customer = "{{ Customer }}";
  254.                 const postal_code = "{{ Customer.postal_code ?? '' }}"
  255.                 if(!customer){
  256.                     window.location.replace("{{ url('shopping_login') }}")
  257.                     $('.background_popup').css('display','none');
  258.                 }
  259.                 else if(postal_code == "0000000"){                    
  260.                         let product_id = $('#product_id_change').val();              
  261.                         window.location.replace(`{{ url('mypage_change') }}?product=${product_id}`)
  262.                         $('.background_popup').css('display','none');
  263.                         console.log("pref is null");
  264.                 }else{
  265.                     const formatPrice = Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' });
  266.                     loadingPayment();
  267.                     $('.content').text('注文確認中');
  268.                     $form = $('#formAmabssador');
  269.                     var stripe_payment_method_id = "{{ PaymentDefault ? PaymentDefault.id : null }}"
  270.                     var stripe_customer_id = "{{ StripeCustomer ? StripeCustomer.getStripeCustomerId : null }}"
  271.                     let paymentData = $form.serialize()
  272.                     let isloading = false
  273.                     paymentData += `&is_one_touch_payment=1`
  274.                     
  275.                     $.ajax({
  276.                         url: $('.open-one-touch-ambassador-yearly').attr('data-url'),
  277.                         type: $form.attr('method'),
  278.                         data: paymentData,
  279.                         success: function (data) {
  280.                             if (data.done) {
  281.                                 let order = data.Order
  282.                                 let ordertId = order.order_id;
  283.                                 let shipping = order.shipping;
  284.                                 $('.modal_onetouch_payment').attr("data-form",'1');
  285.                                 $('#ec-price__price').text(formatPrice.format(order.price));
  286.                                 $('#ec-productTitle').text(order.product_name);
  287.                                 $('.product-image-order').attr('src',order.main_image);
  288.                                 if (order.discount != 0) {
  289.                                     $('.has-discount').show();
  290.                                     $('#ec-price__price').text(formatPrice.format(order.price - (order.price * order.discount_rate)/100));
  291.                                     $('#discount_rate').text("初回 " + order.discount_rate + " %OFF");
  292.                                     $('#price').text(formatPrice.format(order.price));
  293.                                     $('#ec-price__tax-point').text("(Pt" + order.pt + ")");
  294.                                 }else{
  295.                                     $('.has-discount').hide();
  296.                                 }
  297.                                 $('#onetouch_discount').text(formatPrice.format( 880 + parsePrice(order.pt_family_benefits) ));
  298.                                 $('#bg_detail_discount').text(formatPrice.format( 880 + parsePrice(order.pt_family_benefits) ));
  299.                                 
  300.                                 $('#pt_normal').text(parseInt(order.pt) );
  301.                                 $('#pt_family_benefits').text(parseInt(order.pt) + parsePrice(order.pt_family_benefits) );
  302.                                 $('#bg_detail_bonus').text(parsePrice(order.pt_family_benefits));
  303.                                 $('#bg_detail_bonus_y').text(formatPrice.format( parsePrice(order.pt_family_benefits) ));
  304.                                 $('.normal-bonus-value').text(order.pt);
  305.                                 $('.kg-bonus-value').text(order.pt_family_benefits);
  306.                                 let full_price = order.subtotal - order.discount;
  307.                                 $('#productRole_totalPayment').text(formatPrice.format(order.payment_total));
  308.                                 $('#productRole_totalPayment').attr("total",order.payment_total);
  309.                                 $('#productRole_totalPayment').attr("delivery",order.delivery_fee_total);
  310.                                 $('#full_total').text(formatPrice.format(full_price));
  311.                                 $('#delivery_total').text(formatPrice.format(order.delivery_fee_total));
  312.                                 $('#payment_total').text(formatPrice.format(order.payment_total));
  313.                                 $('#add_pt').text(order.add_point);
  314.                                 $('#productRole_quantity').text(order.quantity);
  315.                                 /// hotfix 20240122 quantity
  316.                                 if(order.sale_type)
  317.                                     $('.promo_code_box').css("display","none")
  318.                                 else
  319.                                     $('.promo_code_box').css("display","block")
  320.                                 if(shipping.name01 != 'ゲスト'){
  321.                                     $('.ec-addressCustomer').text(shipping.name01+shipping.name02+' ('+shipping.kana01+shipping.kana02+') 様'+' 〒'+shipping.postal_code+ ' ' + shipping.pref +shipping.addr01+shipping.addr02+' '+shipping.phone_number);
  322.                                     $('.show_update_shipping').attr("data-id", shipping.id)
  323.                                 }else{
  324.                                     $('.ec-addressCustomer').text("固定お届け先がありません。")
  325.                                 }
  326.                                 $('#order_id').val(ordertId);
  327.                                 $('#order_id').attr("data-regular",'0');
  328.                                 showModal();
  329.                                 closeLoading();
  330.                                 closePopup()
  331.                                 isloading = false
  332.                             } else {
  333.                                 closePopup()
  334.                                 showError();
  335.                                 window.location.replace("{{ url('shopping_error') }}")
  336.                             }
  337.                                
  338.                         },
  339.                         error: function () {
  340.                             showError();
  341.                             window.location.replace("{{ url('shopping_error') }}")
  342.                         }
  343.                     })
  344.                     $('.background_popup').css('display','block');
  345.                 }
  346.                     
  347.             })
  348.             $('.ec-icon').click(function(){
  349.                 $('.update_shipping').addClass("hidden");
  350.                 $('.modal_onetouch_payment').css('display','none');
  351.                 $('.background_popup').css('display','none');
  352.                 $('.modal_loading_payment').css('display','none');
  353.                 $('.coundown-dialog-create-shipping').css('display','none')
  354.                 $('.update_shipping').addClass("hidden");
  355.                 $('.update_card').addClass("hidden");
  356.                 $('.modal-order').css('display','block');
  357.                 $('.coundown-dialog-create-card').css('display','none')
  358.                 $('.coundown-dialog-update-card').css('display','none')
  359.                 $('.modal-order').css('top','20%');
  360.                 $('.modal_kg_family').addClass("hidden");
  361.                 $('.modal_payment_kg').addClass("hidden");
  362.             });
  363.             function closePopup(){
  364.                 $('.background_popup').click(function(){
  365.                     if(isloading==false){
  366.                         $('.update_shipping').addClass("hidden");
  367.                         $('.modal_onetouch_payment').css('display','none');
  368.                         $('.background_popup').css('display','none');
  369.                         $('.modal_loading_payment').css('display','none');
  370.                         $('.modal-order').css('top','20%');
  371.                         $('.modal_kg_family').addClass("hidden");
  372.                         $('.modal_payment_kg').addClass("hidden");
  373.                     }
  374.                     }
  375.                 );
  376.             }
  377.             function showSuccess(){
  378.                 $('.modal_loading_payment').css('display','block');
  379.                 $('.loading').css('display','none');
  380.                 $('.complete').css('display','block');
  381.                 $('.loading').css('display','none');
  382.                 $('.content').text('決済は完了しました。');
  383.             }
  384.             function showError(){
  385.                 $('.modal_loading_payment').css('display','block');
  386.                 $('.loading').css('display','none');
  387.                 $('.error').css('display','block');
  388.                 $('.complete').css('display','none');
  389.                 $('.content').text('決済が失敗しました。');
  390.                 isloading = true;
  391.             }
  392.             function loadingPayment(){
  393.                     $('.modal_loading_payment').css('display','block');
  394.                     $('.loading').css('display','block');
  395.                     $('.error').css('display','none');
  396.                     $('.complete').css('display','none');
  397.                     $('.content').text('決済中');
  398.             }
  399.             function closeLoading(){
  400.                 $('.loading').css('display','none');
  401.                 $('.error').css('display','none');
  402.                 $('.complete').css('display','none');
  403.             }
  404.             function showModal(){
  405.                 $('.modal_onetouch_payment').css('display','block');
  406.                 $('.modal_loading_payment').css('display','none');
  407.                 $('.update_shipping').addClass("hidden");
  408.                 $('.modal-order').css('display','block');
  409.                 $('.modal-order').css('top','20%');
  410.                 $('.modal_kg_family').addClass("hidden");
  411.                 $('.modal_payment_kg').addClass("hidden");
  412.             }
  413.     });
  414. </script>