Abiility to have decimal quantities

Project:Ubercart Contributions
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description
Project: 
Ubercart

Many stores sell items that are by the lbs, meter, yard, foot, etc .... requiring decimal quantities such as 1.4 meter or 2.5 lbs. Most e-cart offer the functionality to turn decimal quantities ON or OFF. Adding this feature would bring in more users to ubercart.

This is really required - you can build a cart that doesn't support decimal quantities.

Danny

Dan-902's picture
Offline
Joined: 06/29/2008
Juice: 69
decimal quantities

Bump...

cYu
cYu's picture
Offline
Bug FinderGetting busy with the Ubercode.
Joined: 11/19/2007
Juice: 852
Re: decimal quantities
Dan-902's picture
Offline
Joined: 06/29/2008
Juice: 69
Fractions
Assigned to:cYu» Dan-902

Many ecart can do this - I'am suggesting that ubercart should be able to it as well. I can do a work around like buy xx cm of fabric versus 1 meter... but the customers would get confused and it just doesn't look professional.

I think that only allowing integers is too limiting and should be reconsidered - if we want more people to use Ubercart.

Danny

robdinardo's picture
Offline
Joined: 02/01/2009
Juice: 27
Code and Database Changes
Assigned to:Dan-902» robdinardo

Ubercart 2.0 – Fractional Quantities (By Rob DiNardo)

In some cases, it is necessary to sell products in non-integer quantities. Examples: a fabric store or butcher shop should be able to sell in 2.4 m of fabric or 3.75 kg of meat. The current Uberart (v 2.0) does not allow decimals for their quantities.

If you seach the Ubercart forums for "quantity as decimal", "fractions for quantity", and "decimal quantities" you get some hits. This article is an effort to outline some changes that may be made to your installed Ubercart system to allow for "fractional quantities".

Thank you to Lyle and his post reply that helped me get started on this article: http://www.ubercart.org/forum/ideas_and_suggestions/2510/quantity_decimal

Add To Ubercart Core?
I hope that the Ubercart developers can find a way to implement the ability for “fractional quantities” to Ubercart Core. I hope that this document / article will help make that happen!

Database Changes
Changing Ubercart to accept fractional quantities means the datatype of some table columns must be changed from INTEGER to FLOAT(M,D). The FLOAT data type allows for the decimal to be stored. Here is a description from http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

Here, “(M,D)” means than values can be stored with up to M digits in total, of which D digits may be after the decimal point. For example, a column defined as FLOAT(7,4) will look like -999.9999 when displayed. MySQL performs rounding when storing values, so if you insert 999.00009 into a FLOAT(7,4) column, the approximate result is 999.0001.

Below are the changes to the tables to be made to allow 2 decimal places and 6 total digits. Applying the following database table changes should not affect existing data, unless you have quantities greater than 6 digits – in which case you could increase the M value.

UC_CART_PRODUCTS
The UC_CART_PRODUCTS table intersect the UC_CARTS table and the UC_PRODUCTS table. The column “qty” is the one to ajust. Here is the MySQL statement that will modify the column to a FLOAT data type:

ALTER TABLE `uc_cart_products` MODIFY COLUMN `qty` FLOAT(6,2) UNSIGNED NOT NULL DEFAULT 0;

UC_ORDERS
The UC_ORDERS table holds all the orders created. The column to adjust is “product_count”.

ALTER TABLE `uc_orders` MODIFY COLUMN `product_count` FLOAT(6,2) UNSIGNED NOT NULL DEFAULT 0;

UC_PRODUCTS
The UC_PRODUCTS table contains a default_qty field. This value gets inserted into both the product edit page and the product view page (for the customer). I think it would be a good idea to show the customer how many decimal places one may use.

ALTER TABLE `uc_products` MODIFY COLUMN `default_qty` FLOAT(6,2) UNSIGNED NOT NULL DEFAULT 1.00;

UC_ORDER_PRODUCTS
The UC_ORDER_PRODUCTS table intersects the UC_ORDERS table and the UC_PRODUCTS table. The column to adjust is the “qty” column. Here is the MySQL statement that will modify the column to a FLOAT data type:

ALTER TABLE `uc_order_products` MODIFY COLUMN `qty` FLOAT(6,2) UNSIGNED NOT NULL DEFAULT 0;

Code Changes
Making changes to core Ubercart code is not recommend. It is recommended to create modules (with seperate database tables) to add custom functionality. These code changes may not be complete and may disrupt your system. If you decide to make these changes... Please proceed with caution. I suggest that the changes

uc_cart.module (line 1445)

db_query("UPDATE {uc_cart_products} SET qty = %d, changed = UNIX_TIMESTAMP(), data = '%s' WHERE cart_item_id = %d",

to...

db_query("UPDATE {uc_cart_products} SET qty = %f, changed = UNIX_TIMESTAMP(), data = '%s' WHERE cart_item_id = %d",

uc_cart.module (line 1509)

db_query("INSERT INTO {uc_cart_products} (cart_id, nid, qty, changed, data) VALUES ('%s', %d, %d, %d, '%s')", $cid, $node->nid, $qty, time(), serialize($data));

to...

db_query("INSERT INTO {uc_cart_products} (cart_id, nid, qty, changed, data) VALUES ('%s', %d, %f, %d, '%s')", $cid, $node->nid, $qty, time(), serialize($data));

uc_order.module (line 1043)

db_query("UPDATE {uc_orders} SET uid = %d, order_status = '%s', order_total = %f, product_count = %d, primary_email = '%s', "

to...

db_query("UPDATE {uc_orders} SET uid = %d, order_status = '%s', order_total = %f, product_count = %f, primary_email = '%s', "

uc_order.module (line 1143)

db_query("UPDATE {uc_orders} SET product_count = %d WHERE order_id = %d", $count, $order->order_id);

to...

db_query("UPDATE {uc_orders} SET product_count = %f WHERE order_id = %d", $count, $order->order_id);

uc_order.install (replace lines 48 to 51)

'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,

with...

'type' => 'float',
'precision' => '6',
'scale' => '2',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1.00,

uc_product.module (line 1207)

db_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE nid = %d AND cart_id = '%s' AND data = '%s'", $qty, time(), $nid, $cid, serialize($data));

to...

db_query("UPDATE {uc_cart_products} SET qty = %f, changed = %d WHERE nid = %d AND cart_id = '%s' AND data = '%s'", $qty, time(), $nid, $cid, serialize($data));

Thoughts - Additional Changes
It would probably be a good idea to add some functionality to allow a choice as to whether or not a certain product type / class is allowed to accept "fractional quantities". Maybe the addition of a boolean column (like "allow_frac_qty") in the uc_product_classes table or the uc_products table. Then of course, there would be more code additions / changes to allow for this.
Also, the "pkg_qty" column in the uc_products table may also need to be changed!

Other Forum Posts
http://www.ubercart.org/forum/support/4651/use_fractions_quantity_15_yards
http://www.ubercart.org/forum/support/6074/decimal_quantities_items
http://www.ubercart.org/issue/6044/abiility_have_decimal_quantities
http://www.ubercart.org/forum/ideas_and_suggestions/3283/comma_values_qu...

-- end, last updated: 2009-11-13

AttachmentSize
Ubercart - Fractional Quantities.pdf 110.28 KB
mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Workaround without hacking Ubercart
Assigned to:robdinardo» mstrelan

Hi,

I'm after opinions, I'm trying to implement a workaround which doesn't require hacking Ubercart. The client sells fabric and therefore needs to list prices per metre, but allow customers to purchase say 3.14m.

My idea is to have the admin enter the price (per metre) and divide it by 100 so the stored price is per centimetre. This requires a custom submit handler for product_node_form.

Then when displaying the product I am implementing hook_price_handler_alter which multiplies the price by 100 when $context['type'] is 'product'. I also append "per metre" to the price.

Finally for cart and checkout I add custom submit handlers for uc_cart_view_form and uc_product_add_to_cart_form_n which multiply the entered quantities by 100. So when the customer orders 3.14 metres really they order 314 centimetres.

I'm sure there are other places I will need to address, such as invoices, order history etc. I am also considering making this in to a module which is configurable to use for weight / length / certain product conditions.

What are your thoughts / feedback?

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

- hook_order if $op is 'load' need to multiply price by 100, divide quantity by 100.
- need to theme cart_review_table to show divide $item->qty and format it with 2 decimals and the metre suffix

Then it looks like its reasonably complete, except one part i haven't managed to override is uc_checkout_pane_cart.

customer.itpl.php can also be modified so instead of "$3.00 each" it says "$3.00 per metre"

j.mead's picture
Offline
Joined: 07/27/2009
Juice: 385
metric only i presume...
Assigned to:mstrelan» j.mead

Are you attempting to implement this only for metric measurements? My experience with fabric sellers and their customers has been that many expect the measurements to be in yards. And then fractions of those yards in terms of thirds, or eighths as this has been standard practice for years. At least for people not accustomed to using the metric system.

Also does your client only sell fabric, or are they also looking at selling non-divisible items such as say scissors or spools of thread. Will this be the default for 'products' and they adjust pricing for other types as well or would they set up extra classes for single unit items. Scissors at "$3.00 per metre" might look a little odd.

Really would love to see a more elegant way of doing this than how a lot of people have come up with, ie. single yard units with 9 extra measurement attributes. (I think part of the key for yards would be having 3 decimal places so that 1/8 = 0.125)

I hope you figure it out even if it's only metric. Universal adoption in the past would have made a lot of things easier, now though it would be like Y2k all over.

the sites i'm always breaking.... www.sew-la-fabric.com
http://lostpetsla.com (though i hope i never break this one too bad)

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
thanks for the feedback
Assigned to:j.mead» mstrelan

Fortunately for me the store / target audience is Australia where the metric system is widely accepted, most of us don't even know the imperial scale.

I have a constant (which could be a configurable variable) for the factor and for the unit. I suppose you could set the factor to 12 and list the price per foot and store it as inches. Or 36 and list it as price per yard. It doesn't even make sense to me why anyone would want to order an eighth of a foot or of a yard I mean what even is that? 4.5 inches? What a strange system. Anyway I guess I could have handlers for that, like 3 different inputs, yards, feet, inches and they can type 3 1 6 to get 3 yards 1 foot and 6 inches and then it's just converted to inches. But thats something I'm not going to code as it does not concern me.

My module allows the admin to choose which product classes allow decimal quantities, and theoretically could have a different unit per class, eg one for metres, one for kilograms.

If you or anyone is interested I've attached my work in progress. Ideally it would be great if there was a better way to do this in core but this seems to be working for the time being.

--- Update 4 Dec 2009 ---
Attached a new version with slightly better code and now a block to replace the default cart block

AttachmentSize
uc_decimal_quantities.zip (older) 3.81 KB
uc_decimal_quantities.zip (now includs block to replace default cart block) 5.85 KB
beb
beb's picture
Offline
Joined: 01/16/2010
Juice: 2
thank you for the module!
Assigned to:mstrelan» beb

I've only just begun testing, but this seems to have solved a problem I was having recording time! A "product" in my case is a block of time and is recorded in 10ths of an hour according to a Hobbs meter (for example "1.7 hrs"). I changed the unit and factor and it was very close what I was envisioning. Many thanks!

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
Updated version
Assigned to:beb» mstrelan

Hi beb,

I'm extremely happy my module has helped you out. Please find attached this updated version which includes a few extra modifications including invoices etc. Read the README.txt for more instructions.

AttachmentSize
uc_decimal_quantities.zip 14.18 KB
Dan-902's picture
Offline
Joined: 06/29/2008
Juice: 69
Core
Assigned to:mstrelan» Dan-902

This should definitely be part of the core ubercart modules. Great work

cristianmos's picture
Offline
Joined: 01/18/2010
Juice: 20
futher developing for weight support
Assigned to:Dan-902» cristianmos

hi there, really excited about your module. Any chance of further developing for weight support?

mstrelan's picture
Offline
Joined: 12/02/2009
Juice: 73
@cristianmos At the moment I
Assigned to:cristianmos» mstrelan

@cristianmos

At the moment I don't have a lot of time to work on this. For now you can simple edit uc_decimal_quantities.module and change the following code:

define('UC_DECIMAL_QUANTITIES_FACTOR', 100);
define('UC_DECIMAL_QUANTITIES_UNIT', 'm');
define('UC_DECIMAL_QUANTITIES_UNIT_NAME', 'metre');
define('UC_DECIMAL_QUANTITIES_UNIT_NAME_PLURAL', 'metres');
define('UC_DECIMAL_QUANTITIES_STORED_UNIT', 'cm');

I might get around to setting this up as a contribution if I get time.

cristianmos's picture
Offline
Joined: 01/18/2010
Juice: 20
Re: @cristianmos At the moment I
Assigned to:mstrelan» cristianmos

Thank you for the quick answer. I already thought of doing that. Nevertheless I appreciate your effort in building this add-on for ubercart.

100IT's picture
Offline
Joined: 02/14/2010
Juice: 19
#15
Category:» feature request
Priority:normal» critical
Assigned to:cristianmos» Guest

One could easily regard the absent of this feature as a major drawback in choosing Ubercart. I vote to put this 'feature' in the core asap because of its wide demand and possibilities.
Any progress here?

dudy's picture
Offline
Joined: 02/16/2010
Juice: 7
#16
Category:feature request» bug report
Priority:critical» normal

Hi mstrelan,

I have found a little bug in your code. while checking out - price was always 0.
FIX (in function theme_uc_decimal_quantities_cart_review_table):

$base_array = array(
$quantity_cell,
array('data' => $description, 'class' => 'products'),
// Original:
// array('data' => uc_price($price, $context), 'class' => 'price'),
// New:
array('data' => uc_price($price_info, $context), 'class' => 'price'),
);

Please apply this fix if you have a zero amount in checkout form.

Regards,
Dudy

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

Thanks for picking this up. I must admit I have only tested this using the GST or VAT modules as that is the specific use case I have been interested in.

pgacv2's picture
Offline
Joined: 03/19/2010
Juice: 39
#18

I vote for this. I also read Lyle's post at http://www.ubercart.org/comment/10001/Re_Quantity_Decimal (link provided by cYu at the top of this thread) and I think his concern about rounding errors with floats, while technically valid, doesn't really apply to the kind of scenarios Ubercart will commonly be used in. Rounding errors are typically several decimal places to the right, and once you get past the hundredths place in a decimal number, it's pretty hard to tell the difference, either visually or with any widely available measuring instrument. If someone orders 1.5 yards of fabric, and they actually end up getting 1.489023481 or whatever, do you really think the customer will notice or even care? Of course there are times when even that small margin of error is unacceptable (maybe you want to enable customers to order exactly 400.43580021 grams of enriched uranium for... whatever reason), but if you're in that situation, you wouldn't be using Ubercart for your selling needs anyway.

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

@pgacv2 - you might like to try this out http://www.ubercart.org/project/uc_decimal_quantities and see if it works for you

pgacv2's picture
Offline
Joined: 03/19/2010
Juice: 39
#21

Thanks for your work on that module, mstrelan. My client doesn't need decimals in the site I'm building for him (knock on wood), but I'll keep it in mind for future work. I just think Ubercart should support this kind of thing out of the box. Maybe the devs can merge some of your code into the Ubercart core?

Dan-902's picture
Offline
Joined: 06/29/2008
Juice: 69
#22

I would vote for this to be in Ubercart core. Please provide a link for me to do so.

mstrelan Thanks for the module... I plan on testing in the near future.

Danny

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#23

I am using this contributed module and I am getting the following error when I get to the checkout page:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'theme_uc_decimal_quantities_cart_review_table' was given in /home/theswedi/public_html/includes/theme.inc on line 656.

Everything else seems to be working fine and this seems to be a great module but I don't understand what this means.

Can anyone help?

I am running Drupal 6.16 and Ubercart 2.0. I have followed the instructions in the readme file.

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

Hi gav643,

Try Flush all caches. This can be done at Admin > Site configuration > Performance, right down the bottom. See if that works. Otherwise let me know and I'll investigate further.

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#25

Hi mstrelan,

Thanks for your help. I've tried clearing all cached data but it doesn't work. Do you have any other ideas?

Thanks.

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#26

Hi mstrelan,

This problem doesn't seem to cause any major issues with the checkout process, the only thing I notice is that the information in Cart contents (including decimal quantities) does not display. However, when the user gets to the Review order page the information in this pane is displayed. Does this make sense to you as I still don't know what to do about it?

Thanks,

Gav

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

Can you please try install the uc_vat module and see if you still get the problem? I have never tested this without uc_vat or uc_gst. Also ensure that the database weight for uc_decimal_quantities is higher than for uc_vat. This can be edited in the system table.

If this solves the problem I will fix it to work without uc_vat

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#28

I've installed the uc_vat module and checked the weight of both of these using the Utils module. The module weight for uc_vat is 0 and the weight for Decimal Quanities is 3. I've tried clearing all caches again but I I'm still getting the same problem.

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#29

Hi mstrelan,

Have you had a chance to look at getting the module to work without the uc_vat module? Is there anything else I can do or do to help?

Thanks,

Gav

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

Gav - are you on PHP4 or PHP5? This could be relevant.

Also in your THEME_preprocess_page() function can you please add

  $registry = theme_get_registry();
  drupal_set_message('<pre>'.print_r($registry, 1).'</pre>');

Then load up a page (twice)

You should see something like this somewhere in this print r

    [cart_review_table] => Array
        (
            [arguments] => Array
                (
                    [show_subtotal] => 1
                )

            [type] => module
            [theme path] => sites/all/modules/ubercart/uc_cart
            [function] => theme_uc_decimal_quantities_cart_review_table
            [include files] => Array
                (
                )

            [theme paths] => Array
                (
                    [0] => sites/all/modules/ubercart/uc_cart
                )

            [preprocess functions] => Array
                (
                    [0] => template_preprocess
                )

            [file] => sites/all/modules/uc_decimal_quantities/uc_decimal_quantities.module
        )

Please post any differences between yours and that

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#31

I'm running PHP 5.2.13

This is the code from the print r:

[cart_review_table] => Array
        (
            [arguments] => Array
                (
                    [show_subtotal] => 1
                )

            [type] => module
            [theme path] => modules/ubercart/uc_cart
            [function] => theme_uc_decimal_quantities_cart_review_table
            [include files] => Array
                (
                )

            [theme paths] => Array
                (
                    [0] => modules/ubercart/uc_cart
                )

            [preprocess functions] => Array
                (
                    [0] => template_preprocess
                    [1] => skinr_preprocess
                )

        )

---------------------

This is extra:

[1] => skinr_preprocess

And something similar to this is missing:

[file] => sites/all/modules/uc_decimal_quantities/uc_decimal_quantities.module

Thanks for your help on this.

Gav

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

Hi Gav,

Please try edit uc_decimal_quantities.module and add the following to uc_decimal_quantities_theme_registry_alter()

$registry['cart_review_table']['file'] = drupal_get_path('module', 'uc_decimal_quantities') .'/uc_decimal_quantities.module';

Then try flush the cache and see if it works. If not, please try disable skinr module and flush cache and try again.

Cheers,
Michael

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#33

Hi Michael,

This still hasn't worked. My print r is now the same as yours:

[cart_review_table] => Array
        (
            [arguments] => Array
                (
                    [show_subtotal] => 1
                )

            [type] => module
            [theme path] => modules/ubercart/uc_cart
            [function] => theme_uc_decimal_quantities_cart_review_table
            [include files] => Array
                (
                )

            [theme paths] => Array
                (
                    [0] => modules/ubercart/uc_cart
                )

            [preprocess functions] => Array
                (
                    [0] => template_preprocess
                )

            [file] => modules/uc_decimal_quantities/uc_decimal_quantities.module
        )

Do you have any more ideas?

Thanks,

Gav

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

hmm ... try a different theme? create another site from scratch and start adding the modules you've got until it breaks? PM me with your site login details and I could take a quick look

satrapa's picture
Offline
Joined: 08/13/2010
Juice: 7
#35

I think the problem is in uc_decimal_quantities.module line 212. The name of this function would be theme_uc_decimal_quantities_cart_review_table instead decimal_quantities_cart_review_table. It worked to me, but I'd like to mstrelan confirm us that this aproach don't break another thing.

And of course... thank you very much for the plugin. It's a great job.

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

satrapa - You're absolutely correct. I have updated the zip on http://www.ubercart.org/project/uc_decimal_quantities I'm not quite sure how the wrongly named function got there in the first place, since older versions have the correct name. Thanks for spotting this and I'm glad this module has been helpful to you.

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

If anyone has had problems with invoice templates in ubercart 2.3 and above please download the latest zip from http://www.ubercart.org/project/uc_decimal_quantities

This version alters the theme registry to look for invoice templates in the uc_decimal_quantities/templates directory, so you no longer have to copy and paste files in to the uc_order directory. You can also override these templates by copying theme from uc_decimal_quantities/templates to your theme directory.

gav643's picture
Offline
Joined: 06/27/2010
Juice: 48
#38

mstrelan and satrapa,

Thanks for fixing in this bug. It's definitely an incentive to get more involved in PHP and Drupal.

Gav

satrapa's picture
Offline
Joined: 08/13/2010
Juice: 7
#39

I found another little problem. When you add products to cart from product list (i.e. front page) you cannot add quantities minor to 1 (i.e. 0.5, or 0.75) because in uc_product is an uc_product_add_to_cart_form_validate function.

I wrote a new function validator at the bottom of uc_decimal_quantities.module to fix this problem.

/**
* Allow only positive, numeric quantities.
*
* @see uc_product_add_to_cart_form()
*/
function uc_decimal_quantities_add_to_cart_form_validate($form, &$form_state) {
  if (!_product_allows_decimal_quantities($form_state['values']['node'])) {
    $checkpositive = intval($form_state['values']['qty']);
  }
  else {
    $checkpositive = $form_state['values']['qty'];
  }

  if (!is_numeric($form_state['values']['qty']) || $checkpositive <= 0) {
      form_set_error('qty', t('You have entered an invalid quantity.'));
  }
}

To change to the new validator you'll need to add this line on uc_decimal_quantities.module (line 133):

$form['#validate'][0] = 'uc_decimal_quantities_add_to_cart_form_validate';

I paste the line in its context to explain me better Eye-wink

  if (strpos($form_id, 'uc_product_add_to_cart_form_') === 0) {
    // Alter display of decimal quantity add to cart forms
    $nid = substr($form_id, strrpos($form_id, '_') + 1);
    if(_product_allows_decimal_quantities(node_load($nid))) {
      $form['qty']['#title'] = t('Amount Required');
      $form['qty']['#default_value'] = number_format($form['qty']['#default_value'], 2);
      $form['qty']['#description'] = UC_DECIMAL_QUANTITIES_UNIT_NAME_PLURAL;
    }
    // On save multiply the number that was input to get the stored quantity
    array_unshift($form['#submit'], 'uc_decimal_quantities_cart_view_form_submit');
    $form['#validate'][0] = 'uc_decimal_quantities_add_to_cart_form_validate';
  }
  elseif (strpos($form_id, 'uc_catalog_buy_it_now_form_') === 0) {
    $form['#submit'] = array('uc_decimal_quantities_buy_it_now_form_submit');
  }

I'm new on Drupal so i don't know if it is the better way to redefine this... so i'll apreciate feedback. I hope this will be helpful.

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

Hey satrapa,

Thanks for this, it looks great. The only tip I would suggest is that you can't be certain that the current validation callback is going to be at index 0 of the array, another module might alter that. I would rewrite it like this.

<?php
   
// Override the default validate function, if it exists, otherwise add ours to the end.
   
$validate_key = array_search('uc_product_add_to_cart_form_validate', $form['#validate']);
    if (
$validate_key === FALSE) {
     
$form['#validate'][] = 'uc_decimal_quantities_add_to_cart_form_validate';
    }
    else {
     
$form['#validate'][$validate_key] = 'uc_decimal_quantities_add_to_cart_form_validate';
    }
?>

I would be really grateful if you could test this for me, thanks!

satrapa's picture
Offline
Joined: 08/13/2010
Juice: 7
#41

Hi mstrelan,

Thanks for your advice. I tested your code and works perfectly.

Maybe it can be improve with a finish line:

$form['#validate'] = array_unique($form['#validate']);

because if the array already has an uc_decimal_quientities_add_to_cart_form_validate, your code will duplicate this validator. But I'm not sure if this case could be really happen Eye-wink

Thank you very much for your support.

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

There should not be a situation where uc_decimal_quantities_add_to_cart_form_validate already exists in the validate array, unless there is another module using the "uc_decimal_quantities_" prefix, which seems highly unlikely.

alfthecat's picture
Offline
Joined: 06/11/2010
Juice: 14
#43

I have followed the steps from http://www.ubercart.org/node/#comment-46468, and it works perfectly everywhere except in the order admin. As an order is checked-out, the quantity is still rounded off.

Have I perhaps missed something? Hope anyone can help!

Thanks in advance.

Wombasta's picture
Offline
Joined: 07/19/2012
Juice: 16
#44

I described what had worked for me in Drupal 7, Ubercart 3.1 here:
http://www.ubercart.org/project/uc_decimal_quantities#comment-68750