【问题标题】:wp_util loaded before underscore causing error in WordPress Woocommerce frontendwp_util 在下划线之前加载,导致 WordPress Woocommerce 前端出错
【发布时间】:2021-09-22 17:37:02
【问题描述】:

在客户网站上工作,突然出现错误 - 无法将变体产品添加到购物车,一直说“请先选择一些产品选项,然后再将此产品添加到您的购物车。”即使选择了其中一个选项。 the dev website product link here

在控制台看到 JS 错误 "wp-util.min.js?ver=5.7.2:2 Uncaught ReferenceError: _ is not defined" 并意识到这是一个脚本入队错误,因为下划线应该在 wp-util 之前入队. 如果我以管理员身份登录,并且下划线在 wp-util 之前排队,我可以做任何事情。

我该如何解决这个错误?有没有办法调试排队的订单?尝试将 wp_enqueue_script 添加到 childtheme 样式的队列句柄,并将 add_action 优先级更改为最高(即 0),但这无法修复它。 如何确保在 wp-util 之前加载下划线?

if ( ! function_exists( 'suffice_child_enqueue_child_styles' ) ) {
function customizr_child_enqueue_child_styles() {
    // loading parent style
    wp_register_style(
      'parente2-style',
      get_template_directory_uri() . '/style.css'
    );

    wp_enqueue_style( 'parente2-style' );
    // loading child style
    wp_register_style(
      'childe2-style',
      get_stylesheet_directory_uri() . '/style.css'
    );
    wp_enqueue_style( 'childe2-style');
    //enqueue underscore
    wp_enqueue_script( 'underscore' );
  }
 }
add_action( 'wp_enqueue_scripts', 'customizr_child_enqueue_child_styles',0 );

【问题讨论】:

    标签: php wordpress woocommerce wordpress-theming


    【解决方案1】:

    来自 wp_localize_script 文档:

    重要!必须在使用 wp_register_script() 或 wp_enqueue_script() 注册它所附加的脚本之后调用 wp_localize_script()。

    所以,首先加载你的脚本,然后加载 wp-util。

        wp_localize_script( 'ajax-script', 'jsVars', [ 'ajax_url' => admin_url( 'admin-ajax.php' ) ], [ 'wp-util' ] );
        wp_enqueue_script( 'wp-util' );
    

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 2016-10-27
      • 1970-01-01
      • 2014-03-28
      • 2015-07-28
      相关资源
      最近更新 更多