• Resolved kminderhout

    (@kminderhout)


    I swear I’m almost done asking questions. Is it possible to safely add to the PayPal.prototype.onClick? I can see a couple of hacky ways to achieve what I need, but the most correct way would be to add my trigger here. I’m not super familiar with the IIFEs, but my understanding is that they aren’t extendable in the normal way a prototype would be, but wanted to ask just in case.

    The problem is the PayPal smart button is an iFrame so I can’t easily setup an onclick listener from the window.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Payment Plugins

    (@mrclayton)

    Hi @kminderhout,

    The PayPal function located in wc-braintree.js does not have an onClick function.

    What are you trying to do with this customization?

    Within the PayPal.prototype.render_options function there is an onClick function. If that’s the one you’re referring to then you could do the following:

    1. Create your own custom script that is enqueued in the header of your WordPress site. 2. Make one of its dependencies the wc-briantree.js file.
    3. Import wc-braintree.js using a local function and redefine the render_options function.

    Here is an example to get you started:

    (function(wc_braintree){
        // save a reference to the prototype.render_options
        var render_options = PayPal.prototype.render_options;
    
        // redefine the render_options function with your custom code
        wc_braintree.PayPal.prototype.render_options = function(){
            var options = render_options.apply(this, arguments);
            var onClick = options.onClick;
            options.onClick = function(data, actions){
                // your custom code here
               
                // call the original onClick function
                onClick.apply(this, arguments);
            }.bind(this)
        }
    })(window.wc_braintree)
    Thread Starter kminderhout

    (@kminderhout)

    Thanks for suggestion and the snippet! I was wanting to trigger an event that I can use to send a GA Enhanced E-commerce hit.

    Am I correct that moving the script to the header wouldn’t be very performative as it would then become a blocking script?

    Plugin Author Payment Plugins

    (@mrclayton)

    @kminderhout It’s not a lot of Javascript, so it’s not going to cause a noticeable performance hit.

    You can put it in the footer if you like, just make sure you make your custom script a dependency for the PayPal script.

    Thread Starter kminderhout

    (@kminderhout)

    True. Thanks again for the help. I’m starting to understand it all, so I’ll give that a try with the dependency.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding to PayPal .onClick’ is closed to new replies.