Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / Constarints for product quantities?

Constarints for product quantities?

Locked

Views: 3,024

Results 1 to 20 of 27
This thread is locked. New replies are disabled.
4 Mar 2009, 9:32 AM
#1
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Constarints for product quantities?

Hi all!
I'm new to this forum. I'm setting up a shop with Zen cart 1.3.8. It's a wine related shop. The owner has some constraints on the number of products, because, for shipping reasons, 0,75 l bottles (classic wine bottles) must be sent in groups of 6. Now, he does not want to be able to sell only 6-bottle pack of the same wine, but the costumer must be able to buy differnt bottle of wine, while the sum of the number of 0,75 bottles must be a multiple of 6. He can also buy other kinds of products, but there are no constraints on those. Is it possible to add such a control during checkout? I mean, display an alert when the user sees the cart before checkout?

Thank you very much for all you help!

4 Mar 2009, 10:35 AM
#2
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Constarints for product quantities?

I think Scott Wilson (swguy) has a Cart Validation technique to ensure that products in the cart are equal to a multiple of a given number (in your case "6"), before the customer can proceed to checkout.

PM swguy.

6 Mar 2009, 11:45 AM
#3
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

I would like some more informations about this, I think I can add something to extra_cart_actions, but I would like to know hot to forbid checkout if cart is not validated, I could'nt find a tutorial or some documentation on how to do this. Any help is appreciated.
Thanks

11 Mar 2009, 9:04 AM
#4
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

I've managed to do somethng similar to what I want, well actually it does exactly what I wanted. It's pretty simple, I just need to modify the Get_products method of the shopping_cart class, but the problem is that in this way I modified core files, which will be obiouvsly replaced in caso of an update, wouldn't it be possible to do the same by simply adding some extension, without modifying zen cart files?

11 Mar 2009, 6:16 PM
#5
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Constarints for product quantities?

Tell us which files you modified, and how and what code you added, and we'll be able to see if there's over-ride potential...

11 Mar 2009, 8:43 PM
#6
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

I modified the file /includes/classes/shopping_cart.php, but I would like to know how to do it without touching core files. I think I could do something with extra cart functions but I don't know how to do it. In that file I modified the get_products functions, setting valid = false if the check (ie product_number / 6 == 0) is false. But I don't know how to do the same without using this file. Also, doing this is ok, since it shows the checkout button but if I click it it send me again to the shopping cart, I would like to know how to hide it if the cart is not valid.

11 Mar 2009, 9:02 PM
#7
bunyip avatar

bunyip

Totally Zenned

Join Date:
Sep 2004
Location:
Western Massachusetts
Posts:
2,361
Plugin Contributions:
1

Re: Constarints for product quantities?

Although the class files are generally considered 'core' files, there is a way to override them using the auto_loader system, which does have an override capability.

Take your modified shopping_cart class file for example.

Make a copy of includes/auto_loaders/config.core.php and save it into the includes/auto_loaders/overrides/ folder.

Now edit that new copy, and replace this code$autoLoadConfig[0][] = array('autoType'=>'class', 'loadFile'=>'shopping_cart.php'); with this```
$autoLoadConfig[0][] = array('autoType'=>'class',
'loadFile'=>'my_shopping_cart.php');


When you edit the file includes/classes/shopping_cart.php, save the edited file as includes/classes/my_shopping_cart.php and **voila!** you are now using an 'override' copy that won't get overwritten during an upgrade.

I have used this technique for various class files that I've had reason to modify.
12 Mar 2009, 8:57 AM
#8
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

Interesting tip, I'll try it! I think this could be ok, but now I would like to go a bit further: is it possible to add an hidden attribute to a product in order to be able to choose which products must be taken into account when doing this calculation? I mean, my goal is to check if the user has bought a number of products multiple of 6, but not all products must be subject to this limit. I could hard-code this list in the class, but it would be better if I could select them in the admin section.

12 Mar 2009, 9:12 AM
#9
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Constarints for product quantities?

See if you can "clone" Andrew Berezin's "Minimum Order" module. His module displays an alert ($messageStack) if the conditional is not met.

In the case of Andrew's module, the store owner sets a minimum MONETARY value that must be in the shopping cart before it can proceed to checkout - let's say the shop owner says if the value of the order must total £10, he sets the value "10" in admin.

If a shopper's cart is LESS than £10, the alert warning is displayed.

I'm no PHP coder, so I wouldn't know what code to use to parse the item-count, but I'm sure this would be a piece of cake for a coder.

Something like:

          if($_SESSION['cart']->count_contents() % CART_MULTIPLE_AMOUNT == 0) {
            $_SESSION['valid_to_checkout'] = false;
            $messageStack->add('shopping_cart', sprintf(TEXT_ORDER_NOT_A_MULTIPLE, (CART_MULTIPLE_AMOUNT)) . '<br />', 'caution');

... where I use the php modulus expression (%) rather than "/" (divided by) and CART_MULTIPLE_AMOUNT is the value the shop owner expresses in the database via the Admin Panel. In your case, **6 **.

As I say, I don't now how to parse the "count_contents" value...

12 Mar 2009, 9:17 AM
#10
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Constarints for product quantities?

[In the product editing page in admin, you can set

Product Qty Units: []

to 6, and customers will only be allowed to buy that product in groups of 6.

I don't know for sure if this forbids changing the quantity to an odd number once in the shopping cart, but I think that aspect is covered too.]

Nevermind, eyes too bleary to see the "different kinds of wine" aspect of your question... no wonder knowledgeable people weren't giving that answer... :) Time for bed.

12 Mar 2009, 9:18 AM
#11
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

Thank you, maybe using that module I'll be able to adapt it to my needs.
Thank you, both of you, for your help.

12 Mar 2009, 9:20 AM
#12
schoolboy avatar

schoolboy

Totally Zenned

Join Date:
Jun 2005
Location:
Cumbria, UK
Posts:
10,327
Plugin Contributions:
0

Re: Constarints for product quantities?

If you succeed, I'd really like to know the solution, as I too am building a wineshop with the same challenge!

12 Mar 2009, 9:20 AM
#13
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

gjh42:

In the product editing page in admin, you can set

Product Qty Units: []

to 6, and customers will only be allowed to buy that product in groups of 6.

That's not what I want, the customer can buy 3 products A 1 products B and 1 product C, the important thing is that that sum is a multiple of 6 (only for some products, not for all).

12 Mar 2009, 2:18 PM
#14
bunyip avatar

bunyip

Totally Zenned

Join Date:
Sep 2004
Location:
Western Massachusetts
Posts:
2,361
Plugin Contributions:
1

Re: Constarints for product quantities?

ccppll:

Interesting tip, I'll try it! I think this could be ok, but now I would like to go a bit further: is it possible to add an hidden attribute to a product in order to be able to choose which products must be taken into account when doing this calculation? I mean, my goal is to check if the user has bought a number of products multiple of 6, but not all products must be subject to this limit. I could hard-code this list in the class, but it would be better if I could select them in the admin section.

The best way to approach this would be to create a new product type that uses the same handler page as a normal product. Then you use that new product type only for items that have the qty 6 limit, rather than try to use a hidden attribute. Your custom code then just needs to check the product type, rather than against a list of product ids or extra fields/attributes.

I don't think the qty-units setting in admin will help you, because that sets the order units just for the single product, which is not what you are trying to achieve.

12 Mar 2009, 2:20 PM
#15
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

gjh42:

[In the product editing page in admin, you can set

Product Qty Units: []

to 6, and customers will only be allowed to buy that product in groups of 6.

I don't know for sure if this forbids changing the quantity to an odd number once in the shopping cart, but I think that aspect is covered too.]

Nevermind, eyes too bleary to see the "different kinds of wine" aspect of your question... no wonder knowledgeable people weren't giving that answer... :) Time for bed.

bunyip:

The best way to approach this would be to create a new product type that uses the same handler page as a normal product. Then you use that new product type only for items that have the qty 6 limit, rather than try to use a hidden attribute. Your custom code then just needs to check the product type, rather than against a list of product ids or extra fields/attributes.

I don't think the qty-units setting in admin will help you, because that sets the order units just for the single product, which is not what you are trying to achieve.

Mmmm, could I have a sample of this?

thanks

12 Mar 2009, 2:36 PM
#16
bunyip avatar

bunyip

Totally Zenned

Join Date:
Sep 2004
Location:
Western Massachusetts
Posts:
2,361
Plugin Contributions:
1

Re: Constarints for product quantities?

To add the new product type to your database, run this query from your SQL Patch page in admin:

INSERT INTO `product_types` ( `type_id` , `type_name` , `type_handler` , `type_master_type` , `allow_add_to_cart` , `default_image` , `date_added` , `last_modified` ) VALUES ('', 'Product - Wine', 'product', '1', 'Y', '', NOW( ) , NOW( ));

sample function codes I've used for checking the product type:```
// function to lookup product_type for product-wine
function get_wine_product_type_id() {
global $db;

$sql = "select type_id from " . TABLE_PRODUCT_TYPES . " where type_name = 'Product - Wine'";
$type_query = $db->Execute($sql);
$wine_product_type = $type_query->fields['type_id'];

return $wine_product_type;

}

// function to lookup a product's product_type
function get_products_product_type_id($products_id) {
global $db;

$sql = "select products_type from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'";
$type_query = $db->Execute($sql);
$product_type_id = $type_query->fields['products_type'];

return $product_type_id;

}


Then you could change the product type of your existing products that need to be this type directly from phpmyadmin - replace the value in the products_type field of the products table with the newe value (if you haven't previously added a new product type, the new one will be 6)
12 Mar 2009, 2:40 PM
#17
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

bunyip:

To add the new product type to your database, run this query from your SQL Patch page in admin:

INSERT INTO product_types ( type_id , type_name , type_handler , type_master_type , allow_add_to_cart , default_image , date_added , last_modified ) VALUES ('', 'Product - Wine', 'product', '1', 'Y', '', NOW( ) , NOW( ));

> 
> sample function codes I've used for checking the product type:```
// function to lookup product_type for product-wine
function get_wine_product_type_id() {
    global $db;
	
	$sql = "select type_id from " . TABLE_PRODUCT_TYPES . " where type_name = 'Product - Wine'";
	$type_query = $db->Execute($sql);
	$wine_product_type = $type_query->fields['type_id'];
	
	return $wine_product_type;
}

// function to lookup a product's product_type
function get_products_product_type_id($products_id) {
    global $db;
	
	$sql = "select products_type from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'";
	$type_query = $db->Execute($sql);
	$product_type_id = $type_query->fields['products_type'];
	
	return $product_type_id;
}

So type_handler is product, that is, it uses the same product class? Or should I override it? And, regarding the functions you posted, where could i add them? And where to call them?

Thanks

12 Mar 2009, 2:46 PM
#18
bunyip avatar

bunyip

Totally Zenned

Join Date:
Sep 2004
Location:
Western Massachusetts
Posts:
2,361
Plugin Contributions:
1

Re: Constarints for product quantities?

yes, if you keep the type_handler the same, you don't need to create a whole new bunch of files in admin or the catalog.
If for example you set the handler to product_wine, you'd need to duplicate the folder admin/includes/modules/product, and add new pages and templates on the catalog side. You'd really only want to do that if you intended to add specific product fields into the database for standardizing display, or perhaps adding new product filters, etc.
As for the functions, add them into a new file (eg wine_functions.php) in the includes/functions/extra_functions folder.
You'd call them when writing your code for quantity checking:
eg: ```
if (get_products_product_type_id($products_id) == get_wine_product_type_id()) {
check quantities code
} else {
do nothing
}

2 Apr 2009, 8:17 AM
#19
ccppll avatar

ccppll

New Zenner

Join Date:
Mar 2009
Posts:
19
Plugin Contributions:
0

Re: Constarints for product quantities?

Hi! After some discussion with the costumer, he decided to change the way to handle things. He now wants to send only groups of 6 bottles of the same wine. So, now I just use quantity units to 6 and that's all. I would like to know if it would be possible to change a bit the way it's done. For example, why does zen cart lets me add 5 bottles to my cart if I must buy 6? Also, wouldn't it be possible to add the product unit instead of the single bottle? I mean, since I must add 6 or 12 etc bottles, but I would like to keep the price of a single bottle etc, is there a way to add not single bottles but groups of 6 bottles? So the number in the product quantity would be the number of units...

Thanks!

2 Apr 2009, 5:43 PM
#20
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Constarints for product quantities?

If you set the Units to 6, then the add to cart quantity box is 6 ...