覆盖 WooCommerce 前端 Javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11852570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:23:38  来源:igfitidea点击:

Override WooCommerce Frontend Javascript

javascriptwordpressoverridingwoocommerce

提问by Sebastien

Can someone guide me as to what is the proper method of overriding WooCommerce core Javascript files, specifically frontend files. I have not found any documentation on this and looking at the code, the path to the frontend script files is hard coded in the plugin so I doubt that placing an assets folder in my theme will do anything.

有人可以指导我什么是覆盖 WooCommerce 核心 Javascript 文件的正确方法,特别是前端文件。我没有找到任何关于此的文档并查看代码,前端脚本文件的路径在插件中硬编码,所以我怀疑在我的主题中放置一个资产文件夹会做任何事情。

What is the cleanest way to to this so that I can load a file located in my theme dir?

什么是最干净的方法,以便我可以加载位于我的主题目录中的文件?

Thanks

谢谢

回答by Simon Unger

I had the same problem except with add-to-cart.js. Simple solution is to DEQUEUE the woocommerce script and ENQUEUE your replacement. In my case I added the following to my functions.php:

除了 add-to-cart.js 之外,我遇到了同样的问题。简单的解决方案是对 woocommerce 脚本进行 DEQUEUE 并将您的替代品入队。就我而言,我将以下内容添加到我的functions.php:

wp_dequeue_script('wc-add-to-cart');
wp_enqueue_script( 'wc-add-to-cart', get_bloginfo( 'stylesheet_directory' ). '/js/add-to-cart-multi.js' , array( 'jquery' ), false, true );

You would want to DEQUEUE the 'wc-add-to-cart-variation' script. I don't think you haveto ENQUEUE with the same name, but I couldn't see a reason not to.

您可能希望使“wc-add-to-cart-variation”脚本出队。我不认为你入队用相同的名字,但我看不出一个理由不这样做。

Hope this helps.

希望这可以帮助。

If you're using WordPress Version 4.0.1 and WooCommerce Version 2.2.10. You can use the following scripts:

如果您使用的是 WordPress 4.0.1 版和 WooCommerce 2.2.10 版。您可以使用以下脚本:


wp_deregister_script('wc-add-to-cart');
wp_register_script('wc-add-to-cart', get_bloginfo( 'stylesheet_directory' ). '/js/add-to-cart-multi.js' , array( 'jquery' ), WC_VERSION, TRUE);
wp_enqueue_script('wc-add-to-cart');

回答by Danijel

WooCommerce loads frontend scripts and styles in class-wc-frontend-scripts.phpfile, and there can be found how the scripts are registered, enqueued, localized and dependencies.

WooCommerce 在class-wc-frontend-scripts.php文件中加载前端脚本和样式,可以找到脚本的注册、排队、本地化和依赖关系。

The preferred place to enqueue scripts in Wordpress is the wp_enqueue_scriptsaction hook, because that is the moment after Wordpress is fully loaded but before any output is made. And also I like to enqueue all my related script and styles in one section of code.

在 Wordpress 中加入脚本的首选位置是wp_enqueue_scriptsaction hook,因为那是 Wordpress 完全加载之后但在进行任何输出之前的时刻。而且我喜欢将所有相关的脚本和样式排在一段代码中。

When you want to completely remove some scripts, calling either wp_deregister_script()or wp_dequeue_script()is enough. But sometimes if want to make some changes and leave the existing dependencies, variables and localization there is a problem because plugins are loaded before themes. So enqueue functions will not work as you would expect. Simple wp_dequeue_script() => wp_enqueue_script()will not work, wp_deregister_script() => wp_register_script() will work, but localized data will be lost.

当您想完全删除某些脚本时,调用wp_deregister_script()wp_dequeue_script()就足够了。但有时如果要进行一些更改并保留现有的依赖项、变量和本地化,则会出现问题,因为插件是在主题之前加载的。所以入队函数不会像你期望的那样工作。简单的wp_dequeue_script() => wp_enqueue_script()无法正常工作wp_deregister_script() => wp_register_script()将工作,但局部的数据都将丢失。



This can be solved by working directly with $wp_scriptsglobal object that contains and manages all the scripts loaded through wp_enqueue_script()or registered with wp_register_script():

这可以通过直接使用$wp_scripts包含和管理通过wp_enqueue_script()以下方式加载或注册的所有脚本的全局对象来解决wp_register_script()

    add_action( 'wp_enqueue_scripts', 'load_theme_scripts' );

    function load_theme_scripts() {
        global $wp_scripts; 
        $wp_scripts->registered[ 'wc-add-to-cart' ]->src = get_template_directory_uri() . '/woocommerce/js/wc-add-to-cart.js';
    }

回答by Ritcher

Add this section to your function.php

将此部分添加到您的 function.php

function themeslug_enqueue_script() {
wp_enqueue_script( 'add-to-cart-variation', get_bloginfo( 'url' ). '/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart-variation.js', false );
}
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );