WordPress.org

Ready to get started?Download WordPress

Forums

Possible to overwrite a WooCommerce Widget? (14 posts)

  1. Jaja..
    Member
    Posted 2 years ago #

    Hello,

    I was working on a website with this WooCommerce WP plugin and i was asking myself would it be possible to overwrite a default WooCommerce Widget template?

    I know i can overwrite the template files by copy-/pasting them from my plugin folder to my theme folder, I've done that.
    But now i would like to use the Featured product Widget and change the core code a little bit so i can use that Widget inside a homepage slideshow.

    Only problem i face is this Widget (widget_featured_products.php) is inside the plugin folder so it will be overwritten by the next update from WooCommerce if i make any changes to that file.

    Thanks.

  2. Smith
    Member
    Posted 2 years ago #

    did you work out how to fix this issue?

  3. mbizfreak
    Member
    Posted 2 years ago #

    Here's what I did -- it's a roundabout way, but I couldn't find any other solution. I made a separate folder under my theme called 'woowidgets' and then copied over the ones I needed from the '/plugins/woocommerce/widgets' folder.

    Then, I copied the function that loads/initializes the widgets from the '/plugins/woocommerce/widgets/widget-init.php' file. I had to modify the naming conventions so that I didn't try to make a second call to that same function.

    Finally I updated my theme's functions.php file to include the new initialization string and include to reference the widget file within my newly created widget folder. Now I'm free to modify that widget as a custom file and it will appear on my widget screen in the UI alongside the original Woocommerce-supplied one. I'll just drag my widget into the sidebar instead. Here's more or less what my structure looked like afterwards:

    functions.php

    include_once('woowidgets/widget-cart.php');
    function woocommerce_register_widgets_CUSTOM() {
    register_widget('WooCommerce_Widget_Cart_MJ');
    }
    add_action('widgets_init', 'woocommerce_register_widgets_CUSTOM');

    /woowidgets/widget-cart.php
    I replaced any instance of 'WooCommerce_Widget_Cart' with 'WooCommerce_Widget_Cart_CUSTOM'

    I also edited line 26 '$this->woo_widget_name = __('MY CUSTOM CART WIDGET', 'woocommerce' );'

    Lastly, I edited line 31 to give the cart a new name:
    /* Create the widget. */
    $this->WP_Widget('shopping_cart_CUSTOM', $this->woo_widget_name, $widget_ops);

    It all seems to be working pretty well, just a lot of copying, pasting and editing. let me know if it works out for you if you find a better/cleaner solution.

  4. mbizfreak
    Member
    Posted 2 years ago #

    ...forgot to mention that at least going about it this route ensures that with a WooCommerce update, nothing will be overwritten. The only danger is a major code change that could break it, but I don't foresee that happening any time soon.

  5. mbizfreak
    Member
    Posted 2 years ago #

    Correction on that functions.php, I typed that in hastily, it should actually be this:

    include_once('woowidgets/widget-cart.php');
    function woocommerce_register_widgets_CUSTOM() {
    register_widget('WooCommerce_Widget_Cart_CUSTOM');
    }
    add_action('widgets_init', 'woocommerce_register_widgets_CUSTOM');
  6. ivyowl
    Member
    Posted 2 years ago #

    Hi there,

    This is the solution i've been hunting for!

    However, i cant get it to work! I'm getting a server error :(

    I'm trying to edit the login widget so i have copied that into a new woowidget folder in my theme.
    I've editied all instances of WooCommerce_Widget_Login to WooCommerce_Widget_Login_CUSTOM in the widget-login.php file

    edited this:
    $this->woo_widget_name = __('WooCommerce CUSTOM Login', 'woocommerce' );

    and this:

    $this->WP_Widget('woocommerce_login_CUSTOM', $this->woo_widget_name, $widget_ops);

    and added this to my functions.php file:

    include_once('woowidgets/widget-login.php');
    function woocommerce_register_widgets_CUSTOM() {
    register_widget('WooCommerce_Widget_Login_CUSTOM');
    }
    add_action('widgets_init', 'woocommerce_register_widgets_CUSTOM');

    unfortunately its breaking the site though!

    any idea whats wrong?
    Many thanks in advance.

  7. mbizfreak
    Member
    Posted 2 years ago #

    I think you might just be missing something lower down in the code of that particular file -- around line 210. Try this file, I tweaked the original one with your CUSTOM name:

    widget-login.php

    Otherwise, your functions.php entries look fine.

  8. mbizfreak
    Member
    Posted 2 years ago #

    Oops -- I lied -- I grabbed a modified version that I had made for myself. Here's the original WOO-provided file, but modified based on your CUSTOM naming:

    widget-login.php

  9. Lodewijk van der Pol
    Member
    Posted 1 year ago #

    Hi mbizfreak,

    I really think your roundabout way is a solution for an issue I have.
    But I do have one question, before I mess thing up.

    What do you mean with:

    "I had to modify the naming conventions so that I didn't try to make a second call to that same function"?

    How did you modified what? Could you please explain this?

    Thanks!

  10. Lodewijk van der Pol
    Member
    Posted 1 year ago #

    What I want to do is change the size of the product image in the cart widget. I just want the image to be a tiny thumbnail (about 45x45 pixels).

    I found this line in the widget_cart.php:
    echo $_product->get_image();

    But how can I change the style or size of this image?
    Any suggestions? Thx!

  11. rawdolphe
    Member
    Posted 1 year ago #

    Interesting, how would you add two or three widgets?

    PS: in the functions.php I meant.

  12. rawdolphe
    Member
    Posted 1 year ago #

    I had a go, and it seems to work well. Thank you.

  13. ethanhinson
    Member
    Posted 1 year ago #

    This worked quite well for me too. Seems like the best solution I have found so far.

  14. rawdolphe
    Member
    Posted 1 year ago #

    Yeah, it's pretty good -- I tested it, although it's on the back burner atm, I'll get back to it. How/where is mbizfreak? Thanks again.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.