Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Limit Maximum Quantity


phypon

Recommended Posts

Hey guys. I'm a newbie and also new to php.

 

Here is a snippit of code I just came up with for limiting an item to only be added once to the cart. Once the user clicks the buy now button, the item is added to the cart and the buy now button turns into a checkout button instead of buy now.

 

Obviously you will need to tweak for your desired maximum values and for specific items if you only want to place a limit on certain items. I did a lot of searching for something like this and found people asking for the same thing, but did not receive a reply. So I hope this at least gets people started. Like I said, I'm not a php coder and I am very very new to osc, just over a week, so be cautious.

 

In product_listing.php

 

I replaced this:

case 'PRODUCT_LIST_BUY_NOW':
		$lc_align = 'center';
		$lc_text = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $listing['products_id']) . '">' . tep_image_button('button_buy_now.gif', IMAGE_BUTTON_BUY_NOW) . '</a> ';
		break;

 

with this:

case 'PRODUCT_LIST_BUY_NOW':
		$lc_align = 'center';
// try to use an if then statement to check if product is already in cart.  If so, change button to Checkout button
$test_products = $cart->get_products();	
$b_product_in_cart = 'false';
for ($i=0, $n=sizeof($test_products); $i<$n; $i++) 
{
  if ($test_products[$i]['id'] == $listing['products_id'])
{ 
  $b_product_in_cart = true;
  break;
}
}

if ($b_product_in_cart == 'true')
{
 $lc_text = '<a href="' . tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL') . '">' . tep_image_button('button_checkout.gif', 

IMAGE_BUTTON_CHECKOUT) . '</a> ';
 break;
} else {

		$lc_text = '<a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' .	  

   $listing['products_id']) . '">' . tep_image_button('button_buy_now.gif', IMAGE_BUTTON_BUY_NOW) . '</a> ';
		break;
}

 

Like I said, it is just a start for those who have trouble finding any answer to this. Maybe someone of experience can build on this.

 

phy

Link to comment
Share on other sites

  • 2 months later...

well there is another way as someone can alter the quantity from different pages. So you could modify the catalog\includes\classes\shopping_cart.php function add_cart

 

after the function definition and global lines

	function add_cart($products_id, $qty = '1', $attributes = '', $notify = true) {
  global $new_products_id_in_cart, $customer_id;

 

add this to force the quantity passed to be always 1.

$qty='1';

 

then in the same function, comment out the quantity update from this:

		  if ($this->in_cart($products_id_string)) {
		$this->update_quantity($products_id_string, $qty, $attributes);
	  } else {

 

to this:

		  if ($this->in_cart($products_id_string)) {
		//$this->update_quantity($products_id_string, $qty, $attributes);
	  } else {

Link to comment
Share on other sites

  • 3 years later...

Archived

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

×
×
  • Create New...