user3747074
user3747074

Reputation: 63

WooCommerce loads wrong path to assets

I have a WP site with WC, which was working fine to my knowledge. I recently noticed a lot of the functions weren't working in WooCommerce (i.e. inquiry functions, show/hide boxes etc).

I have nailed it down to this:

The files need to be loaded from the correct path. Seems like WC is appending the url twice in the path.

CORRECT PATH TO FILE:

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

WHAT IS BEING CALLED:

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

I nailed down the source file, but I couldn't find anything that would cause this path to differ from other calls. I am sure i am missing something. Here is the code:

    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 );

Upvotes: 2

Views: 1637

Answers (2)

Eric K
Eric K

Reputation: 716

I had this exact problem and it seemed to be caused by the Root Relative URLs plugin. Disabling this problematic plugin (it seems to adversely effect WP Mail as well) seemed to solve the problem for me.

Upvotes: 1

David
David

Reputation: 5937

You are better off using CONSTANTS when defining paths. At least then they cannot be overwritten.

e.g.

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

And you can use it then whereever:

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

Upvotes: 0

Related Questions