Editing WooCommerce includes functions

Looking for assistance in exactly how I could override and edit a file like

plugins\woocommerce\includes\wc-cart-functions.php

I'm effectively trying to change the wc_cart_totals_shipping_html() function in a way that doesn't involve me editing the core plugin files.

Thanks

  • B
    • Flash Drive

    I'm assuming you got that from Google? I tried long and hard to make the second option work. No success... Exactly how would you recommend I hook into that specific function? The problem is I need to override it and edit the contents inside ...

  • Michael Kastler
    • Freaky Fast Code

    Hi B,

    Predrag has you on the right path for using hooks - that's definitely the way to do it. Unlike the template files, however, you won't need the whole /woocommerce/includes directory thing - that's not going to help with some of the core plugin functionality, and is really just designed for updating specific layout. Rather, what you need to do is the following:

    1. Figure out what hooks you are looking to alter. This can be easier or more difficult depending on how deep you are in the code ... however, woo is pretty good about identifying the hooks and the order they present in, inline in the php. Sounds from above like you already know or are pretty close.
    2. Figure out if what you are trying to do is completely replace or simply add to the existing function/hook. Additive code is much easier, naturally - if you are replacing it entirely you'll need to review the hook function itself to confirm you aren't breaking some other functionality you need.
    3. Write some code!! :slight_smile: This could go in your functions.php, or better yet create a woofunctions.php you pull in separately so it's easier to debug and reuse.

    I'm not familiar with the hooks you are looking at altering, but here's two examples of code that I've written into woo for a client that may get you on the right path:

    This first example is adding to a hook. Due to the prop65 warning required if you sell certain products in California, my client needed to add a label at the bottom of every product indicating we met the prop65 standards.

    In this case, I discovered our hook was 'woocommerce_after_single_product_summary' and that in order for it to come after the summary info, but before the "you might like" sections it would go somewhere between 10 and 15 in order. I chose 12 arbitrarily. As an aside, I chose to write this as code, rather than adding it to the theme page layout in the eventuality they wanted different labels on different product types - I can add case statements if necessary for that purpose later.

    Here's that code:

    add_action('woocommerce_after_single_product_summary', 'show_prop65_warning', 12);
    
    function show_prop65_warning() {
    	  echo '<div class="prop65"><a href="http://www.p65warnings.ca.gov" target="_blank"><img width="260" src="/wp-content/uploads/Prop-65-Wood-Dust-Chromium-Warning-Lbl.jpg" /> </a></div>';
      }

    This second example here completely removes the function of a hook and replaces it . This is super simple example, where I'm not even creating a new function, but simply moving the existing one (for title) from above to below the images. However, as above it requires knowing what hook the title is already in, what order it comes (for example this starts at 40), and where in the larger order it should be (in this case after images - they order to 5 - and before the meta and description - position 10). Thus we have:

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);
    add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 8);

    These are very simple examples, but you can expand to do much more advanced things ... e.g. I had to rebuild the product info output section to add new sections, and additional columns that also automatically calculated metric weights/dimensions from the imperial #'s entered ... now that took some work! Hopefully you get the idea here, and to get the right hooks and conditional tags these might be of assistance - I use them as reference frequently:

    https://docs.woocommerce.com/wc-apidocs/hook-docs.html
    https://docs.woocommerce.com/document/conditional-tags/

  • Michael Kastler
    • Freaky Fast Code

    One quick thing - just occurred to me is that working on the template files like Predrag said above is easiest in some cases ... and you can take anything from plugins/woocommerce/templates/ directory and subdirectories and place them into your theme file under /woocommerce/ and those will overwrite the existing.

    NOTE - you don't include the 'templates' part of the subdirectory for this to work. So for example when I updated the code to break the single product table out, calculate metric, and etc., I copied the from from:
    wp-content/plugins/woocommerce/templates/single-product/product-attributes.php
    to:
    wp-content/themes/mytheme/woocommerce/single-product/product-attributes.php

    But what that also means is that anything outside of the woocommerce/template directory must be programatically addressed as I wrote above - you can't copy it in and edit anywhere.

    Experiment a little and see if it gets you in the right place. OK hope some of this helps!! :slight_smile:

  • B
    • Flash Drive

    Hi Michael, thanks for your response.

    Yeah, regarding templates, I'm fully aware on how they work and use them often. :slight_smile:

    In regards to the core functionality, I'm attempting to add data to the AJAX call that is used every time you change your shipping options. Essentially, I have added my own custom select drop boxes that will further show/hide some of the options depending on the options selected. The issue I have is that I need to pass those select box values along with do additional coding to hide/show the shipping options that should be available based on those pre-selected options.

    At the moment, the shipping method AJAX function only preserves the shipping method select itself into a function called wc_cart_totals_shipping_html in the wc-cart-functions.php file as "$chosen_method". I essentially need to add to that array that is passing this perpetual data and I'm honest not sure of the best method for doing this in WooCommerce. I'm a long time php and wordpress developer, but when it comes to editing WooCommerce on a fundamental level, I'm rather ignorant.

  • Michael Kastler
    • Freaky Fast Code

    Ahh, gotcha - the extra detail helps a lot!

    I don't know the precise answer here; obviously you have a lot going on with what you are changing. How I would approach it though, is to start with wc-cart-functions.php and look closely at the way that the $chosen_method variable is populated.

    In my version of woo, i see the first instance at line 210 of the code:

    foreach ( $packages as $i => $package ) {
    		$chosen_method = isset( WC()->session->chosen_shipping_methods[ $i ] ) ? WC()->session->chosen_shipping_methods[ $i ] : '';

    From there, depending on what you're doing I'd suggest possibly adding your own calls to pull in the data areas you've added and process them however you envision - but you may have to go deeper into those shipping methods.

    When programming for woo, it's a bit of a goose chase sometimes - you'll probably need to look into the other files for instances of chosen_shipping_methods, so you can figure out how it gets that array in the first place ... that may be as easy as opening up a couple that are named in such a way you can assume, or I've pulled the code over locally and do global searches often (I use visual studio, but assume most ide will have that ability).

    And that's how you'll solve most problems in woo - start with whatever you can find, break down the creation of the data you're looking to alter, figure out what hooks or files will lead you to the next bit of info ... rinse and repeat until you've got what you want. Generally if they are using hooks available in other files, they'll comment them in at the top of the function as hook name followed by order of call, so you can look for those notes as helpers (like /* @the_hook_name 15 )

    Finding someone who's expert in 'chosen_shipping_methods' is probably a lot to ask ... but if you figure this out, then the good news is YOU'LL be that person!!! :slight_smile: Good luck to you sir!

  • B
    • Flash Drive

    Well that's the issue I am having. I found all the same things you did. And editing it isn't an issue in terms of PHP logic. It's HOW I'm supposed to change it. How would I "hook" into it ... I would need to override it, no? Hooking into whatever, I would only be able to add to it.. not replace it, no?

    Thanks again

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.