1

I need to accept a decimal value for the quantity of a line item and for the total quantity. I understand this is not a normal request for most products but my client sells items by the foot and needs the flexibility to sell lengths in feet and inches (ex. 10 ft. 6 in. = 10.5 or 5 ft 6.5 in. = 5.541. I could probably hack this using JavaScript on the front-end but would like to do this the correct way. Any guidance is greatly appreciated. Has anyone achieved this?

2

You could build a plugin that listens to https://craftcommerce.com/docs/events-reference#commerce_cart.onbeforeaddtocart and translates the front end values of 10.5 or 10'6" or whatever into the number of inches (or, if you're working in a sane part of the world, even cm!).

Then on display of the cart or updates etc, you'll want to use some JS and/or twig maths to translate that back into those crazy feet/inch units you need there.

I honestly think trying to retro-fit in partial unit sales into Commerce as a whole would be quite a considerable challenge, so I think the best way to solve this is to use your smallest unit as the back end unit of quantity for the product, and do the magic in your plugin/and display.

4
  • You are the second one to present this solution, thank you! The biggest problem is getting the client the change all their pricing to reflect the smallest unit of measure. Was hoping I could change the database or extend the LineItem Model to do this but can't find any good docs on that. – Richard Gil Aug 9 '17 at 14:45
  • Well if you're not going down the above path, a custom purchasable would be the way to go I guess. That will involve digging in to the Purchasable interface and fair bit more work.... – Jeremy Daalder Aug 9 '17 at 23:58
  • Are there any good resources for writing a custom purchasable? I haven't been able to find anything. I am a generalist, so I can muck my way through it but need a good starting point. Thanks for all the replies, appreciate it. – Richard Gil Aug 10 '17 at 15:36
  • Other than this? craftcommerce.com/docs/purchasables - that should be enough to get going (with some Luke help on Slack added ;) ) - but personally I would solve it the other way. – Jeremy Daalder Aug 10 '17 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.