Decimal Quantities

Contrib type: 
Module
Status: 
Work in progress
Moderation: 
Not ready for moderation

Downloads

Compatibility: 
Ubercart 2.x

As discussed on Abiility to have decimal quantities this module alters the necessary forms to order certain product types with a decimal quantity. An example of this would be to purchase 4.3 metres of fabric.

Currently it is only set up to handle one unit of conversion, being metres, however it can be improved to handle any other unit of measurement. All it does is multiply out the decimals to store in the database and then divide back in to decimals for display to the user.

I would really appreciate any feedback and would like to know if anyone experiences any issues with it.

PS. Read the README.txt

Related threads: 

PreviewAttachmentSize
uc_decimal_quantities-20100816.zip13.43 KB
uc_decimal_quantities-20100817.zip13.33 KB
uc_decimal_quantities-20100914.zip14.53 KB
uc_decimal_quantities-20101014.zip14.52 KB
Wonder95's picture
Offline
Joined: 02/20/2008
Juice: 283
Forms for all types being modified

All product types are being returned as valid from _product_allows_decimal_quantities() because in_array() is returning as valid since the node type is a key, even though it has a value of 0. I changed

return in_array($product->type, $allowed_types);

to

  if($allowed_types[$product->type]) {
    return TRUE;
  }
  else {
    return FALSE;
  }

and it works fine.

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Re: Forms for all types being modified

Thanks Wonder95

This actually was caused by another issue due to lack of testing. The configuration page (which also had the wrong path) only was showing user generated content types and not the default "product" type. Since I only ever had one of these it only ever saved that value for me. I have updated the code to call array_filter on the form submission to remove all the unchecked product types from the uc_decimal_quantities_product_classes variable.

Please download the latest code and re-save your uc_decimal_quantities configuration page.

rusky's picture
Offline
Joined: 11/17/2010
Juice: 11
quantity fields

Greetings,
Thanks so much for developing this module -- it's going a long way to solving our wish list. We're trying to implement this for a quilting fabric seller -- and we've got it working to a certain extent.

Our client sells fabrics listed per metre, and now we can divide these up into the common lengths of fat quarter and half metres.
So that's great and working fine as long as the product is added to the cart from the individual product nodes.

HOWEVER, if a user selects any of the other products that are not supposed to be altered by this module, (the class is not ticked in the decimal options page, so theoretically should not be affected at all -- eg books, bags etc) --- unfortunately their quantities are multiplied by 100.

So for all other Add to cart submit buttons, it seems to multiply the quantity of non-class items by 100.

Any help/suggestions would be gratefully received ... we're almost there!!!

many thanks
Donna
New Zealand

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Latest version

Did you try uc_decimal_quantities-20101014.zip ? This update should fix that problem. I really ought to release this module properly.

rusky's picture
Offline
Joined: 11/17/2010
Juice: 11
decimals for non-fabric products

Thanks for replying so quickly,
I've attached a screenshot of the error we are getting -- and I can confirm we did upload the latest version and resaved the config page and cleared caches etc.
For some reason it is treating all products as a fabric although we the others under a different class.

It's almost like it is just missing one little checkpoint.

As I said before, it works fine for all products if we click on add to cart from each individual node -- but throws a wobbly if we choose an item from a catalog block in the sidebar.

See what you think.

best
Donna (Rusky)

AttachmentSize
shoperrorscreenshot.jpg 281.19 KB
mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Try this

Thanks for pointing it out. You may need to replace this function, as previously it wasn't calling _product_allows_decimal_quantities()

function uc_decimal_quantities_product_node_form_submit($form, &$form_state) {
  if (_product_allows_decimal_quantities($form_state['values']['nid'])) {
    $form_state['values']['cost'] /= UC_DECIMAL_QUANTITIES_FACTOR;
    $form_state['values']['list_price'] /= UC_DECIMAL_QUANTITIES_FACTOR;
    $form_state['values']['sell_price'] /= UC_DECIMAL_QUANTITIES_FACTOR;
  }
}

Which prompted me to look at this function

function uc_decimal_quantities_buy_it_now_form_submit($form, &$form_state) {
  $form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], UC_DECIMAL_QUANTITIES_FACTOR,  module_invoke_all('add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
}

I would have thought UC_DECIMAL_QUANTITIES_FACTOR should read UC_DECIMAL_QUANTITIES_FACTOR * $form_state['values']['qty'] and the whole lot should be wrapped in the same if statement from above.

So that would read

function uc_decimal_quantities_buy_it_now_form_submit($form, &$form_state) {
  if (_product_allows_decimal_quantities($form_state['values']['nid'])) {
    $form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], UC_DECIMAL_QUANTITIES_FACTOR * $form_state['values']['qty'],  module_invoke_all('add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
  }
}

PS. These mods are untested.

Are you able to test those out and let me know if that fixes it?

rusky's picture
Offline
Joined: 11/17/2010
Juice: 11
testing results

Hey mstrelan,

Thanks for the suggested code changes - we've been testing those suggestions, and the effects are:

When we add a fabric class from a node, the correct quantity is added.
When we click to continue shopping and add a different product not of the same decimal class, eg a book, it is added correctly -- but then the existing fabric product is divided by 100.
Each time a new product is added the existing fabric is divided by 100 -- so of course it drops out of the cart when it becomes less than 1.

We had added in a redirect, because it wasn't going back to the shopping cart for a non-fabric (decimal) item.
However, we haven't done it very well because the quantities are coming at 0.... *sigh*

Can you take another look please?
The URL for our client's shop is www.applebasketquilts.co.nz/homepg
Many thanks
Donna and Carla

***********snip********************

function uc_decimal_quantities_buy_it_now_form_submit($form, &$form_state) {
if (_product_allows_decimal_quantities($form_state['values']['nid'])) {
$form_state['redirect'] =
uc_cart_add_item($form_state['values']['nid'], UC_DECIMAL_QUANTITIES_FACTOR * $form_state['values']['qty'], module_invoke_all('add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
}
else {
$form_state['redirect'] =
uc_cart_add_item($form_state['values']['nid'], $form_state['values']['qty'], module_invoke_all('add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
}
}

ferrum's picture
Offline
Joined: 04/11/2010
Juice: 20
workaround for avoiding decimal quantities

another solution for avoiding decimal quantities is to reduce the unit to the smallest value which should be sold. Instead of using a base price / meter (or kilogram...) use a base price for centimeter or millimeter (or gram) for price calculation even in case the legal regulation says you have to show the price in currency unit per meter (kilogram). This can be done additionally in the product description.

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
@ferrum

Yes, this is true. Although not particularly user friendly, particularly when an item might be less than $0.01 per gram / millimetre. This is EXACTLY what this module does, but behind the scenes.

rusky's picture
Offline
Joined: 11/17/2010
Juice: 11
sorted

Hey Michael,

OK we've had a eureka moment and it's all sweet now.

Have attached the code so you can see the changes.... (had to change it to txt to upload it)

Apart from implementing your changes:

in line 108 we added a bit to allow continue shopping buttons to save the quantities and prices correctly.

and about line 348 we changed from $price to $price_info to bring the prices through to the review pane (they had been showing as zero).

Thanks for developing this very handy module.

best
Donna and Carla

AttachmentSize
uc_decimal_quantities.module.txt 18.39 KB
leosapiens's picture
Offline
Joined: 11/24/2010
Juice: 10
..

..

despi's picture
Offline
Joined: 05/03/2011
Juice: 3
Translations of 'meter' and 'meters'

Hi mstrelan,

I like your module. It helped me a lot with a fabric shop. Thank you very much.

As my shop is for client from Czech Republic I needed to translate strings 'meter' and 'meters' and I couldn't do it.
This tiny little bug in uc_decimal_quantities.module can be fixed easily by adding the t() function into the code bellow.

Current code:

define('UC_DECIMAL_QUANTITIES_UNIT_NAME', 'meter');
define('UC_DECIMAL_QUANTITIES_UNIT_NAME_PLURAL', 'meters');

New code:

define('UC_DECIMAL_QUANTITIES_UNIT_NAME', t('meter'));
define('UC_DECIMAL_QUANTITIES_UNIT_NAME_PLURAL', t('meters'));
mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Translation

Hi Despi,

It is generally not a good idea to use the return value of a function in a constant definition. I think it would be much better if the unit name and unit name plural were configurable settings with in uc_decimal_quantities using Drupal's variable_get() and variable_set() functions. I don't have time to work on this at the moment, but if anyone wants to right a patch I will happily put up a new version.

Michael

Stan79's picture
Offline
Joined: 07/25/2011
Juice: 17
Re: Decimal Quantities

If you want to use code from comment #10, remove string 108

If you have a string in module file, you will see errors like
warning: array_unshift() [function.array-unshift]: The first argument should be an array in /var/www/mysite/sites/all/modules/ubercart/contrib/uc_decimal_quantities/uc_decimal_quantities.module on line 108.

alfthecat's picture
Offline
Joined: 06/11/2010
Juice: 14
Hi, After enabling the latest

Hi,

After enabling the latest version of the module I'm getting a fatal error when I visit a product page:
Fatal error: Call to undefined function dpm() in [... ]modules/uc_decimal_quantities/uc_decimal_quantities.module on line 473

It would really be great to get the quantity field to accept decimals, hopefully someone has some suggestions.

Thanks in advance!

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Easy

alfthecat - you can safely delete the line in uc_decimal_quantities.module with the dpm() function call in it.

The dpm function is used for developers to debug modules, and requires the devel module. If you ever see dpm() errors it's because the developer accidentally forgot to remove them.

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
Decimal Quantities and Stock

Hi mstrelan,

Should your module work with the stock module? I'm using it in a fabric website that sells quantities in half metres. I've implemented stock functionality using the out of stock notification module which all seems to work well with the half metre functionality up until I click review order in the checkout page. As an example, if my current stock of a particular fabric is 3 metres and I have 3 metres in my basket when I click review order I get an error message saying that only 3 units are in stock but I have 6 units in my shopping cart. The system requires 6 half metre units to complete the order.

Have I done anything wrong? Can you provide any support on this matter please?

Thanks in advance.

Gav

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
Re: Decimal Quantities and Stock

Also, has anyone had problems with this module and Ubercart 6.x-2.7? I updated Ubercart and got errors with Decimal Quantities so reverted to my previous version (6.x-2.3).

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Stock module

Hi gav643,

I have not tested this with the stock module, nor do I expect it to work. If you want to try it out you would need to enter the stock in centimetres rather than metres, as behind the scenes the decimal quantities module is converting everything to centimetres (dividing the price and multiplying the quantity by 100).

I have not tested this module with versions of Ubercart newer than when I posted it, so feel free to suggest any patches.

Regards,
Michael

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
Hi Michael, Thanks for your

Hi Michael,

Thanks for your quick response. The fabric is being sold by the half metre so listing it as centimetres may cause a problem with converting it to the nearest half metre. Setting the UC_DECIMAL_QUANTITIES_FACTOR to 2 does this automatically. The only way I can think of solving this is to intercept the input number and times it by 2 and if quantity of stock was set to double the two would marry up nicely. I'm not sure how easy this would be and it seems like a bit of a hack.

I think I'll post this out to the wider community but if you have any more suggestions then I would be grateful.

Cheers,

Gav

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Stock

Hi Gav,

The whole concept of uc_decimal_quantities is to catch input and multiply it, then catch output and divide it, so your ideas are no more hacky than the module itself. Ideally uc_decimal_quantities should provide support for stock but my client didn't need it, so if you want to work on factoring that in to the module I will post your changes up here.

Michael

alfthecat's picture
Offline
Joined: 06/11/2010
Juice: 14
Hey guys, Just thought I'd

Hey guys,

Just thought I'd share that I got the decimal quantities working perfectly after re-creating the steps that originally spawned this discussion and resulting module. The latter didn't quite work for me, probably because of a conflicting version of uc core on my stack.

What I did was recreate these (super wonderfully documented by the way) steps found here: http://www.ubercart.org/issue/6044/abiility_have_decimal_quantities#comm...
The exact line numbers might differ off course as versions have passed after the post was written and off course it's for those who want decimals kinda nowish, for the neatest implementation of this functionality would be through a stable uc_decimal_quantities module.

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
Decimal Quantities and Secure Trading

Hi all,

I'm having a problem with the Decimal Quantities and Secure Trading modules. When a payment is made in the Secure Trading site and the completed callback is made to my Ubercart site some information in the order gets changed - the quantity gets halved and price gets doubled. I've tried disabling the Decimal Quantities module and working with whole quantities is fine.

Please can anyone help? I can provide more information if necessary.

Thanks in advance.

Gav

Wombasta's picture
Offline
Joined: 07/19/2012
Juice: 16
That worked for me in Drupal 7 ubercart 3.1

I've had a long way to solve the decimal problem in ubercart 3 with no luck in searching the web so that's why i feel indebted to help others Smiling
I know that hacking the module core files is a bad idea but still it is the only way that worked for me in Drupal 7 ubercart 3.1 to make possible to buyers to purchase decimal quantities of products. Make sure that module updates are turned off on your site so the changes weren't cleared when new version of ubercart will be available.
This solution very similar to what people were doing on ubercart 6 with some changes. If anybody will find some bugs - please let us know here.
Solution is in the attached text file.

AttachmentSize
_ubercart3_core_changes_for_decimal_qty.txt 4.71 KB
tcclindy's picture
Offline
Joined: 05/10/2008
Juice: 32
Shipping Bug

Thanks for your contributions, I just wanted to point out that there is a bug in the shipping module. The shipping weights are only correct if you have more than one product in your cart, otherwise it goes through some extra code and loses the fractional portion, for our business model I simply changed the it to >= 1 which nullifies the else clause in following in the function listed below.

This shipping module is very convoluted for usps shipping, way too complicated in my opinion. Not sure of he benefit of the else clause running.

If you dont make this change your shipping quotes are underestimated by up to .99% of your item weight.

function _uc_usps_package_products($products, &$addresses) {
$last_key = 0;
$packages = array();
if (variable_get('uc_usps_all_in_one', TRUE) && count($products) >= 1) {