woocommerce / woocommerce-gateway-paypal-express-checkout

58 stars 65 forks source link

Remove Unused Java Script. Paypal & Paypalobjects #859

Closed brag-man closed 3 years ago

brag-man commented 3 years ago

Hi, I have been referred to this forum as I am having difficulty getting an answer to a common issue. I am using current version of Wordpress, with latest versions of Woocommerce and Paypal Gateway. In an effort to improve website speed and meet 'Core Web Vitals' demands, I am trying to address some major issues presented by 'Page Speed Insights'.

To reproduce Regardless of the website, if you use Wordpress, Woocommerce and WooCommerce PayPal Checkout Payment Gateway, the result in 'Page Speed Insights' is the same.

**Remove Unused Java Script

/sdk/js?client-id=…(www.paypal.com) | 129.2 KiB | 76.7 KiB

…analytics/index.html?frameId=…(www.paypalobjects.com) | 90.6 KiB | 39.2 KiB**

Expected behavior

While I would expect to this Java Script within the Woocommerce Cart and Check Out pages, maybe even the Product pages too, I do not expect to see this script appearing anywhere else. Especially the Home Page.

I have read many threads about Deregister & Dequeue Stylesheets and Scripts, but to be honest, this means nothing to me. I am not a coder, but will have go if it is explained clearly. Sadly all the threads I found, assume you are a professional coder. So having tried to follow these threads and getting nowhere, here I am.

What I am trying to do is stop the Paypal Scripts from loading on pages / posts which are not Woocommerce pages. This I assume will improve my Page Speed results.

I have similar issues with the Zip Money script as well as and Recaptcha, but figure once I know how to remove the unused Paypal script, I can apply this new found knowledge to the others.

Environment :

So in short, I am looking for a means to prevent these scripts loading, except for on the Woocommerce pages.

Many thanks in advance.

brag-man commented 3 years ago

I think that I have found how to fix this issue, thanks to a very kind gentleman on the Wordpress support forum.

add_action('wp_enqueue_scripts', 'disable_woocommerce_loading_css_js', 99 );

function disable_woocommerce_loading_css_js() {
        if(! is_cart() && ! is_checkout() ) {
                wp_dequeue_style('wc-gateway-ppec-frontend');
                wp_deregister_script('paypal-checkout-sdk');
        }
}

This seems to removed the Paypal script from the Home page etc.

My next question to anyone reading this who cares, is how do I apply this same code to Zip Money?

I tried this and it hasn't worked;

add_action('wp_enqueue_scripts', 'disable_woocommerce_loading_css_js', 99 );

function disable_woocommerce_loading_css_js() {
        if(! is_cart() && ! is_checkout() ) {
                wp_dequeue_style('wc-zipmoney-payment-frontend');
                wp_deregister_script('zipmoney-checkout-sdk');
        }
}

Thanks

jorgeatorres commented 3 years ago

Hi @brag-man!

The snippet you have for the PayPal Checkout should work correctly in most cases. That said, you should be aware that the cart and the checkout pages are not the only places where a PayPal button might be needed (and thus the relevant scripts and styles). For instance, if you're using the Storefront theme, you might have the "mini cart" feature enabled, which includes a PayPal button and is available everywhere (not just on WooCommerce pages):

Screen Shot 2021-05-25 at 16 24 18

There are other contexts where something similar happens (single product pages, if you have the related setting enabled). That's not to say there is nothing to optimize, but it would have to be done very carefully. We'll take note of your suggestion for a future update.

As for your question regarding the zipMoney plugin, it's not developed by us so I wouldn't know what to suggest. You'll have to reach to the authors directly.

Feel free to re-open this issue should you have any further questions. Thanks!

amcmorrow-revo commented 2 years ago

Just curious where to place brag-man's code snippets. Do they go in the functions.php file or somewhere else? Would it go on any page where we'd not need the Paypal button?