Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

User-define Price for a Product


Jadigidy

Recommended Posts

Does anyone know if there is a contribution here or anywhere else that has a module that will allow my user to define how much money they would like to donate to the seller. I want to put a charitable donation set together and allow the user to define how much they want to send. Thanks for the help.

Link to comment
Share on other sites

  • 9 months later...

I am in the process of trying to add this sort of feature. I wonder what a good topic title would be for this subject; I have found about 20 different posts requesting this feature.

 

Here's an outline of what I want to do:

The visitor could purchase a Gift Card or Certificate of any amount, his/her own amount.

This is validated through Javascript and ultimately through PHP to make sure it's a non-negative number.

Then, the item shows up in the shopping cart.

I had thought simply adding a "TEXT" type of option would suffice, and the user could type a number in, but there is no automatic way for the shopping cart to know that this value should be added--- I guess you could use the text field for something like personalization of a tshirt or a comment about an item.

 

Below are my thoughts about how to go about this, with some code samples. You may apply these to your own store at your own risk; if you are at all skittish about modifying code, just don't do anything. I'm really posting this in hopes that someone else brighter than myself will be able to figure a better way.

 

In the database, I've created some products attributes named "gift card amount" and "gift certificate amount" of type TEXT and assigned them to a product which has no price. Then the visitor can just type in how much of a gift card/certificate he/she wants.

 

I nosed around in the code for quite a bit. I'm using the CREloaded version of OSC, CRE Loaded6 v6.2 Pro[10.1] so the code may be a different.

There are several functions in the shopping_cart.php file (in both includes/classes and admin/includes/classes) that seem to be the place to make changes.

I am going to add three functions:

get_user_entered_value($productsid,$textvalue=1,$quantity=1,$languages_id=1)

get_product_attribute_text_values($arr)

and

forcenum($x)

This last one, forcenum, is relatively simple and probably belongs in both of the includes/functions/general.php and admin/includes/functions/general.php

All it does is to force a value to be a positive number, so the user is free to type in $ -999,999.99.

 

//start
function forcenum($x) {
$validchars='0123457890.';
$y='';
for ($i=0;$i<strlen($x);$i++) {
	if (strpos($validchars,substr($x,$i,1)) >-1) { $y.=substr($x,$i,1);}
	}
return (float)$y;
}
//end

There is probably some other function that does this, but this seemed to be a quick way to make sure. I dunno if it should be named tep_force_num or something, but anyway you get the point.

 

The second function, get_user_entered_value, will take a products_id and look it up to see if it has the magic words "gift card amount" or "online gift certificate" as an attribute. It should live in the includes/classes/shopping_cart.php file, probably at the bottom.

You can also pass the user-entered text value (see other function) and the quantity, and the languages id, if you want.

Minimally, if the words are found, this will return a 1, otherwise it returns 0.

Based on this, you can tell other parts of the code to add a different price than the cart.

 

//start
function get_user_entered_value($productsid,$textvalue=1,$quantity=1,$languages_id=1) {
// $productsid may be formatted like this: 
// 1111{5}Array{8}Array
$oid=split("{",$productsid);
$productsid=$oid[0];

$sql="select op.options_id, ot.products_options_name, o.options_type, op.options_values_price, op.price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " op   
	left join " . TABLE_PRODUCTS_OPTIONS . " o on o.products_options_id = op.options_id
	left join " . TABLE_PRODUCTS_OPTIONS_TEXT . " ot on ot.products_options_text_id = o.products_options_id and ot.language_id = '" . (int)$languages_id . "'
	where op.products_id = '" . $productsid . "'"; 
$attributes = tep_db_query($sql);
$attributes_values = tep_db_fetch_array($attributes);
$products_options_name = $attributes_values['products_options_name'];
$theprice=0;
if ((strpos(strtolower($products_options_name),'gift card amount') >-1) ||
	(strpos(strtolower($products_options_name),'online gift certificate') >-1) )
	{$theprice=forcenum($textvalue) * forcenum($quantity);}	
return $theprice;
}
//end

The SQL code is from some other section of the cart where it's looking up attributes; I have no real idea of how it works.

 

The next function, get_product_attribute_text_values, will take an array of the user's entered values and return the total. Here, it's simply looking to see if the 't' is in the array. It, too, should live in the includes/classes/shopping_cart.php file, probably at the bottom. At some point in the shopping cart code, see, there is an array of attributes set, so it's at that point you want to investigate whether or not it is a text field into which the user may have entered something. It seems to be something in an object or array or whatever, contents[$products_id]['attributes']

 

//start
function get_product_attribute_text_values($arr) {
$tot=0;
if (is_array($arr)) {
	foreach ($arr as $val) {
		foreach( $val as $key=>$value) {
			if ($key=='t') {$tot += $value;}
			}
		}
	}
return $tot;
}
//end

 

Next comes the really hard part. You have to track down all of the places in the shopping cart where it may be adding things in the cart.

In the templates/mytemplatename/content/shopping_cart.tpl.php file, look for something like

elseif ( isset($value['t']) )

and add to it

 

//start
			$temp=get_user_entered_value($products[$i]['id'],forcenum($value['t']));
			if ($temp>0) {
				$products[$i]['final_price']=$temp;
				}				
			//end

 

In classes/shopping_cart.php, look for several places

if ($special_price) {

$products_price = $special_price;

}

and add

	//bkj added for user-entered-value-based price:
	$temp=get_user_entered_value($prid,get_product_attribute_text_values($this->contents[$products_id]['attributes']));
	if ($temp>0){ $products_price= $temp;}
	//end bkj addition for user-entered-value-based price

 

Look further for

function attributes_price($products_id) {

$attributes_price = 0;

and add

//bkj added for user-entered-value-based price:
	$temp=get_user_entered_value($products_id,get_product_attribute_text_values($this->contents[$products_id]['attributes']));
	if ($temp>0){return $temp;}
	//end bkj addition for user-entered-value-based price

 

Look still further for

if ($products = $pf->loadProduct($products_id, $languages_id)) {

$products_price = $pf->computePrice($this->contents[$products_id]['qty']);

and add

		//bkj added for user-entered-value-based price:
		$temp=get_user_entered_value($products_id,get_product_attribute_text_values($this->contents[$products_id]['attributes']));
		if ($temp>0) { $products_price=$temp; }

 

and this should JUST ABOUT do it. I say just about, because I think the same changes need to be applied somewhere else in the Admin modules, and I haven't gotten that far. There also may be another place to add it somewhere during checkout, in case there are additional places where the cart is adding things.

 

 

Anyway, if someone wants to check through this and see if it makes sense, I'd love to hear from you. I will be posting further changes as I work through this; it's pretty much working for adding an item of any user-entered price to the cart.

 

--Brian

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...