The Foxy forums are on the move!

We're in the process of moving our forums over to a new system, and so these forums are now read-only.
If you have a question about your store in the meantime, please don't hesitate to reach out to us via email.

Setting custom flat rate

hi,

I'm trying to set a custom flat rate total_shipping amount. my customer wants to be able to populate a cart for their customers and they want to be able to set the flat rate shipping amount to whatever they want. I thought i could maybe affect the twig template value by passing in a custom field and setting the total_shipping based on that like so

{% for key, custom_field in custom_fields %}
{% if key == "Flat_Rate" %}
{% set total_shipping = custom_field.value %}
{% set total_order = total_order + custom_field.value %}
{% endif %}
{% endfor %}


but it doesnt seem to work as expected bcs total_order doesn't get updated.

is there anyway to to do this via form submission? thanks
Comments
  • fc_adamfc_adam FoxyCart Team
    edited December 2018
    @freshjones,

    Good question. While we don't support customising the shipping value via Twig like that - it is possible using our custom shipping code functionality available in the administration.

    We can help you with how to approach that - could I confirm how exactly the shipping value will be passed through? Will it be on custom options at the product level where the shipping cost for each product needs to be added together? Or just as a single hidden session variable on the cart?
  • just a single hidden session variable on the cart...but please note that this is just for some special cases...in most instances we are using the custom shipping endpoint to bring back live shipping rates.

    its just on some specific orders (ones that are generated internally by my customer) where they need to be able to set things specifically.

    so we can enable the custom shipping code functionality *if* it can be used conditionally on some orders, but not for all orders?

    thanks!
  • fc_adamfc_adam FoxyCart Team
    @freshjones,

    Thanks for the additional details. If you're already using the custom shipping endpoint to return rates - I'd suggest adding your additional logic there to return the shipping cost from the hidden session attribute rather than whatever custom logic the endpoint is already doing.

    The shipping endpoint receives a full summary of the customers cart - including any session attributes.

    As a quick example, taking what you pasted above, you could add something like this to your custom shipping endpoint (assuming you're using the PHP endpoint):
    $preset_shipping = 0;

    foreach ($cart_details['_embedded']['fx:custom_fields'] as $field) {
    if ($field['name'] == "Flat_Rate") {
    $preset_shipping = $field['value'];
    }
    }

    if ($preset_shipping > 0) {
    $rates->add(10001, $preset_shipping, "", "Standard Shipping");
    } else {
    // Your existing custom shipping logic
    }

    That logic loops through any custom fields, and if it finds the "Flat_Rate" one, it assigns it to a variable. Then if that variable is greater than 0, it just adds a rate for the preset shipping, otherwise it can proceed with your existing custom shipping logic.

    Does that help?

  • ah thats cool thanks!
  • freshjonesfreshjones Member
    edited December 2018
    hey so the above works, but its not ideal in that customer has to enter shipping info and wait for live response then check the single option...where as a flat rate is already defined and in place....

    i was trying this code in the custom shipping code logic

    for (let d in cart['_embedded']['fx:custom_fields']) {
    if (cart['_embedded']['fx:custom_fields'][d]['name'] == "Flat_Rate") {
    rates.hide();
    rates.add(10001, cart['_embedded']['fx:custom_fields'][d]['value'], '', 'Shipping');
    }
    }


    which works great, except for when that variable is missing...then an error occurs about the shipping not being configured correctly.

    the docs seem to indicate that I can *in theory* use live shipping with the above and that the above should run after all other shipping options?

    is that not true? or am I doing something wrong?

    basically what I do is have a specific category set as flat rate and then i set all products to that category if it should be flat rate shipping. other wise default category should use live rates...

    thanks!
  • fc_adamfc_adam FoxyCart Team
    @freshjones,
    hey so the above works, but its not ideal in that customer has to enter shipping info and wait for live response then check the single option...where as a flat rate is already defined and in place....
    With our current shipping set up, when using live rates or rates from our custom shipping code feature, it does rely on the customer having entered a shipping address before rates are returned. It is possible to have the first rate selected automatically if you'd like with a small snippet - you can see that here: https://pastebin.com/raw/N2wc11wK - and you'd include that in the "footer" textarea of the "Add custom header and footer code to your templates" option found on the "configuration" page.
    which works great, except for when that variable is missing...then an error occurs about the shipping not being configured correctly.
    Your code there looks fine - except that if you have multiple products present with that custom attribute, it will result in duplicate rates which will cause an error. The code I provided earlier would allow you to have the flat rates that are passed through with custom attributes. If you wanted to have the code I sent earlier included via the custom shipping code instead, that's possible too - it'd just need to be converted to javascript compatible.
    the docs seem to indicate that I can *in theory* use live shipping with the above and that the above should run after all other shipping options?
    That's right - the custom shipping code entered in the admin is executed after all other live shipping has been calculated - whether that's from USPS, FedEx and UPS, or from a custom shipping endpoint.
    basically what I do is have a specific category set as flat rate and then i set all products to that category if it should be flat rate shipping. other wise default category should use live rates...
    It's worth noting that if you have a category that is set to flat rate shipping as it's delivery type, the custom shipping code won't be run if that's the only category present on the cart.

    Does that help?
Sign In or Register to comment.