• Resolved Hendra Setiawan

    (@hendcorp)


    Hi,
    I was wondering how to override a single javascript function from WooCommerce extension?

    I found some articles on google that mentioned that I have to copy entire .js file into my child-theme, modify from there (.js in child-theme) and then enqueue the entire .js file in the child theme to override the parent.

    Is that the correct way to override a javascript function?
    And where to put the .js file in my child-theme folder?
    Should I create a folder like “my-child-theme/js/jsname.js”?

    Thanks!

    https://www.ads-software.com/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    You can’t override javascript files like that. I suppose you could dequeue the plugin’s js file in your child theme: https://codex.www.ads-software.com/Function_Reference/wp_dequeue_script. Then you could enqueue your own in it’s place.

    The better option would be to write your javascript to work along side the main script though, rather than needing to directly edit it.

    Thread Starter Hendra Setiawan

    (@hendcorp)

    Hi Caleb,
    thanks for the answer!

    Do you have any reference to write my javascript to work along side the main script?

    Actually, I only need to make a little customization on WooCommerce Product Bundle extension. The file is add-to-cart-bundle.js and I think it’s not a good idea to dequeue the whole original Javascript file and replace it with a new customized file.

    I just need to make a little modification on this function:

    /**
     * Formatted bundle contents for display in Composite Products summary views.
     */
    this.cp_get_formatted_contents = function(component) {
    
        var formatted_contents = '',
            bundle_qty = component.get_selected_quantity();
    
        $.each(this.bundled_items, function(index, bundled_item) {
    
            if (bundled_item.$self.hasClass('bundled_item_hidden')) {
                return true;
            }
    
            if (bundled_item.$bundled_item_cart.data('quantity') > 0) {
    
                var formatted_item_title = bundled_item.$bundled_item_cart.data('title'),
                    item_quantity = parseInt(bundled_item.$bundled_item_cart.data('quantity') * bundle_qty),
                    formatted_item_quantity = item_quantity > 1 ? '<strong>' + wc_composite_params.i18n_qty_string.replace('%s', item_quantity) + '</strong>' : '',
                    formatted_item_meta = wc_cp_get_variation_data(bundled_item.$bundled_item_cart.find('.variations'), true);
    
                formatted_item_title = wc_composite_params.i18n_title_string.replace('%t', formatted_item_title).replace('%q', formatted_item_quantity).replace('%p', '');
    
                if (formatted_item_meta) {
                    formatted_item_title = wc_composite_params.i18n_selected_product_string.replace('%t', formatted_item_title).replace('%m', '<span class="content_product_meta">' + formatted_item_meta + '</span>');
                }
    
                formatted_contents = formatted_contents + '<span class="content_bundled_product_title content_product_title">' + formatted_item_title + '</span>';
            }
        });
    
        return formatted_contents;
    };

    Thanks!

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    If other scripts call that directly, you won’t be able to just add your own function. You’d have to replace the entire script. If something needs customisation consider informing the author to see if they can make it extensible.

    Thread Starter Hendra Setiawan

    (@hendcorp)

    Hi Mike,
    thanks for the answer!

    Actually, I’ve already posted a ticket on WooCommerce support but still didn’t get any response so far. It’s been 3 days and my client keep asking me about the progress. ??

    I will try to contact the author as well.
    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Is it possible to override one javascript function from WooCommerce extension?’ is closed to new replies.