WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Overwriting add-to-cart-variation.min.js (11 posts)

  1. budThornhill
    Member
    Posted 1 year ago #

    I have modified the add-to-cart-variation.min.js in woocommerce directly to get the functionality I want. It works, but I dont like overwriting the core file directly. i want to create a plugin.

    I'm new to plugins, so bear with me. I have create a my_plugin_name.php file. I've looked up several tutorials online about how to add a woocommerce template override in the plugin.php, but not a woocommerce js override.

    Any pointers? Any simple plugins out there I can look at and see how they did it?

    Thanks,
    Bryan

    http://wordpress.org/extend/plugins/woocommerce/

  2. DesignLoud
    Member
    Posted 1 year ago #

    man, I was hoping for some luck on this.. I am in the exact same situation.. Tried dequeueing the scripts enqueue mine but cant seem to get it right.. Especially with the add-to-cart-variation.min.js

    Anyone know of anything? I am also trying to write a plugin that will over ride the variable.php file and add-to-cart-variation.min.js file in woocommerce..

  3. samueljeden
    Member
    Posted 1 year ago #

    What sort of modifications have you made to the file?

  4. royho
    Member
    Posted 1 year ago #

    As samueljeden stated, what are you trying to do? Because it may not be necessary to modify it as it comes with many triggers you can already use.

    In addition, dequeueing and enqueueing does work for this.

  5. DesignLoud
    Member
    Posted 1 year ago #

    I am just trying to dequeue their script and enqueue my own but I cannot seem to get it working.. I got the variable.php issue worked out

    Here is what I have in my plugin

    function register_woo_radio_button_scripts () {
    
      wp_dequeue_script('wc-add-to-cart-variation');
      // Register the script like this for a theme:
      wp_register_script( 'wc-add-to-cart-variation', plugins_url( 'woo-radio-buttons/woocommerce/js/add-to-cart-variation.min.js', __FILE__ ), array( 'jquery'), false, true );
    
    wp_enqueue_script('wc-add-to-cart');
    }
    add_action( 'wp_enqueue_script', 'register_woo_radio_button_scripts' );

    But that neither dequeues their script or picks up on mine when I view the source.

  6. royho
    Member
    Posted 1 year ago #

    Well for one thing your code is not correct. your enqueue script is calling "wc-add-to-cart" but your register script is called "wc-add-to-cart-variation".

    And second, you need to use wp_deregister_script()... So wp_deregister_script('wc-add-to-cart-variation');

  7. DesignLoud
    Member
    Posted 1 year ago #

    Ya I for some reason didnt even think of deregister script, thanks for that. I did catch the name thing just after posting my last one. I still cant seem to get either of them to work though.

    here is updated:

    function register_woo_radio_button_scripts () {
    
      wp_deregister_script('wc-add-to-cart-variation');
    
      wp_dequeue_script('wc-add-to-cart-variation');
    
      wp_register_script( 'wc-add-to-cart-variation', plugins_url( 'woocommerce/assests/js/frontend/add-to-cart-variation.min.js', __FILE__ ), array( 'jquery'), false, true );
    
      wp_enqueue_script('wc-add-to-cart-variation');
    
    }
    
    add_action( 'wp_enqueue_script', 'register_woo_radio_button_scripts' );

    is that correct?

  8. DesignLoud
    Member
    Posted 1 year ago #

    Nevermind, I solved it.. Thanks for the help

  9. budThornhill
    Member
    Posted 1 year ago #

    Thanks for the updates.

    Rather than create a plugin, I actually did this via my child theme. Now that I've had more experience with modifying files via triggers/filters, I find this to be a better way.

    In my child theme, I create a /childthemedir/js directory and placed my version of the javascript (a copy of the original with some minor modifications).

    Then, in my /childtheme/functions.php file, I did this:


    wp_deregister_script('wc-add-to-cart-variation');

    wp_register_script( 'wc-add-to-cart-variation', $cust_child_theme_dir.'/js/add-to-cart-variation.min.js', array( 'jquery' ), $wp_version, true );

    I had read somewhere that a plugin was the preferred method of modifying core js (or other plugin js files), but I think this is cleaner as it keeps all of my customizations in one place.

    I hope this helps someone else in the future who searches the forums.

    -B

  10. mbrinson
    Member
    Posted 1 year ago #

    Could you please share your solution DesignLoud?
    I'm in the same boat.

  11. mbrinson
    Member
    Posted 1 year ago #

    Unfortunately for me, budThornhill's solution isn't working for me either.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic