【问题标题】:How to add custom JS code on review-order page in woocommerce如何在 woocommerce 的评论订单页面上添加自定义 JS 代码
【发布时间】:2021-04-23 23:01:27
【问题描述】:

这是上一个与该主题相关的问题:Stripe JS element disappering in WooCommerce Checkout

所以在我玩了一点之后,我以某种方式设法获得了这样的字段以及它的验证。

但问题是我在这个文件中插入了 JS 代码:wp-content/plugins/woocommerce/templates/checkout/review-order.php

看起来是这样的:

这是添加的 JS 代码:

<script>
    (function ($) {
    "use strict";
    $(document).ready(function () {
        // Create a Stripe client.
        var stripe = Stripe('pk_test_51IB3etJxogSn2Fj0hqBSBcCkGH76cUowa9iWK7Xm7gj3O2jdt8FfcIXrHoZGIk4ySL1MyYYp1IxN582FOsjgI1DD00Hr7nvVF1');

// Create an instance of Elements.
        var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
        var style = {
            base: {
                color: '#32325d',
                fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
                fontSmoothing: 'antialiased',
                fontSize: '16px',
                '::placeholder': {
                    color: '#aab7c4'
                }
            },
            invalid: {
                color: '#fa755a',
                iconColor: '#fa755a'
            }
        };

// Create an instance of the card Element.
        var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-number` <div>.
        card.mount('#card-number');
// Handle real-time validation errors from the card Element.
        card.on('change', function (event) {
            var displayError = document.getElementById('card-errors');
            if (event.error) {
                displayError.textContent = event.error.message;
            } else {
                displayError.textContent = '';
            }
        });

// Handle form submission.
// var form = document.getElementById('payment-form');
// form.addEventListener('submit', function(event) {
//   event.preventDefault();
//
//   stripe.createToken(card).then(function(result) {
//     if (result.error) {
//       // Inform the user if there was an error.
//       var errorElement = document.getElementById('card-errors');
//       errorElement.textContent = result.error.message;
//     } else {
//       // Send the token to your server.
//       stripeTokenHandler(result.token);
//     }
//   });
// });

// Submit the form with the token ID.
// function stripeTokenHandler(token) {
//   // Insert the token ID into the form so it gets submitted to the server
//   var form = document.getElementById('payment-form');
//   var hiddenInput = document.createElement('input');
//   hiddenInput.setAttribute('type', 'hidden');
//   hiddenInput.setAttribute('name', 'stripeToken');
//   hiddenInput.setAttribute('value', token.id);
//   form.appendChild(hiddenInput);
//
//   // Submit the form
//   form.submit();
// }
    });
})(this.jQuery);



</script>

所以我的问题是我怎样才能正确地对这个 JS 代码进行优先级排序/排队,以便它可以在我的插件中工作,而不仅仅是在 woocommerce 模板中复制和粘贴代码。

这就是我将脚本排队的方式:

add_action('wp_enqueue_scripts', array(self::class, 'enqueue'), 100, 1);

 // add js in wordpress frontend
    public static function enqueue()
    {
        wp_enqueue_script('woocommerce_pay_later_stripe_script', 'https://js.stripe.com/v3/', array('jquery'), false, true);
        wp_enqueue_script('woocommerce_pay_later_main_script', plugins_url('/assets/main.js', __DIR__), array('jquery'), false, true);
    }

【问题讨论】:

  • 为你的 sn-p 创建一个插件
  • @mehmoodkhan 我正在创建一个插件来创建一个自定义支付网关。 I have achieved the creation of payment gateway but i want strip js elements when that payment gateway is selected or shown.问题在于管理 JS 我已将脚本放在 payment_fields() 方法中并且它可以工作,但我想知道正确的做法,而不是只是把代码放在这里和那里。

标签: php jquery wordpress woocommerce


【解决方案1】:

您可以将脚本排入该特定页面!

首先找到你review-order page的id,然后检查你是否在那个页面上。如果是,请加载您的脚本!

以下代码将转到您的活动主题或活动子主题的functions.php。

function loading_your_custom_js(){

$order_review_page_id = ; #assign the id of your page to the variable here - You could find that id in the admin area under pages section

# check whether you're on the review-order page or not

  if(get_the_ID() == $order_review_page_id){
    wp_enqueue_script('checkout_js', 'your_custom_script.js', 'JQuery', '1.0', TRUE);
  };
};

add_action('wp_enqueue_scripts', 'loading_your_custom_js');

【讨论】:

  • 我不想把它放到functions.php中,我希望一切都通过我的自定义插件进行管理。另外,如果我在 $order_review_page_id 变量中对 pageID 进行硬编码,并且如果它不符合条件,则该条件将不起作用。
  • 你绝对可以在你的插件文件中使用相同的逻辑,当你激活你的插件时,它会被包含在你的functions.php文件中。这很容易做到。只需检查您是否在该页面上,然后将脚本加载到该页面中。对于第二个问题,是的,没错,如果不满足条件,即你不在review-order页面并且代码不会加载,那不是你想要的吗?
  • 不起作用。管理部分没有订单审查页面,但结帐页面。 prnt.sc/xarvwj 它的页面 id 是 8,我把你的代码放在激活方法中。去并重新激活插件并转到结帐页面。什么都没发生。 :-/
  • 我明白了!好吧,我现在唯一能想到的就是你的 JavaScript 代码。我看到你正在使用 jquery $ 对吗?你曾经在 wordpress 环境中使用过 jquery 吗?在 wordpress 中加载和使用 jquery 的方法与普通页面略有不同...
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多