Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

All sites have this bug, so heres the fix


Arpit

Recommended Posts

tep_draw_input_field('cart_quantity[]', (int)$products[$i]['quantity'], 'size="4"','onBlur="this.value = parseInt(this.value);"'

should be

tep_draw_input_field('cart_quantity[]', (int)$products[$i]['quantity'], 'size="4" onBlur="this.value = parseInt(this.value);"'

Hth,

Matt

Hi

 

I can't get this to work on my site.

 

I use Milestone2.

 

Any one know?

 

/gert

Link to comment
Share on other sites

  • Replies 54
  • Created
  • Last Reply
On line 148 of catalog/shopping_cart.php, find:
'text' => tep_draw_input_field('cart_quantity[]', (int)$products[$i]['quantity'])

 

and replace it with:

'text' => tep_draw_input_field('cart_quantity[]', (int)$products[$i]['quantity'], 'onBlur="this.value = parseInt(this.value);"')

 

This causes the field to truncate its value to an integer when the user clicks outside of the field (i.e. when s/he clicks 'Update').

I can't get it to work.. here are my original line 145 from my shopping_cart.php, it dose't look like yours??

 

btw, I use milestone2.

 

'text' => tep_draw_input_field('cart_quantity[]', $products[$i]['quantity'], 'size="4"') . tep_draw_hidden_field('products_id[]', $products[$i]['id']));

Link to comment
Share on other sites

Glad to hear that at least a few people have gotten some use out of my snippet of code...

 

Indeed, instead of making a call to parseInt(), one could write a javascript function that asked the user if they intended '1.2' or '12'. One could say that would be the polite thing to do. :)

 

I later found a more elegant way to trap all instances of quantities of products being added to the cart: handle it in application_top.php (After all, many people have added quantity fields to more pages than just shopping_cart.php).

 

Near line 329 of application_top.php, there's the code:

switch ($HTTP_GET_VARS['action']) {

There are three cases where products are added to the cart (case 'update_product', case 'add_product' and case 'buy_now'). Each of these makes a call to cart->add_cart(...), which takes as one of its arguments a quantity value. Cast that quantity value to an (int), like so (line 351):

$cart->add_cart($HTTP_POST_VARS['products_id'][$i], (int)$HTTP_POST_VARS['cart_quantity'][$i], $attributes, false);

Yes, this impolitely fails to notify the customer that you're changing their input, but it saves you the hassle of having to ship 1.2 televisions.

 

If one were really ambitious, one could create a setting in the Admin section, such as requireWholeQuantities (true/false), and call cart->add_cart(...) with or without the (int) cast, depending on how that flag was set.

 

I wish I had posted this suggestion first, but hey, I'm learning as I go.

Link to comment
Share on other sites

  • 4 weeks later...

Excellent mod :) ... thanks ... just one question ...

 

Do you need to do that just once (as you describe), or on all three of the instances in that section of application_top.php where $cart->add_cart is called ?

 

ie.

 

File "Catalog:includes:application_top.php"; Line 381:  $cart->add_cart($HTTP_POST_VARS['products_id'][$i], (int)$HTTP_POST_VARS['cart_quantity'][$i], $attributes, false);
File "Catalog:includes:application_top.php"; Line 389: $cart->add_cart($HTTP_POST_VARS['products_id'], (int)$cart->get_quantity(tep_get_uprid($HTTP_POST_VARS['products_id'], $HTTP_POST_VARS['id']))+1, $HTTP_POST_VARS['id']);
File "Catalog:includes:application_top.php"; Line 399:$cart->add_cart($HTTP_GET_VARS['products_id'], (int)$cart->get_quantity($HTTP_GET_VARS['products_id'])+1);

 

TIA

 

:rolleyes:

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

If you've followed the whole thread up till here, this is the answer to Tia's question (2 posts down):

 

Do you need to do that just once (as you describe), or on all three of the instances in that section of application_top.php where $cart->add_cart is called ?

 

You only need to put the (int) for the 'update_product' case i.e.

 

    switch ($HTTP_GET_VARS['action']) {
     // customer wants to update the product quantity in their shopping cart
     case 'update_product' : 
for ($i=0, $n=sizeof($HTTP_POST_VARS['products_id']); $i<$n; $i++) {
...
$cart->add_cart($HTTP_POST_VARS['products_id'][$i], 
                        (int)$HTTP_POST_VARS['cart_quantity'][$i], $attributes, false);

 

The reason is that this is the only instance where you are allowed to enter a non-integer number. When you 'add a product' or 'buy now' you only add 1 item.

 

A.

Link to comment
Share on other sites

now I dont know how to give a customer 2.5 of any item

 

Scissors, saws, chainsaws work well for me. Just make sure you have a 'Customization' clause at your site for a non-return/refund on custom order items.

Link to comment
Share on other sites

  • 2 months later...

You could always simply add a javascript letting the customer know they must input only numbers in the field. Tons of free scripts around the net just for this purpose. Search something like "js IsNumeric alert" in any search engine.

Link to comment
Share on other sites

One way that you could do this would be to have a field for each product for order multiples. This would do many things.

 

The default (for most products) would be 1

For fabric, the multiple would be .1

For cases of products, you specify the case amount (like 24 for example).

 

Then, the script would check the qty entered versus the quantity increment value and adjust accordingly

 

Now what if someone ordered 1.4294 yards of fabric? Could you measure this? This would limit it to 1.4, 1.5, ... yards. I'm guessing that people who sell fabric also sell regular items too.

 

Do you think this would work?

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

I also needed my script to limit the number of any one product in the cart. So they couldn't add 500 t-shirts. I changed the code listed above in application_top.php to this instead:

 

$sCart_Quantity = intval($HTTP_POST_VARS['cart_quantity'][$i]);
If ( $sCart_Quantity > 20 )
{
$sCart_Quantity = 20;
}
$cart->add_cart($HTTP_POST_VARS['products_id'][$i], $sCart_Quantity, $attributes, false);

--Dean Higginbotham

--www.aricaur.com -- sell your images online!

Link to comment
Share on other sites

The interesting 'feature' is that without these fixes, out of the box, OSC will allow you to enter a quantity of ten 9's, or more. This get's converted to x(e^10) causing the shopping cart to have totals like $1.50 for 9999999999 items. Works with nine 9's, but not more than that.

 

So by adding at least the (int) fix, you can have a legit quantity of 2147483647 items that calculates properly, one less headache. You know, for those 10 billion item sales.

 

:blink:

Riyzo

"Eruces, the god of network security."

Link to comment
Share on other sites

wow this is a great thread with all the neat ways to do things with the cart maybe you all can help me, any ideas on how to stop "0" quantity from adding the product to the cart. If the product is not already in the cart and i put in "0" for the qty, it gets added. If already in the cart, adding "0" appears to do nothing. This is driving me crazy, is anyone else having this issue/feature problem. I need to figure out why it is adding a "0" quantity product to the cart from the product listing pages. thanks

Link to comment
Share on other sites

I have been following this thread and found it very usefull. The Shop I maintain sells fabric by the meter. It works if someone buys 1.5 meter but it doesn't accept less than 1 meter. (EG 0.2 meters)

It then says the cart is empty.

This must be something easy but I can't seem to find it. Anyone some clues?

 

Thanx,

 

Remko

Link to comment
Share on other sites

  • 1 month later...

My shop sells fabrics aan needs to sell parts of a meter. (eg. 1.5 meter)

 

To accomplish this I changed 2 database tables.

 

The first table

 

orders_products

 

I changed the field products_quantity from INT to Decimal (15,3)

 

The second table

 

customers_basket

 

I also changed the field customers_basket_quantity from INT to Decimal (15,3)

 

 

This second table is when users are not logged in yet and add products to there cart.

 

If you need this I recommend to backup your database first. I am just an amateur in this so I might have overlooked something.

 

Remko

Link to comment
Share on other sites

  • 1 month later...
I actually modified the update_quantity() method in the shoppingCart class to convert the $quantity to an int before processing. 

 

$quantity = (int)$quantity;

 

 

Where exactly did you modify this? And what did you enter?

 

    function update_quantity($products_id, $quantity = '', $attributes = '') {
     global $customer_id;

     if (empty($quantity)) return true; // nothing needs to be updated if theres no quantity, so we return true..

     $this->contents[$products_id] = array('qty' => $quantity);
// update database
     if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET . " set customers_basket_quantity = '" . $quantity . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "'");

     if (is_array($attributes)) {
       reset($attributes);
       while (list($option, $value) = each($attributes)) {
         $this->contents[$products_id]['attributes'][$option] = $value;
// update database
         if (tep_session_is_registered('customer_id')) tep_db_query("update " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " set products_options_value_id = '" . (int)$value . "' where customers_id = '" . (int)$customer_id . "' and products_id = '" . tep_db_input($products_id) . "' and products_options_id = '" . (int)$option . "'");
       }
     }
   }

// define the project version
define('PROJECT_VERSION', 'osCommerce 2.2-MS2');

Link to comment
Share on other sites

Hi folks out there, reading all these postings ... I've done it today and tried a few things, but, how sad, it didn't make any fancy impression what so ever!

 

To give some support to one posting, I would also suggest, if some of you bright guys (f/m) out there would just add a configuration click in the admin tool. This way, a lot of time could be saved and almost everyone made happy at once. People like me don't really fancy implementing or changing codes. Testing and reporting is fine, but if changes could be handled the easy way, that would improve this fabulous tool even faster.

 

I stop here and just enjoy the times, when customers think they want to order 1.5 Bonsai Pot from my store to be - think positive: it improves communication or customer relationship and that's the beginning of suggess anyway.

 

Best Regards,

 

tulx

Link to comment
Share on other sites

Gerhard - - since it's more work to add configuration items into the Admin area, if you're making a tweak that you won't be turning on/off much, it is less work to just make the code change.

 

A central strength of osCommerce is that it's open-source. That means that anyone can change it in any way that they want. It's not reasonable to expect all of those changes to be nice, pretty, or to work for everyone. The changes often do just what the author wanted them to do, and need a little editing for general consumption.

 

Since you say that you "don't really fancy implementing or changing codes (sic)", then I suggest that you either:

1) avoid customization as much as possible

2) start learning to make code changes

3) hire someone who is more comfortable making code changes

4) purchase an alternate shopping cart

 

I don't mean to be rude, but if you want customization, you can't really expect it to take place on its own. Someone has to do it. That someone will have to make code changes.

 

-jared

Link to comment
Share on other sites

Where exactly did you modify this?  And what did you enter?

 

 

Mooretis gave a really great tip which simplifies this greatly.

 

Using his idea in shopping_cart.php (in /includes/classes/) i changed:

 

$this->update_quantity($products_id, $qty, $attributes);

 

to:

 

$this->update_quantity($products_id, $qty = (int)$qty, $attributes);

 

This works great for me. Of course, it does round down any fractional quantity entered. i.e. 1.1 becomes 1, 1.5 becomes 1 and 1.9 becomes 1.

 

Hope this helps someone.

 

KB

Network Webcams - The IP Camera Specialists

Link to comment
Share on other sites

  • 2 weeks later...

Yes, this last option was the easiest and worked lika a charm!

Thanks alot, i really needed this for my shop, it ain't easy shipping a half golf club :D

Link to comment
Share on other sites

  • 11 months later...
My shop sells fabrics aan needs to sell parts of a meter. (eg. 1.5 meter)

 

To accomplish this I changed 2 database tables.

 

The first table

 

orders_products

 

I changed the field products_quantity from INT to Decimal (15,3)

 

The second table

 

customers_basket

 

I also changed the field customers_basket_quantity from INT to Decimal (15,3)

This second table is when users are not logged in yet and add products to there cart.

 

If you need this I recommend to backup your database first. I am just an amateur in this so I might have overlooked something.

 

Remko

 

Hello, Your suggestion was very helpful. I also changed 2 fields in the products table. The 2 fields are:

 

products_quantity

products_ordered

 

This will help track those fractions in your inventory and sales totals.

 

One other change I had to make was in includes/classes/shopping_cart.php

 

There is a function called: "function add_cart" around line 91.

 

Below it you'll find the code: $this->update_quantity($products_id, (int)$qty, $attributes);

 

If you need fractions this line should read: $this->update_quantity($products_id, $qty, $attributes);

 

I removed the (int) since I need the decimal feature for a fabric store. That got decimals working correctly for our store.

 

Jim

Link to comment
Share on other sites

  • 3 months later...

I think this is a problem that doesn't need addressing. It may be true that someone COULD order 2.5 shirts, but WHY would they? What are they expecting to get as .5 of a shirt. However, if you change the fuctionality of the store it limits you when you start selling a product that can be broken into parts. 1.5 lbs of gumballs, or 2.6 feet of wire.

 

Real problems are problems that actually cause issues. In this case, your time and resources are better spent elsewhere.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...