Jump to content

Limit number of purchase of certain item or multiple items


fs_xyz

Recommended Posts

Hi,
Not sure if this is the correct section. Just wanted to contribute something with modification which I managed to do.
This is about limiting purchase of an item. For example in the code is product with product id = 2, and the limit is 1.

If you're not sure with what is your product id, you can see the list from catalog->product menu at admin page under 'ID' column.

The modification is in file controllers/front/CartController.php. The modification worked for 1.5 and 1.6.
Find this function around line 190 ish.
 

protected function processChangeProductInCart()
{
	$mode = (Tools::getIsset('update') && $this->id_product) ? 'update' : 'add';
		
	if ($this->qty == 0)
		$this->errors[] = Tools::displayError('Null quantity.', !Tools::getValue('ajax'));
	elseif (!$this->id_product)
		$this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax'));

I put modification to become :

protected function processChangeProductInCart()
{
	$mode = (Tools::getIsset('update') && $this->id_product) ? 'update' : 'add';
               
        //this is to prevent user to multiple click 'add to cart' button and add more than 1 when inputing number of purchase
	if (($this->qty > 1) && ($this->id_product == 2))
		$this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
		
        //this one is for checkout page where this will disable plus button upon press and produce message when pressed
	if (($this->context->cart->id) && ($this->id_product == 2)) {
		$db = Db::getInstance();
		$result = $db->getValue('
			SELECT quantity as pur_num
			FROM '._DB_PREFIX_.'cart_product
			WHERE id_product = 2 AND
				id_cart = '.$this->context->cart->id);
		if ($result>=1)
			$this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
	}
		
	if ($this->qty == 0)
		$this->errors[] = Tools::displayError('Null quantity.', !Tools::getValue('ajax'));
	elseif (!$this->id_product)
		$this->errors[] = Tools::displayError('Product not found', !Tools::getValue('ajax'));

Hopefully, this can help. Sorry for bad english, not my 1st language.

Link to comment
Share on other sites

If you need to set more rules, you can use this :
 

//this part to prevent multi 'add to cart' button usage on the same product and 
//to prevent input bigger than your limit when inputing number of purchase
switch ($this->id_product) {
    case 1: //example of product id, change to any number you want
        if ($this->qty > 1) //you can change 1 to any number you want as product limit
            $this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
        break;
    case 2:
        if ($this->qty > 1)
            $this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
        break;
    case 3:
        if ($this->qty > 1)
            $this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
        break;
} //if you need more items, just re-do one of those 'case' command.

//this part to disable 'plus' button on checkout page if the number went bigger or user change the number
//you will need the same number of 'case' command depend on previous usage.

$db = Db::getInstance();
switch ($this->id_product) {
    case 1: 
        $result = $db->getValue('
		SELECT quantity as pur_num
		FROM '._DB_PREFIX_.'cart_product
		WHERE id_product = 1 AND
			id_cart = '.$this->context->cart->id);
        if ($result>=1)
	    $this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
        break;
    case 2:
        $result = $db->getValue('
		SELECT quantity as pur_num
		FROM '._DB_PREFIX_.'cart_product
		WHERE id_product = 2 AND
			id_cart = '.$this->context->cart->id);
        if ($result>=1)
	    $this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
        break;
    case 3:
        $result = $db->getValue('
		SELECT quantity as pur_num
		FROM '._DB_PREFIX_.'cart_product
		WHERE id_product = 3 AND
			id_cart = '.$this->context->cart->id);
        if ($result>=1)
	    $this->errors[] = Tools::displayError('This product is limited to one quantity in purchase', !Tools::getValue('ajax'));
        break;
}

Well, this modification is useful to those who only require small amount of items require this purchase rule. For those with larger, you might need module for this.

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...