如何卸载新 WooCommerce 2.3.x 加载的 select2 script/styles?

How to unload select2 script/styles loaded by new WooCommerce 2.3.x?

我们是主题开发人员,我们已经在我们的 wordpress 主题中为 HTML 中的 SELECT 框使用了 select2 (http://select2.github.io/) 脚本。 刚刚发布的新 WooCommerce 2.3.x 现在也使用 select2 脚本。但是他们以许多不同的方式用自己的样式(大部分都带有 !important 标签)覆盖它的样式。

我们不能用 !important 规则重新声明他们的所有 CSS 更改,这将是我们不喜欢的很多 CSS 混乱。现在我们的 JS 脚本加载了 2 次,有时也会发生冲突(因为 woocommerce 加载了它自己的 select2 js)。

我的问题是在我们的主题功能中停用 woocommerce select2 CSS 和 JS 文件的最佳方法是什么?我们想使用我们的脚本版本和样式文件(也用于 woocommerce select 元素)。

我尝试使用 wp_dequeue_script、wp_deregister_script 和可用于样式的相同功能,但这没有帮助。正如我看到的 woocommerce 添加 scripts/css 在我们的主题已经初始化并且我们无法停用它之后。

谢谢。

这是在 /includes/class-wc-frontend-scripts.php 中加载的代码,我需要从主题功能中禁用它:

self::register_script( 'select2', $assets_path . 'js/select2/select2' . $suffix . '.js', array( 'jquery' ), '3.5.2' );
self::enqueue_script( 'select2' );
wp_enqueue_style( 'select2', $assets_path . 'css/select2.css' );

如何在主题功能中卸载此 CSS/JS 文件,而不更改原始 WooCommerce 文件?

这里有一个要点,可能会有所帮助或提供前进的方向:https://gist.github.com/gregrickaby/2846416

另请查看 includes/class-wc-frontend-scripts.php 以了解脚本是如何排队的。

我从下面的 URL 中复制了下面粘贴的代码,它显示了如何使单个样式出队,这也可能有帮助:http://docs.woothemes.com/document/disable-the-default-stylesheet/

// Remove each style one by one
add_filter( 'woocommerce_enqueue_styles', 'jk_dequeue_styles' );
function jk_dequeue_styles( $enqueue_styles ) {
    unset( $enqueue_styles['woocommerce-general'] ); // Remove the gloss
    unset( $enqueue_styles['woocommerce-layout'] ); // Remove the layout
    unset( $enqueue_styles['woocommerce-smallscreen'] ); // Remove the smallscreen optimisation
    return $enqueue_styles;
} 

我找到了解决方案:

add_action( 'wp_enqueue_scripts', 'mgt_dequeue_stylesandscripts', 100 );

function mgt_dequeue_stylesandscripts() {
    if ( class_exists( 'woocommerce' ) ) {
        wp_dequeue_style( 'select2' );
        wp_deregister_style( 'select2' );

        wp_dequeue_script( 'select2');
        wp_deregister_script('select2');

    } 
} 

对于获得 select 字段重复的任何人。由于 Woocommerce 调用以下内容,似乎出现了问题:

?wc-ajax=update_order_review

页面加载后,然后在每个 select 元素之后引入一个 select2 元素:

<span class="select2 select2-container ... >

此跨度显示在下拉列表下方并包含当前选择的选项。

Duplicate dropdown element

我尝试将 wp_dequeue_script( 'wc-checkout' ); 添加到他的代码中,这确实阻止了 span 的出现,但也阻止了其他需要的功能。

最后,我用css隐藏了跨度:

.woocommerce-checkout .select2-container {
    display:none;
}

最近对 WooCommerce (v 3.2.1) 进行了一些更改,他们在其中分叉了 select2 脚本,因此@Dmitry 上面的代码不再起作用。下面是对我有用的新代码..

add_action( 'wp_enqueue_scripts', 'agentwp_dequeue_stylesandscripts', 100 );

function agentwp_dequeue_stylesandscripts() {
    if ( class_exists( 'woocommerce' ) ) {
    wp_dequeue_style( 'selectWoo' );
    wp_deregister_style( 'selectWoo' );

    wp_dequeue_script( 'selectWoo');
    wp_deregister_script('selectWoo');

   }
}