WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Free Shipping Rules Based on Role (24 posts)

  1. crazyk73
    Member
    Posted 1 year ago #

    I am wondering if anyone could point me in the right direction here. I am looking for a solution to this problem. Woocommerce offers an option to allow free shipping if the order is above a certain amount. I need to make this option a different amount for different kinds of user roles i.e wholesale and regular customer. I am figuring a custom snippet should get the job done, but I am not sure of the hook I need to be using to accomplish this task. I am thinking about using a filter so that IF the user role is wholesale then replace the variable used for the amount with the new number. Any help would be appreciated.

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

  2. danbrady
    Member
    Posted 1 year ago #

    Did you get this sorted? I'm looking for the same thing.

  3. crazyk73
    Member
    Posted 1 year ago #

    Yes I did...I created a ticket with woothemes support and they walked me through the code I needed to add to my functions.php file. I have not implemented yet so I am not 100% sure it will work. I do not want to post the code here, but would be happy to share via email, or pm.

  4. danbrady
    Member
    Posted 1 year ago #

    Thanks, that would be great. If you have a transcript of the walkthrough and code, could you please email: dan.brady [at] me.com

    Much appreciated :)

  5. danbrady
    Member
    Posted 12 months ago #

    It worked for us, with minor mods to suit our client's requirements, thanks again

  6. nyxerus
    Member
    Posted 11 months ago #

    This is exactly what I have been banging my head against the wall for. Any chance you guys could help me out? I need to provide free shipping to certain user roles. Any help would be appreciated. thanks.

  7. danbrady
    Member
    Posted 11 months ago #

    @crazyk73 – I don't see any reason not to post the code here?

    I'll leave it with you :)

  8. nyxerus
    Member
    Posted 11 months ago #

    Ya i would definitely appreciate it. If you prefer not to post it in here, please feel free to email me at gabriel [at] boley.me

    Thanks!

  9. crazyk73
    Member
    Posted 11 months ago #

    I will be more than happy to send the code to any other members if they need it. Just post here with your email.

  10. jeanne38654
    Member
    Posted 11 months ago #

    I'd like the code! [email removed]

    Thank you very much!!

  11. lsl923
    Member
    Posted 10 months ago #

    Can I please get the code - [email address moderated - these forums do not provide support via email]

    Thank you!

  12. WPyogi
    Volunteer Moderator
    Posted 10 months ago #

    These forums do not provide email support - and we do not recommend posting emails here.

    @crazyk73 - please post that to a pastebin for anyone else to download.

  13. WPyogi
    Volunteer Moderator
    Posted 10 months ago #

  14. danbrady
    Member
    Posted 7 months ago #

    A few people are emailing me for the code, so I am putting it here. But the disclaimer first – we amended the code supplied by woothemes (via crazyk73). This worked for our site and our specific requirements – make sure you test thoroughly for your site and needs.

    If you want the shortcut, quick option, purchase: the role-based-payment-shipping-methods extension/

    Alternatively, try this (note that we are giving free shipping to users of the role 'wholesale buyer' plus a couple of conditions which you will definitely want to change):

    In the theme's functions.php file, in the main class:

    add_filter( 'woocommerce_available_shipping_methods', array( &$this, 'woocommerce_set_free_shipping_for_certain_users' ));

    And add this:
    http://pastebin.com/i4vLvggn


    In case it wasn't clear – you need to modify this to fit your requirements

  15. Sean Barton
    Member
    Posted 3 months ago #

    I've just tried to use this latest snippet and just to let you know it's been depracated. You can use a play on the following code in the Woothemes documentation:

    http://docs.woothemes.com/document/hide-other-shipping-methods-when-free-shipping-is-available/

  16. Sean Barton
    Member
    Posted 3 months ago #

    I just put together a little example to give free shipping to replace the last example by @danbrady which no longer works.

    http://www.sean-barton.co.uk/2014/04/wordpress-snippet-woocommerce-free-shipping-role/

  17. danbrady
    Member
    Posted 3 months ago #

    Good work Sean, thanks for sharing :)

    We haven't yet upgraded WC to 2.1+ on the site we used this for, but when we do I'm sure we'll be revisiting your solution.

  18. Sean Barton
    Member
    Posted 3 months ago #

    Ah yes good point. Certainly a WC2.1+ implementation this is. They depracated the hook so @danbrady's approach will work beforehand and mine will work afterwards. They are similarly written so easy enough to integrate.

    ta
    S

  19. Chris Hatton
    Member
    Posted 3 months ago #

    @seanbarton

    Strange coincidence, I was just looking into this - Thanks for updating the thread.

    What do the rates and package parameters for and why are you passing 10 and 2 or have you just done that because it is the same as on the example snippet on the other page? The explanation on the example are one-liners and not easy to understand. I notice you redefine $rates in your code and $package is unused.

    I would have commented on your blog post, but I think it will benefit others posting here.

  20. Sean Barton
    Member
    Posted 3 months ago #

    No worries at all. 10 is the priority and 2 is the number of accepted arguments by the function I have declared. 10 is really arbitrary.. it means people can declare one as 9 and get theirs in first or 999 and have it running at the bottom of the pile. This is good mainly for filters and things which modify global variables for a variety of reasons. Also without the 2 only a single argument would be passed to my declared function.

    You are entirely correct that packages is never used but seeing as it was based on the Woo snippet I just glossed over that. It does not harm in it being there but you can safely remove the 2 from the declaration and the $packages from the function.

    $rates is redefined intentionally.. the function is a filter which means it receives a variable containing something and expects it back after it has been manipulated. Because my code conditionally manipulates the shipping methods the redeclaration is there to essentially empty the array called $rates AFTER I have removed the 'free shipping' settings and then save it back. Essentially it means that if there were 5 shipping rates defined then I am storing the content of the free shipping away, clearing the list and then rebuilding the list with ONLY the free part in there. Another way of writing it would be $rates = array($rates['free_shipping']); if you're wanting to compact it any.

    ta
    S

  21. Chris Hatton
    Member
    Posted 3 months ago #

    Thank you so much.

  22. Chris Hatton
    Member
    Posted 3 months ago #

    Sorry @seanbarton I am getting unpredictable results using the code.

    First I noticed it only applies when an item is added to the cart (i.e. the cart is updated). It took me a long time for me to realise that haha. But I wonder, does the shipping specifics (that are set in the WooCommerce shipping settings) still apply?

    By the way, I have changed...

    get_currentuserinfo();
    global $current_user;

    to...

    $current_user = wp_get_current_user();

    As it didn't seem to be grabbing anything.

  23. Sean Barton
    Member
    Posted 3 months ago #

    Shipping methods are cached. A great way to check it would be to go to WooCommerce and choose system status then choose the Tools tab and click Clear Transients. It should give better results thereafter.

    Hopefully that works. The code works perfectly for me and I did test it exhaustively. Happy to help you get it working though if you want to contact me directly or via this site or my blog.

    ta
    S

  24. Chris Hatton
    Member
    Posted 3 months ago #

    Oh awesome! Didn't know that was there!

    Ok cool, thanks, I'll give it some more testing trying out.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic