VirtueMart Forum

VirtueMart 1.1.x [ Old version - no longer supported ] => Questions VM 1.1 ( the only active board for the old version) => Topic started by: dineshprima on February 23, 2011, 10:10:13 am

Title: Need to allow only one product purchase VirtueMart 1.1
Post by: dineshprima on February 23, 2011, 10:10:13 am
Hello,

In my website i need to allow users only select one product to their cart, multiple products are not allowed to add...so how can i do that using virtuemart...any suggestions??

Thanks
Dinesh
Title: Re: Need to allow only one product purchase
Post by: seyi on February 23, 2011, 11:16:59 am
there are commercial products that do this:
https://polishedgeek.com/paid-extensions/virtuemart-purchasing-rules/overview-a-features.html?amigosid=3
Title: Re: Need to allow only one product purchase
Post by: dineshprima on February 23, 2011, 11:40:51 am
Thanks for the prompt reply...There is no hack for this freely?
Title: Re: Need to allow only one product purchase
Post by: seyi on February 23, 2011, 19:02:47 pm
do you mean 1 in quantity (1 of a and 1 of b) or 1 product only into the cart (1 of either a or b)?
Title: Re: Need to allow only one product purchase
Post by: dineshprima on February 24, 2011, 06:08:49 am
1 product only into the cart (1 of either a or b)

Only one product will be there in cart..no other products ...because it is a service websites , so users can choose only one service package at a time..
Title: Re: Need to allow only one product purchase
Post by: seyi on February 24, 2011, 11:44:25 am
Here is a hack that will do just that.  There are 2 places you need to intercept the cart code. 
1) When adding to the cart:  if there is 1 or more items in the cart, dont add
in www/administrator/components/com_virtuemart/classes/ps_cart.php, in function add, right after
Code: [Select]
<?php
function add(&$d) {
?>
enter this
Code: [Select]
<?php
if($_SESSION['cart']["idx"]>=1) return false;
?>



2) when updating the cart: if updating the quantity to more than 1, dont allow
in same file, inside function update, after this line:
Code: [Select]
<?php
$quantity 
= isset($d["quantity"]) ? (int)$d["quantity"] : 1;
?>
enter this
Code: [Select]
<?php
if($quantity>1$quantity 1;
if(
strtolower($func)=='cartadd') return false;
?>

All done.
Title: Re: Need to allow only one product purchase
Post by: dineshprima on February 25, 2011, 07:40:53 am
Thank you very very much seyi...it worked for me...love you..
Title: Re: Need to allow only one product purchase
Post by: DREK on March 29, 2011, 17:46:48 pm
Thanks it works for me also.

However, I have 1 question.

I want to be able to buy only 1 product but in several quantities. I mean, I click in one product, it goes to the cart. I navigate in the webpage and again click in the same product and it gives me an error.

Any idea to buy ONE TYPE of product but in several quantities?

Regards,
Title: Re: Need to allow only one product purchase
Post by: seyi on March 30, 2011, 00:52:17 am
Hi,

The above code is specific for 1 product only.  For one product with multiple quantities, you can try this:

in www/administrator/components/com_virtuemart/classes/ps_cart.php, in function add, right after

Code: [Select]
<?php
function add(&$d) {
?>

enter this

Code: [Select]
<?php
if($_SESSION['cart']["idx"]==0);
elseif(
$_SESSION['cart'][0]["product_id"] != $d['product_id']) return;
?>

Title: Re: Need to allow only one product purchase
Post by: DREK on March 30, 2011, 10:29:24 am
@ seyi thanks for the answer, it was very helpful.

However, I did it slightly different. I would like to to put a text message anytime someone wants to buy different products. Imagine, if someone has A in the cart and wants to hit buy in B, it wouldn't allow it but I want to alert it with a message (something like: Sorry, but you can only buy 1 type product).

Here's the code. I tried it, it works what I wanted it (buy diff. quantities of ONE product but the text message doesn't appear).

Quote
function add(&$d) {

 
  if($_SESSION['cart']["idx"]>=1) {}
 

  if($_SESSION['cart']["idx"]==0){}
  elseif($_SESSION['cart'][0]["product_id"] != $d['product_id'])

  {
   echo "<script>alert('Error Here');</script>";
   return;
  }


The problem is how to make an alert message withouth using the ALERT in JS?
 
I think it would be something like? $vmLogger->warning( $VM_LANG->_('PHPSHOP_CART_ERROR_NO_VALID_QUANTITY',false) );  but I am not sure, and I think I am missing something.

Could you please me help me with this one? Or someone else?

Thanks,
Title: Re: Need to allow only one product purchase
Post by: seyi on March 30, 2011, 13:28:21 pm
you could try either of these

Code: [Select]
<?php
$vmLogger
->warning'My Error Message' );
$vmLogger->err'My Error Message' );
?>


Be sure to add
Code: [Select]
<?php
global $vmLogger;
?>

before calling it.
Title: Re: Need to allow only one product purchase
Post by: DREK on March 30, 2011, 15:01:16 pm
Hello seyi,

Thanks for the answer. I introduced this code:

 
Quote
global $sess, $VM_LANG, $cart, $vmLogger,$func;
 
  if($_SESSION['cart']["idx"]>=1) {}
 
  if($_SESSION['cart']["idx"]==0){}
  elseif($_SESSION['cart'][0]["product_id"] != $d['product_id'])
  {
   
   $vmLogger->warning( 'My Error Message' );
   $vmLogger->err( 'My Error Message' );
return;
  }

With that code, it redirects me to the frontpage of the webpage, without alerting an error message, any idea of how to fix it?

Thanks.
Title: Re: Need to allow only one product purchase
Post by: seyi on March 31, 2011, 08:48:35 am
Strange error.  If you look down in the function it is used, for example
Code: [Select]
<?php
if ($quantity 0) {
vmRequest::setVar('product_id'$product_id );
$vmLogger->warning$VM_LANG->_('PHPSHOP_CART_ERROR_NO_NEGATIVE',false) );
return False;
}
?>

you can always try joomla messaging system
Code: [Select]
<?php
$mainframe
->enqueueMessage('My error message','error');
?>

againt, do not forget to add $mainframe to the global variables
Title: Re: Need to allow only one product purchase
Post by: DREK on March 31, 2011, 22:25:24 pm
Hello seyi,

Thanks a million, it worked this way.

Quote

if($_SESSION['cart']["idx"]>=1) {}
 
  if($_SESSION['cart']["idx"]==0){}
  elseif($_SESSION['cart'][0]["product_id"] != $d['product_id'])
  {
   global $mainframe;
   
   $mainframe->enqueueMessage('Error here!','error');
   return true;
  }

Thanks for all the help
, I really appreciated your effort, and WITHOUT abusing of your time, I have another question which I would appreciate just a light about it.

I'm using K2+VirtueMart with Joomla 1.5.

I have a coupons webpage. My problem is that when my clients go to account maintenance link, they have their order history with no problems. However, when they hit in the product that they bought, that link is associated with virtuemart ID and does not redirect to the K2 ID, which I would like to set. Is that possible?

I would like that my clients could have access to the product description even if those products are not available any more. In their order history, they only can click in virtuemart items, which will redirect them to the cart, rather to the products page..

So, basically clients can go to their order history, and hit in a product and is linked to:

Quote
index.php?option=com_virtuemart&page=shop.product_ details&flypage=flypage.tpl&product_id=8&Itemid=13 5

And I would like that the product would go to:
Quote
index.php?option=com_k2&view=item&id=58

My question is how to call the ID of K2?

Here's the part of the code:
Quote
/www/components/com_virtuemart/themes/simplecheckout/templates/pages/account.order_details.tpl.php - path
// ***** Change guard to use new flag $link_to_product instead of !empty( $product_id )
            if( $link_to_product) {
               echo '<a href="'.$sess->url( $mm_action_url."index.php?page=shop.product_details&product_id=$product_id") .'" title="'.$dbcart->f("order_item_name").'">';
     
              }
              $dbcart->p("order_item_name");
              echo " <div style=\"font-size:smaller;\">" . $dbcart->f("product_attribute") . "</div>"
;

I think that the code should me similarly to K2Mart or something like that, but I wanted to stay with VirtueMart and K2. What's the relation of K2 and VirtueMart and how to fix it in order history?

Thanks in advance and one more time, thanks for the help.

Title: Re: Need to allow only one product purchase
Post by: dimcho on December 19, 2011, 14:56:16 pm
Hello,
I need something similar...
In my website I need to allow users to select:
Form category 1 (its obligate): only 1 product
category 2: only 0 or 1
category 3: only 0 or 1
category 4: only 0, 1 or 2
category 5: only 0, 1 or 2
category 6: only 0 or 1

Is this possible?
Title: Re: Need to allow only one product purchase
Post by: servicefly on January 31, 2012, 05:16:05 am
I'm not sure if this can be done, but I would like a User to login and be redirected to a single product based on their login or a coupon? Example: A User logs in and is redirected to photos of their family at a park to purchase as a digital good. A product is listed under the Photos category which is not visible. Then a user logs in with username & password given by park management or a coupon specific to one product. After login or entering coupon they are redirected to product (family photos). That User is the only one who gets to see the product of their family photos.
Title: Re: Need to allow only one product purchase
Post by: seyi on March 11, 2014, 17:34:24 pm
Hello,

Your needs seem quite specific, I dont think something like that exists.  I think you would require the development of a custom plugin which should be able to handle it.
Title: Re: Need to allow only one product purchase
Post by: baqpad on February 25, 2015, 20:45:19 pm
Hello,

I now this is an old post but as my question is related to this one and couldn't find in the forum any other post for this, so here I am.
What if I just want to let people buy from a only one category? Say I sell shoes and shirts. So if someone bought shoes I don't want people to buy shirts aswell.
Any idea for that? Hope to find a bit of help.

Joomla 2.5.28
Virtuemart 2.6.10

Thanks guys
Title: Re: Need to allow only one product purchase
Post by: seyi on February 27, 2015, 01:46:54 am
Not sure if this can do it, but check into it, may be built in:
http://extensions.joomla.org/extensions/extension/extension-specific/virtuemart-extensions/virtuemart-bonus-products
Title: Re: Need to allow only one product purchase
Post by: baqpad on February 28, 2015, 09:17:58 am
Thanks seyi,

Yes, I did found that extension but at first glance thought it was not what I was looking for. Been now in contact with them and will come back with any news.
Title: Re: Need to allow only one product purchase
Post by: baqpad on March 01, 2015, 18:58:57 pm
Hi seyi,

Got an answer from daycounts.com but the extension you mentioned doesn't do the trink :(
Title: Re: Need to allow only one product purchase VirtueMart 1.1
Post by: seyi on March 03, 2015, 18:18:19 pm
Hello,

I created a plugin which automatically excludes selected countries from buying certain products, can be downloaded here:
https://awodev.com/products#plg_vmproductlocexclude

You can alter it to work for your purpose.  In file vmProductLocExclude.php search for the code around line 113
Code: [Select]
<?php
if(empty($cart->BT)) return;
?>

And right before that add this
Code: [Select]
<?php
if(empty($cart->products)) return;

$categories = array();
foreach($cart->products as $product$categories[$product->virtuemart_category_id] = 1;
if(count($categories)==1) return;

//remove the last item in cart
end($cart->products);         // move the internal pointer to the end of the array
$product_cart_id key($cart->products);
$cart->removeProductCart($product_cart_id);
JFactory::getApplication()->enqueueMessage(JText::_('Can only purchase from 1 category'), 'error');

return;
?>

Then install and enable.  That should limit purchase of products to exactly 1 category.  Not sure how it would react if your products happen to be in multiple categories though.

 
Title: Re: Need to allow only one product purchase VirtueMart 1.1
Post by: _Ron on October 01, 2016, 00:57:47 am
Updated solution for Virtuemart 3.

File to edit: \components\com_virtuemart\controllers\cart.php
Modify the function "add".
Code: [Select]
public function add() {
$mainframe = JFactory::getApplication();
if (VmConfig::get('use_as_catalog', 0)) {
$msg = vmText::_('COM_VIRTUEMART_PRODUCT_NOT_ADDED_SUCCESSFULLY');
$type = 'error';
$mainframe->redirect('index.php', $msg, $type);
}
$cart = VirtueMartCart::getCart();
if ($cart) {
$virtuemart_product_ids = vRequest::getInt('virtuemart_product_id');
$error = false;
if (count($cart->cartProductsData) == 0) {
$cart->add($virtuemart_product_ids,$error);
if (!$error) {
$msg = vmText::_('COM_VIRTUEMART_PRODUCT_ADDED_SUCCESSFULLY');
$type = '';
} else {
$msg = vmText::_('COM_VIRTUEMART_PRODUCT_NOT_ADDED_SUCCESSFULLY');
$type = 'error';
}
} else {
$msg = 'One item per checkout allowed!';
$type = 'error';
}

$mainframe->enqueueMessage($msg, $type);
$mainframe->redirect(JRoute::_('index.php?option=com_virtuemart&view=cart', FALSE));

} else {
$mainframe->enqueueMessage('Cart does not exist?', 'error');
}
}