Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow US units on products/variants #5888

Merged

Conversation

andrewpbrett
Copy link
Contributor

What? Why?

No issue has been created yet, but see Discourse for background.

The change addresses the issue that for many farms in the United States, not having an easy way to sell products by the pound/ounce is enough to dissuade them from using OFN. There is a currently a workaround to use "items" and a custom string but this makes it much easier to use.

The Discourse post from @kirstenalarsen discusses four points; the first two are fairly straightforward and addressed by this PR.

Point 3 is certainly possible, and it would be great if enterprises/instances could hide certain units if they never use them so that the dropdown has fewer options. I think it'd take a fair amount of refactoring, however, and possibly making a model out of Scale or Unit, but I don't know a ton about how instances are configured yet and how that config trickles down and is read in the codebase.

Point 4 seems like it might add more complexity than it's worth, however, and I agree that it's beyond "minimum viable feature".

Other items to address:

As @luisramos0 noted further down the Discourse thread, it would be really nice to do some refactoring here... we have:

  1. two places where the units are defined, one in the javascript and one in ruby
  2. similarly, two places where we have logic for the autoconversion of the units given a variant unit_value
  3. lots of magic numbers floating around now (453.6 shows up a lot of different places, including tests, now and fat-fingering it somewhere would break things)

We could consider adding additional non-metric units for volume (pints, quarts, gallons, liquid ounces); this would strengthen the case/need for an instance/enterprise to be able to choose which units are available.

I noticed that, for example, if a variant's unit_value is '1.0' (1g) and you change the product's variant_unit_scale from '1.0' (g) to '453.6' (lb), the variant's unit_value gets converted... this was surprising to me when I did it but I suppose it makes sense. And it's probably rare enough that someone would want to change the units on a product once they can create the product with the desired units in the first place. As an aside, it's taken me a while to get the hang of the admin interface for products/variants/units... if it's not on the design roadmap yet, I have some ideas for making it more clear!

What should we test?

The test suite is (obviously) green as of 5680cc9 but there are still tests I'd like to add, particularly around the new javascript functions to test for bad/unexpected input from other files.

We should also do some extensive sanity/smoke tests, particularly around shipping calculations, and likely add some automated tests to the suite there too.

For example, @luisramos0 asked: "do shipping methods with weight calculators work for orders where the weight was changed in the bulk order management? Example, if I order two items of 0.25kg and then the hub manager uses Bulk Order Management to say the final weight was 400g, the shipping fee with cost 10eur per kilo will have to update from 5eur to to 4eur. This must work across units and scales." I think they're working properly, but I'm a little new to the codebase to know what working properly really looks like yet.

Release notes

"Pounds and ounces are now available as units for products and variants"

Changelog Category: Added

How is this related to the Spree upgrade?

Unrelated

Discourse thread

https://community.openfoodnetwork.org/t/hubs-managers-can-choose-the-adapted-weight-and-measure-units-for-their-shops-given-their-own-local-situation/1289/11

Dependencies

None that I know of.

Documentation updates

Probably! Will investigate and add soon.

@andrewpbrett
Copy link
Contributor Author

Hrrrm, that test was passing on my build.... is there any chance that's a spurious failure?

@Matt-Yorkley
Copy link
Contributor

is there any chance that's a spurious failure?

I restarted the build, and it's passing now 👍

Copy link
Contributor

@Matt-Yorkley Matt-Yorkley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andrewpbrett, this looks really promising! Could you add some more test coverage here on the Ruby side, for the changes in option_value_namer.rb and variant_and_line_item_naming.rb?

@andrewpbrett
Copy link
Contributor Author

I put a couple tests in there to start; I'm still sort of sussing out the conventions around testing and what we want to write as unit tests... feedback welcome in that department :)

@andrewpbrett
Copy link
Contributor Author

On the bulk order management page, all adjustments to the weight of a line item currently have to be done in grams. It would be nice if the weights here were scaled in the same way that the line item's variant is scaled, to avoid having to convert things manually to grams. I don't know how much this functionality is used though, or if it's worth holding off on releasing the ability to have US units on products/variants until we can also have US units on this screen.

@luisramos0
Copy link
Contributor

luisramos0 commented Aug 21, 2020

I started by just giving this PR a try and play with the app.

I notice that the calculator WEIGHT is always per kg. We need the calculators to be per lb right? It will be very weird to use this as is, products in lbs and ship fee per kg. I think can be done in an separate issue/PR.

I have verified that ship method is calculated correctly, it will convert the weight calculator price and use it for lbs.
This is a 1 AUD per kg calculator with a product of 1kg and a product of 1lb, correct fee is 1.45:
image

The problem I found is that the weight calculator is not working for Enterprise Fees. It think it takes lbs to be grams I think.
@andrewpbrett Enterprise fees are a type of fee you create for the enterprise and then add to the order cycle.
I am not sure why it is not working but Enterprise Fees do have quite a few differences to ship or payment methods.

Editing order quantities in the backoffice will update the ship fees according to weight in lbs correctly 👍

In BOM, changing quantities again, also works well 👍
As for the final weight volume I see a value converted to grams:
image

But, like the calculator in Kgs, it does work well and I can change the final weight volume of the 1lb product from 453.999 to 1000 and it will take 1000g for the calculation of the fee 👍
I think this is fabulous start and is acceptable for now!

Quick summary of what I see in terms of features: imperial units are available to be used and display correctly in the shopfront 🎉
There are 3 problems:
1 - weight calculator in kgs - if using imperial units, the weight calculator can be used for shipping fees but its price needs to be set "per kg". The calculations do convert the value to lbs and the values are correct.
2- weight calculator broken for enterprise fees - the weight calculator does not work correctly for enterprise fees with products in lbs
3- BOM final weight volume in grams - if we use lbs, in the BOM on the final weight volume field the user will see the correct weight value in grams and the user will be able to change this value (in grams) to another value in grams and this is correctly picked by calculators.

I think these 3 problems can be addresses in future PRs 🎉

Now I need to look at the code and then think about test coverage. I may be able to do this second part of the review this weekend.
Anyway, great job!

@luisramos0
Copy link
Contributor

To answer your previous comment about BOM Andy: one improvement couldd be to change the title of the column from "Weight/Volume (g)" to "Weight/Volume (in grams)" so that users don't miss it.

Copy link
Contributor

@luisramos0 luisramos0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Andy, I have created a PR on top of your branch, I fixed some rubocop issues as I was going through the code:
https://github.com/andrewpbrett/openfoodnetwork/pull/1/files
I have also moved option_value_namer and variant_and_line_item_naming to app/services where they belong. There's a lot to clean up in this space, this is just a minor improvement so we keep the boy scout rule and do a little refactoring in every change.

I m going to approve this PR as is (even better if you merge my PR into your branch :-)).
Anyway, imo this is good enough 🎉 I am approving it.

@andrewpbrett
Copy link
Contributor Author

Hi Luis, I merged your PR, thanks for the improvements. I'm going to work this week on the shipping calculator being in pounds and hopefully some of the other code cleanliness improvements we've talked about above, but I agree they can be in a separate branch and we can start having people test these changes as they are 🎉

zspencer and others added 15 commits September 3, 2020 09:10
See: https://community.openfoodnetwork.org/t/hubs-managers-can-choose-the-adapted-weight-and-measure-units-for-their-shops-given-their-own-local-situation/1289/11

We're not entirely sure what needs to be changed in order for this to
accurately work with shipping and other parts of the eCommerce platform.

We are assuming that so long as we canonically store the weight scale
in grams, that the shipping calculation will be able to do what it needs
to. So if we put in values for "oz" as grams, we may not need to do
much else in order to let product(s) be sold by the pound (or ounce).

Next steps appear to be:

- [ ] When looking at an order as a customer, do we want to show pounds
      instead of grams? (See: http://localhost:3000/orders/R125684626)
- [ ] Compile a list of tests that are worth writing (because we have
      no confidence that we know what we are supposed to be doing in
      order for this feature to be "ready" to be used by people.)
- [ ] Write a test that demonstrates when we create a product with a
      variant in pound that the product's shipping weight is correctly
      calculated?
- [ ] Do we want to think about i18n?
We are pretty sure this is not the correct final implementation, but
we wanted to get some tests failing so we can start to fix them.
This changes how we display the description of weight, but it doesn't
change the `Spree::OptionValue`s that are being created when someone
adds a product to their cart.

This takes us closer by making the UI look more correct; but it feels
odd compared to settiong the `Spree::OptionValue` to the correct
unit on creation.

But on the other hand, that could possibly make things worse for the
shipping calculation bits.
Copy link
Contributor

@sauloperez sauloperez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome! thanks @andrewpbrett and @luisramos0 👏 !

LGTM although we could add specific unit tests for variant_and_line_item_naming.rb. I'd make me feel safer if we ever refactor this logic. I'm happy to do that in a separate PR as according to Simplecov the methods we touched get covered already.

@andrewpbrett
Copy link
Contributor Author

Thanks for the review @sauloperez! And let me know if you want to collaborate on the refactoring PR.

@filipefurtad0 filipefurtad0 self-assigned this Sep 17, 2020
@filipefurtad0 filipefurtad0 added the pr-staged-uk staging.openfoodnetwork.org.uk label Sep 17, 2020
@filipefurtad0
Copy link
Contributor

Hi @andrewpbrett ,

Thanks for pointing me out to the discourse post - very helpful indeed. I went through it and performed the following test cases:

i) expect lb and oz units to appear in

  • the product page (/admin/products), when creating a new product
  • the product page (/admin/products), when bulk-editing an existing product
  • when editing an order cycle

image

image

  • in the shopfront:

image

  • order confirmation emails

image

ii) expect unit conversion to work correctly, in bulk product edit page

From a previously created product in (1) Kg
image

Interchanging from kg to lb and to oz works:
image

image

Converting between lb and oz works as well, ex:

image

iii) expect product import to accept new units in the field

image

image

This currently fails for oz and lb - I verified it works correctly for kg and g currently, so I'll open a new issue on this regard.

iv) expect to be able to place orders containing items with units of mixed measurement systems

Works fine and units appear displayed correctly.

v) expect enterprise fee weight calculator to work correctly with lb and oz

Verified that enterprise fee weight calculator only includes the kg unit:

image

kg product: 1kg
oz product: 16 oz
lb product: 2.2 lb

image

All good here, I think. What did you find not to be working before @luisramos0 ?

vi) expect shipping fee weight calculator to work correctly with lb and oz

Verified that shipping fee weight calculator only includes the kg unit
image

[9.9792 kg ( = 2.2 lb) + 4.536 kg ( = 16 oz) ] x 2 UK-pounds per kg = 31,0304

image

Discrepancy in values comes from rounding.  

Altogether:

image

vii) edit orders on Bulk Order Management - changing the amount in grams and recalculating fees on the orders to y

The Weight/Volume column shows the amount in grams:

image

After reducing all the items to half:

image

And editing the order one sees:

image

There is a credit owed, respective to the reduction in the amounts in both the enterprise fees and the shipment fees.

Asides from the import issue - test case iii) - I think this is good to go.
I would propose to merge this, and solve this issue in a separate PR.
Thoughts @luisramos0 @RachL ?

Ounces (oz) and pounds (lb) are introduced in OFN! 🎉
Thank you so much Andy.

@filipefurtad0 filipefurtad0 added feedback-needed and removed pr-staged-uk staging.openfoodnetwork.org.uk labels Sep 17, 2020
@Matt-Yorkley Matt-Yorkley merged commit 77eaebc into openfoodfoundation:master Sep 17, 2020
@luisramos0
Copy link
Contributor

Thanks Filipe.
re enterprise fees, maybe my test was not correct or maybe it was fixed meanwhile? Have you tried using a different fee value other than 1eur?

@filipefurtad0
Copy link
Contributor

You are right @luisramos0:
I didn't, and thinking of it, 1 is quite an unwise value to perform a unique test. Thanks for pointing this out.

I've changed the enterprise fee to 2.5 UK-pounds/kg. It looks correct as well:

image

@luisramos0
Copy link
Contributor

awesome, I tested again with sample data locally and enterprise fees are working! I am not sure what happened, it doesnt matter 👍

There's a PR for the shipping and payment methods calculators to support pounds so, it will soon be just BOM on the list of problematic things for imperial units 🎉

@jaycmb
Copy link
Collaborator

jaycmb commented Sep 21, 2020

Just to be sure that I understood correctly:

Both enterprise fee weight calculator and shipping weight only includes the kg unit (so I as an enterprise user can not choose an imperial unit for enterprise fee or shipping weight, for products I created with imperial units), but the units are converted so the amounts are calculated correctly?

And has been an issue created already for BOM?

@luisramos0
Copy link
Contributor

Hello, @jaycmb that depends if you are talking before or after #5998
@andrewpbrett have you tested Enterprise fees on 5998, will we have enterprise fees that can be set in lbs as pat of 5998?

I think we need a new issue for the BOM case.

@andrewpbrett andrewpbrett deleted the imperial-auto-units branch September 21, 2020 14:29
@andrewpbrett
Copy link
Contributor Author

Yes, I think you understand it correctly @jaycmb - this PR doesn't address those. However, as part of #5998 enterprise fees can be set in pounds.

(I did just notice that the UI for setting them up looks a bit off, I'll submit a change to address this):
Screen Shot 2020-09-21 at 7 40 05 AM

I'll create an issue for BOM 👍

@jaycmb
Copy link
Collaborator

jaycmb commented Sep 22, 2020

Another concern that came up again in today´s product curation meeting:
might be confusing for users to see a long list of units to choose from.
Could we have some kind of "simple" option (select, toggle..) for enterprises to choose if they want to have imperial, metric or both?

@luisramos0
Copy link
Contributor

yes, that's been discussed before 👍 I am not sure where...
we can have that toggle, we would need an issue for that.
My main concern is what happens if the user selects imperial, creates products with imperial units and then switches to metric? The simplest solution would be, when user switches to metric, to keep using imperial for the products that have imperial units but only show metric units on the drop downs to select units?

@andrewpbrett
Copy link
Contributor Author

That's what I was envisioning as well. I was picturing long term having each enterprise able to choose from a list of units available (I was thinking a multi-tick-box UI, maybe even separated by imperial/metric). Then a shop could even choose to ignore units they never use, like grams or ounces. And also an instance could have in its configuration the default choices for a new shop on that instance. I think the behavior Luis describes is correct for what should happen if a product is in a unit that's no longer in the list of units available - namely, nothing should happen until the user explicitly changes them.

Here's the issue I created for it: #6082

@luisramos0
Copy link
Contributor

ok 👍 sounds good, but I think this is out of scope for the original wishlist item: https://community.openfoodnetwork.org/t/hubs-managers-can-choose-the-adapted-weight-and-measure-units-for-their-shops-given-their-own-local-situation/1289

I'd say that instead of an issue #6082 it could be a new wishlist item on discourse to define how we are going to build this extra config and also ask how important is it/prioritize: it needs some inception I think.

@andrewpbrett
Copy link
Contributor Author

Got it; I'll close the issue for now and move its contents here: https://community.openfoodnetwork.org/t/allow-enterprises-to-choose-what-available-units-for-products-variants/2060

@jaycmb
Copy link
Collaborator

jaycmb commented Sep 23, 2020

@luisramos0 not sure if I understand the concern:

"what happens if the user selects imperial, creates products with imperial units and then switches to metric?"
I envisioned in the most simple solution - that is to solve the potential problem of confused users - the select should only affect the number of unit options displayed when creating new products, i.e. if enterprise wants to reduce number of options, they won´t be displayed in their unit size list (nor weight calculator & BOM), but it does not affect products that have been created already, i.e. they won´t be converted and just stay as they are

But that raises another questions, since we now discuss basically 2 different features:

  1. Allow user to hide / display options imperial vs metric or both when creating products
  2. Allow user to convert created products from one unit type to other

Was 2) tested and is this possible currently?

@luisramos0 point on #6082 being a new wishlist item: for a more sophisticated solution yes, that needs some inception work.
But for now, simple solution has been "prioritized" in the product curation meeting.
So if @andrewpbrett could work on the Multi-Select:

  • Imperial
  • Metric
    to solve 1)

(Think of for example a user in France, who won´t ever sell in oz just doesn´t wanna be bothered by those weird imperial units ;) (S) he should be able to hide them)

@andrewpbrett
Copy link
Contributor Author

Agreed that something like a multi-select is the first priority.

For your question about conversion: Right now if I have a product variant that is 1 kg and change the product's units to pounds, it will automatically change the variant to be ~2.2 lb. However, as @tschumilas points out, there's some "going further" work we could do here to encourage/allow different shops that might have different units to share a hub and have all their units show up the same without each farm having to recalculate.

@tschumilas
Copy link

Seriously - I had NO idea on this - if I enter a product in pounds, and I change that product's units to grams, it will auto-calculate? OMG - I can't wait to try this. Mega feature for Canada!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants