• 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.

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

Viewing 8 replies - 16 through 23 (of 23 total)
  • 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.

    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

    @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.

    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

    Thank you so much.

    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.

    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

    Oh awesome! Didn’t know that was there!

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

Viewing 8 replies - 16 through 23 (of 23 total)
  • The topic ‘Free Shipping Rules Based on Role’ is closed to new replies.