• Resolved aofhh

    (@aofhh)


    Hi,
    i’d like to setup my flat rate shipping fees.

    The basic shipping fee is 1$ + 0,1$ for each item.
    Here’s the code: 1 + ( 0,10 * [qty] )
    This works fine.

    Additionally i’d like to add a maximum shipping fee of 4$.
    [max_fee=”4”]
    I don’t know how to combine the two lines of code correctly …
    Any ideas how to write the code correctly …?
    Thanks in advance!
    Alex

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter aofhh

    (@aofhh)

    In short i’d like to create a quantity based flatrat combined with a maximum fee of 4$.

    Thanks!

    Unfortunately this is not possible out of the box. You can’t combine those commands.
    Here’s a snippet to limit shipping costs:

    add_filter('woocommerce_package_rates', 'cap_shipping_costs', 10, 2);
    function cap_shipping_costs($rates, $package) {
    $max_shipping_cost = 4; // Set the maximum shipping cost in euros

    foreach ($rates as $rate_id => $rate) {
    if ($rate->cost > $max_shipping_cost) {
    $rates[$rate_id]->cost = $max_shipping_cost;
    }

    // Adjust taxes if any, based on the new capped shipping cost
    if (isset($rates[$rate_id]->taxes) && is_array($rates[$rate_id]->taxes)) {
    foreach ($rates[$rate_id]->taxes as $tax_id => $tax) {
    $tax_rate = $tax / $rate->cost;
    $rates[$rate_id]->taxes[$tax_id] = $tax_rate * $max_shipping_cost;
    }
    }
    }

    return $rates;
    }
    • This reply was modified 4 months, 2 weeks ago by chris2201.
    Thread Starter aofhh

    (@aofhh)

    Hi Chris,
    thanks a lot for the infos & your support!
    Well, i’m not really into java script / php … my knowledge ends with css … ??
    So i’m not sure where to add the code above. (functions.php ….?)

    And the code you’ve been sending is global …?
    I’m asking because i forgot to mention that i’d only like one product catagory (in this case postcards) to work with the quantity based flatrat. (The flatrate setup for all the other products works fine with standard settings.)

    If it’s too complicated to work this out, just let me know.
    If so i’ll check some plugins to manage this.

    Thanks again & cheers,
    Alex

    Hi @aofhh,

    So i’m not sure where to add the code above. (functions.php ….?)

    Just to add onto what @chris2201 had already shared, you’ll need a plugin like Code Snippets in order to correctly add it to your functions.php.

    And the code you’ve been sending is global …?

    From my understanding, the shared code snippet should have a global effect and not to a single category.

    If it’s too complicated to work this out, just let me know.

    As this is beyond the scope of our support, I would recommend the following:

    1. Running the exact question you’re asking, along with the code provided, through an AI platform like ChatGPT for recommendations/changes to your code;
    2. Checking whether there are existing plugins in the WordPress plugin repository that might be doing that already.
    3. Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions): https://woo.com/community-slack/

    I hope this helps!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.