【问题标题】:WooCommerce loads wrong path to assetsWooCommerce 加载错误的资产路径
【发布时间】:2014-07-24 05:30:41
【问题描述】:

我有一个带有 WC 的 WP 站点,据我所知,它运行良好。我最近注意到 WooCommerce 中的很多功能都不起作用(即查询功能、显示/隐藏框等)。

我已经确定了这一点:

文件需要从正确的路径加载。似乎 WC 在路径中附加了两次 url。

文件的正确路径:

http://my-web-site.com/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js

什么叫:

http://my-web-site.com/my-web-site.com/wp-content/plugins/woocommerce/assets/js/frontend/add-to-cart.min.js

我确定了源文件,但找不到任何会导致此路径与其他调用不同的内容。我确定我错过了一些东西。代码如下:

    public function load_scripts() {
    global $post, $wp;

    $suffix               = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
    $lightbox_en          = get_option( 'woocommerce_enable_lightbox' ) == 'yes' ? true : false;
    $ajax_cart_en         = get_option( 'woocommerce_enable_ajax_add_to_cart' ) == 'yes' ? true : false;
    $assets_path          = str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ) . '/assets/';
    $frontend_script_path = $assets_path . 'js/frontend/';

    // Register any scripts for later use, or used as dependencies
    wp_register_script( 'chosen', $assets_path . 'js/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), '1.0.0', true );
    wp_register_script( 'jquery-blockui', $assets_path . 'js/jquery-blockui/jquery.blockUI' . $suffix . '.js', array( 'jquery' ), '2.60', true );
    wp_register_script( 'jquery-payment', $assets_path . 'js/jquery-payment/jquery.payment' . $suffix . '.js', array( 'jquery' ), '1.0.2', true );
    wp_register_script( 'wc-credit-card-form', $assets_path . 'js/frontend/credit-card-form' . $suffix . '.js', array( 'jquery', 'jquery-payment' ), WC_VERSION, true );

    wp_register_script( 'wc-add-to-cart-variation', $frontend_script_path . 'add-to-cart-variation' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
    wp_register_script( 'wc-single-product', $frontend_script_path . 'single-product' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
    wp_register_script( 'wc-country-select', $frontend_script_path . 'country-select' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
    wp_register_script( 'wc-address-i18n', $frontend_script_path . 'address-i18n' . $suffix . '.js', array( 'jquery' ), WC_VERSION, true );
    wp_register_script( 'jquery-cookie', $assets_path . 'js/jquery-cookie/jquery.cookie' . $suffix . '.js', array( 'jquery' ), '1.3.1', true );

【问题讨论】:

  • 尝试将$frontend_script_path = $assets_path .js/frontend/'; 更改为$frontend_script_path = $assets_path;
  • 感谢您的回复。进行了更改,但没有修复它,仍然加载相同的路径。
  • 作为临时修复,我对路径值进行了硬编码,但显然这只是在找到更好的解决方案之前。
  • 这里还有其他反馈吗?将 WooCommerce 升级到 2.2 后,我也遇到了这个问题(可能在该版本之前发生,因为我是从 2.0.20 升级的)并且在升级之前没有发生。

标签: javascript php jquery wordpress woocommerce


【解决方案1】:

我遇到了这个确切的问题,它似乎是由Root Relative URLs 插件引起的。禁用这个有问题的插件(它似乎也对WP Mail 产生了不利影响)似乎解决了我的问题。

【讨论】:

    【解决方案2】:

    在定义路径时最好使用 CONSTANTS。至少它们不能被覆盖。

    例如

    define('AD_PATH', WP_PLUGIN_DIR . '/' . basename(dirname(__FILE__)));
    

    你可以在任何地方使用它:

    wp_register_script( 'chosen', AD_PATH . '/assets/js/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), '1.0.0', true );
    

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多