Jump to content
Sign in to follow this  
loveleaf

Add Weight to Product attributes

Recommended Posts

Hello everyone.

I run a shop selling herbs. Most product weights are set at 0.06 (about one ounce), but if a customer chooses, for example, the 4 ounce option, I wanted to have this reflect in the product weight, since I use the USPS module to calculate postage.

 

I just installed the "Add weight to product attributes" contribution.

It seemed to be working correctly, until I changed the product quantity.

 

It seems that it multiplies the original product weight, and then adds the additional weight defined in the attributes, on top. No good!

 

I don't know much PHP, but with enough time I can solve many problems like this myself. I'm at least hoping someone can point me to the right file where these calculations take place.

 

Any help would be appreciated.

 

Peace,

 

Scott

Share this post


Link to post
Share on other sites

It seems like its something in here, but I can't get anything to work!

 

It just adds the total attribute weight on top of the product weight. It should be Product weight + (total attribute weight * quantity)

 

Please help!

 

--

 

// BOF add-weight-to-product-attributes with UPSxml mod

 

// determine total weight of attributes to add to weight of product

$attributes_total_weight = 0;

 

if (isset($this->contents[$products_id]['attributes'])) {

 

reset($this->contents[$products_id]['attributes']);

 

$where = ' AND ((';

 

while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {

 

$where .= 'options_id=' . $option . ' AND options_values_id=' . $value . ') OR (';

 

}

 

$where=substr($where, 0, -5) . ')';

 

 

$attribute_weight_query = tep_db_query('SELECT options_values_weight FROM ' . TABLE_PRODUCTS_ATTRIBUTES . ' WHERE products_id=' . (int)$prid . $where);

 

 

if (tep_db_num_rows($attribute_weight_query)) {

 

while ($attributes_weight_array = tep_db_fetch_array($attribute_weight_query)) {

$attributes_total_weight += $attributes_weight_array['options_values_weight'];

}

 

} // end if (tep_db_num_rows($attribute_weight_query))

 

} // end if (isset($this->contents[$products_id]['attributes']))

 

// EOF add-weight-to-product-attributes mod

$products_array[] = array('id' => $products_id,

'name' => $products['products_name'],

'model' => $products['products_model'],

'image' => $products['products_image'],

'price' => $products_price,

'quantity' => $this->contents[$products_id]['qty'],

'weight' => $products['products_weight'] + $attributes_total_weight,

'final_price' => ($products_price + $this->attributes_price($products_id)),

'tax_class_id' => $products['products_tax_class_id'],

'attributes' => (isset($this->contents[$products_id]['attributes']) ? $this->contents[$products_id]['attributes'] : ''));

}

}

 

return $products_array;

}

Share this post


Link to post
Share on other sites

just before this line

 

if (tep_db_num_rows($attribute_weight_query)) {

 

 

add

$attributes_total_weight = 0;

 

then after this line

// EOF add-weight-to-product-attributes mod

 

add

if( $attributes_total_weight > 0 )
 $tmp_weight = $attributes_total_weight;
else
 $tmp_weight = $products['products_weight'];

 

replace this line

'weight' => $products['products_weight'] + $attributes_total_weight,

 

with

'weight' => $tmp_weight,

 

So if attributes weight is present it will be taken into account otherwise the products weight will be used

Haven't tested anything so backup your files first.

Share this post


Link to post
Share on other sites

turns out, v0.2 of "add weight to product attributes" is missing a file. classes/shopping_cart.php in v.0.1 works fine.

thanks for the help.

 

just before this line

 

if (tep_db_num_rows($attribute_weight_query)) {

add

$attributes_total_weight = 0;

 

then after this line

// EOF add-weight-to-product-attributes mod

 

add

if( $attributes_total_weight > 0 )
 $tmp_weight = $attributes_total_weight;
else
 $tmp_weight = $products['products_weight'];

 

replace this line

'weight' => $products['products_weight'] + $attributes_total_weight,

 

with

'weight' => $tmp_weight,

 

So if attributes weight is present it will be taken into account otherwise the products weight will be used

Haven't tested anything so backup your files first.

Share this post


Link to post
Share on other sites

I have done everything except merge the catalog/incudes/classes/shopping_cart.php. In the install it says:

catalog/includes/classes/shopping_cart.php

= calculate() adds attribute weight to total weight calculation

 

This is the calculate function from add weight to product attributes.

 function calculate() {
  $this->total = 0;
  $this->weight = 0;
  if (!is_array($this->contents)) return 0;

  reset($this->contents);
 while (list($products_id, ) = each($this->contents)) {
	$qty = $this->contents[$products_id]['qty'];

// products price
	$product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
	if ($product = tep_db_fetch_array($product_query)) {
	  $prid = $product['products_id'];
	  $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
	  $products_price = $product['products_price'];
	  $products_weight = $product['products_weight'];

	  $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
	  if (tep_db_num_rows ($specials_query)) {
		$specials = tep_db_fetch_array($specials_query);
		$products_price = $specials['specials_new_products_price'];
	  }

	  $this->total += tep_add_tax($products_price, $products_tax) * $qty;
	  $this->weight += ($qty * $products_weight);	
	}

// attributes price
// kumar@farmdev.com
// add-weight-to-product-attributes mod:
// added weight to db query
	if (isset($this->contents[$products_id]['attributes'])) {
	  reset($this->contents[$products_id]['attributes']);
	  while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
		$attribute_price_query = tep_db_query("select options_values_price, price_prefix, options_values_weight from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$prid . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
		$attribute_price = tep_db_fetch_array($attribute_price_query);
		if ($attribute_price['price_prefix'] == '+') {
		  $this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
		} else {
		  $this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);
		}
		if(!empty($attribute_price['options_values_weight'])) {
			// kumar@farmdev.com
			// add-weight-to-product-attributes mod:
			$this->weight += ($qty * $attribute_price['options_values_weight']);
		} // END if(!empty($attribute_price['options_values_weight'])) {
	  }
	}
  }
}

function attributes_price($products_id) {
  $attributes_price = 0;

  if (isset($this->contents[$products_id]['attributes'])) {
	reset($this->contents[$products_id]['attributes']);
	while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
	  $attribute_price_query = tep_db_query("select options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id . "' and options_id = '" . (int)$option . "' and options_values_id = '" . (int)$value . "'");
	  $attribute_price = tep_db_fetch_array($attribute_price_query);
	  if ($attribute_price['price_prefix'] == '+') {
		$attributes_price += $attribute_price['options_values_price'];
	  } else {
		$attributes_price -= $attribute_price['options_values_price'];
	  }
	}
  }

  return $attributes_price;
}

function get_products() {
  global $languages_id;

  if (!is_array($this->contents)) return false;

  $products_array = array();
  reset($this->contents);
  while (list($products_id, ) = each($this->contents)) {
	$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
	if ($products = tep_db_fetch_array($products_query)) {
	  $prid = $products['products_id'];
	  $products_price = $products['products_price'];

	  $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
	  if (tep_db_num_rows($specials_query)) {
		$specials = tep_db_fetch_array($specials_query);
		$products_price = $specials['specials_new_products_price'];
	  }

 

 

and this is from my current file:

 

	function calculate() {
  $this->total = 0;
  $this->weight = 0;
  if (!is_array($this->contents)) return 0;

  reset($this->contents);
  while (list($products_id, ) = each($this->contents)) {
	$qty = $this->contents[$products_id]['qty'];
// BOF Separate Pricing Per Customer
// global variable (session) $sppc_customer_group_id -> class variable cg_id
 global $sppc_customer_group_id;

 if(!tep_session_is_registered('sppc_customer_group_id')) {
 $this->cg_id = '0';
 } else {
  $this->cg_id = $sppc_customer_group_id;
 }
// EOF Separate Pricing Per Customer

// products price
	$product_query = tep_db_query("select products_id, products_price, products_tax_class_id, products_weight from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'");
	if ($product = tep_db_fetch_array($product_query)) {
	  $prid = $product['products_id'];
	  $products_tax = tep_get_tax_rate($product['products_tax_class_id']);
	  $products_price = $product['products_price'];
	  $products_weight = $product['products_weight'];

// BOF Separate Pricing Per Customer
/*		 $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
	  if (tep_db_num_rows ($specials_query)) {
		$specials = tep_db_fetch_array($specials_query);
$products_price = $specials['specials_new_products_price'];
	  } */
  $specials_price = tep_get_products_special_price((int)$prid);
  if (tep_not_null($specials_price)) {
 $products_price = $specials_price;
  } elseif ($this->cg_id != 0){
	$customer_group_price_query = tep_db_query("select customers_group_price from " . TABLE_PRODUCTS_GROUPS . " where products_id = '" . (int)$prid . "' and customers_group_id =  '" . $this->cg_id . "'");
	if ($customer_group_price = tep_db_fetch_array($customer_group_price_query)) {
	$products_price = $customer_group_price['customers_group_price'];
	}
  }
// EOF Separate Pricing Per Customer

	  $this->total += tep_add_tax($products_price, $products_tax) * $qty;
	  $this->weight += ($qty * $products_weight);
	}

// attributes price
// BOF SPPC attributes mod
	if (isset($this->contents[$products_id]['attributes'])) {
	  reset($this->contents[$products_id]['attributes']);
   $where = " AND ((";
	while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
	 $where .= "options_id = '" . (int)$option . "' AND options_values_id = '" . (int)$value . "') OR (";
   }
   $where=substr($where, 0, -5) . ')';

   $attribute_price_query = tep_db_query("SELECT products_attributes_id, options_values_price, price_prefix FROM " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE products_id = '" . (int)$products_id . "'" . $where ."");

   if (tep_db_num_rows($attribute_price_query)) { 
	   $list_of_prdcts_attributes_id = '';
			 // empty array $attribute_price
			 $attribute_price = array();
	   while ($attributes_price_array = tep_db_fetch_array($attribute_price_query)) { 
	   $attribute_price[] =  $attributes_price_array;
	   $list_of_prdcts_attributes_id .= $attributes_price_array['products_attributes_id'].",";
		}
	   if (tep_not_null($list_of_prdcts_attributes_id) && $this->cg_id != '0') { 
	 $select_list_of_prdcts_attributes_ids = "(" . substr($list_of_prdcts_attributes_id, 0 , -1) . ")";
 $pag_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES_GROUPS . " where products_attributes_id IN " . $select_list_of_prdcts_attributes_ids . " AND customers_group_id = '" . $this->cg_id . "'");
 while ($pag_array = tep_db_fetch_array($pag_query)) {
	 $cg_attr_prices[] = $pag_array;
 }

 // substitute options_values_price and prefix for those for the customer group (if available)
 if ($customer_group_id != '0' && tep_not_null($cg_attr_prices)) {
	for ($n = 0; $n < count($attribute_price); $n++) {
	 for ($i = 0; $i < count($cg_attr_prices); $i++) {
		 if ($cg_attr_prices[$i]['products_attributes_id'] == $attribute_price[$n]['products_attributes_id']) {
			$attribute_price[$n]['price_prefix'] = $cg_attr_prices[$i]['price_prefix'];
			$attribute_price[$n]['options_values_price'] = $cg_attr_prices[$i]['options_values_price'];
		 }
	 } // end for ($i = 0; $i < count($cg_att_prices); $i++)
	  }
	} // end if ($customer_group_id != '0' && (tep_not_null($cg_attr_prices))
  } // end if (tep_not_null($list_of_prdcts_attributes_id) && $customer_group_id != '0')
// now loop through array $attribute_price to add up/substract attribute prices

  for ($n = 0; $n < count($attribute_price); $n++) {
		if ($attribute_price[$n]['price_prefix'] == '+') {
		  $this->total += $qty * tep_add_tax($attribute_price[$n]['options_values_price'], $products_tax);
		} else {
		  $this->total -= $qty * tep_add_tax($attribute_price[$n]['options_values_price'], $products_tax);
	}
  } // end for ($n = 0; $n < count($attribute_price); $n++)
	  } // end if (tep_db_num_rows($attribute_price_query))
	} // end if (isset($this->contents[$products_id]['attributes'])) 
  }
}
// EOF SPPC attributes mod

// function attributes_price changed partially according to FalseDawn's post
// http://forums.oscommerce.com/index.php?showtopic=139587
// changed completely for Separate Pricing Per Customer, attributes mod
function attributes_price($products_id) {
// global variable (session) $sppc_customer_group_id -> class variable cg_id
global $sppc_customer_group_id;

if(!tep_session_is_registered('sppc_customer_group_id')) { 
$this->cg_id = '0';
} else {
   $this->cg_id = $sppc_customer_group_id;
}
  if (isset($this->contents[$products_id]['attributes'])) {
	reset($this->contents[$products_id]['attributes']);
   $where = " AND ((";
	while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {
	 $where .= "options_id = '" . (int)$option . "' AND options_values_id = '" . (int)$value . "') OR (";
   }
   $where=substr($where, 0, -5) . ')';

   $attribute_price_query = tep_db_query("SELECT products_attributes_id, options_values_price, price_prefix FROM " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE products_id = '" . (int)$products_id . "'" . $where ."");

  if (tep_db_num_rows($attribute_price_query)) {
	   $list_of_prdcts_attributes_id = '';
	   while ($attributes_price_array = tep_db_fetch_array($attribute_price_query)) { 
	   $attribute_price[] =  $attributes_price_array;
	   $list_of_prdcts_attributes_id .= $attributes_price_array['products_attributes_id'].",";
	  }

	   if (tep_not_null($list_of_prdcts_attributes_id) && $this->cg_id != '0') { 
	 $select_list_of_prdcts_attributes_ids = "(" . substr($list_of_prdcts_attributes_id, 0 , -1) . ")";
 $pag_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES_GROUPS . " where products_attributes_id IN " . $select_list_of_prdcts_attributes_ids . " AND customers_group_id = '" . $this->cg_id . "'");
 while ($pag_array = tep_db_fetch_array($pag_query)) {
	 $cg_attr_prices[] = $pag_array;
 }

 // substitute options_values_price and prefix for those for the customer group (if available)
 if ($customer_group_id != '0' && tep_not_null($cg_attr_prices)) {
	for ($n = 0; $n < count($attribute_price); $n++) {
	 for ($i = 0; $i < count($cg_attr_prices); $i++) {
		 if ($cg_attr_prices[$i]['products_attributes_id'] == $attribute_price[$n]['products_attributes_id']) {
			$attribute_price[$n]['price_prefix'] = $cg_attr_prices[$i]['price_prefix'];
			$attribute_price[$n]['options_values_price'] = $cg_attr_prices[$i]['options_values_price'];
	}
	 } // end for ($i = 0; $i < count($cg_att_prices); $i++)
  }
	} // end if ($customer_group_id != '0' && (tep_not_null($cg_attr_prices))
  } // end if (tep_not_null($list_of_prdcts_attributes_id) && $customer_group_id != '0')
// now loop through array $attribute_price to add up/substract attribute prices

  for ($n = 0; $n < count($attribute_price); $n++) {
		if ($attribute_price[$n]['price_prefix'] == '+') {
		  $attributes_price += $attribute_price[$n]['options_values_price'];
		} else {
		  $attributes_price -= $attribute_price[$n]['options_values_price'];
		}
  } // end for ($n = 0; $n < count($attribute_price); $n++)
  return $attributes_price;
   } else { // end if (tep_db_num_rows($attribute_price_query))
	 return 0;
   } 
 }  else { // end if (isset($this->contents[$products_id]['attributes']))
   return 0;
}
  } // end of function attributes_price, modified for SPPC with attributes

function get_products() {
  global $languages_id;
// BOF Separate Pricing Per Customer
// global variable (session) $sppc_customer_group_id -> class variable cg_id
 global $sppc_customer_group_id;

 if(!tep_session_is_registered('sppc_customer_group_id')) {
 $this->cg_id = '0';
 } else {
  $this->cg_id = $sppc_customer_group_id;
 }
// EOF Separate Pricing Per Customer
  if (!is_array($this->contents)) return false;

  $products_array = array();
  reset($this->contents);
  while (list($products_id, ) = each($this->contents)) {
	$products_query = tep_db_query("select p.products_id, pd.products_name, p.products_model, p.products_image, p.products_price, p.products_weight, p.products_tax_class_id from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = '" . (int)$products_id . "' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "'");
	if ($products = tep_db_fetch_array($products_query)) {
	  $prid = $products['products_id'];
	  $products_price = $products['products_price'];

// BOF Separate Pricing Per Customer
/*		  $specials_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . (int)$prid . "' and status = '1'");
	  if (tep_db_num_rows($specials_query)) {
		$specials = tep_db_fetch_array($specials_query);
$products_price = $specials['specials_new_products_price'];
	  } */
  $specials_price = tep_get_products_special_price($prid);
 if (tep_not_null($specials_price)) {
 $products_price = $specials_price;
  } elseif ($this->cg_id != 0){
	$customer_group_price_query = tep_db_query("select customers_group_price from " . TABLE_PRODUCTS_GROUPS . " where products_id = '" . (int)$prid . "' and customers_group_id =  '" . $this->cg_id . "'");
	if ($customer_group_price = tep_db_fetch_array($customer_group_price_query)) {
	$products_price = $customer_group_price['customers_group_price'];
	}
 }
// EOF Separate Pricing Per Customer

 

Every time I think I have it, my whole site goes down. Any help would be much appreciated.

Thank you in advance

Share this post


Link to post
Share on other sites
I have done everything except merge the catalog/incudes/classes/shopping_cart.php. In the install it says:

catalog/includes/classes/shopping_cart.php

= calculate() adds attribute weight to total weight calculation

 

Every time I think I have it, my whole site goes down. Any help would be much appreciated.

Thank you in advance

 

I think I figured it out. this is what I ended up with I decided to print it out so I could see everything. I think I need a bigger monitor.

...

// attributes price

// BOF SPPC attributes mod

// kumar@farmdev.com

// add-weight-to-product-attributes mod:

// added weight to db query

if (isset($this->contents[$products_id]['attributes'])) {

reset($this->contents[$products_id]['attributes']);

$where = " AND ((";

while (list($option, $value) = each($this->contents[$products_id]['attributes'])) {

$where .= "options_id = '" . (int)$option . "' AND options_values_id = '" . (int)$value . "') OR (";

}

$where=substr($where, 0, -5) . ')';

 

$attribute_price_query = tep_db_query("SELECT products_attributes_id, options_values_price, price_prefix, options_values_weight FROM " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE products_id = '" . (int)$products_id . "'" . $where ."");

$attribute_price = tep_db_fetch_array($attribute_price_query);

if ($attribute_price['price_prefix'] == '+') {

$this->total += $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);

} else {

$this->total -= $qty * tep_add_tax($attribute_price['options_values_price'], $products_tax);

}

if(!empty($attribute_price['options_values_weight'])) {

// kumar@farmdev.com

// add-weight-to-product-attributes mod:

$this->weight += ($qty * $attribute_price['options_values_weight']);

} // END if(!empty($attribute_price['options_values_weight'])) {

 

if (tep_db_num_rows($attribute_price_query)) {

$list_of_prdcts_attributes_id = '';

// empty array $attribute_price

$attribute_price = array();

while ($attributes_price_array = tep_db_fetch_array($attribute_price_query)) {

$attribute_price[] = $attributes_price_array;

$list_of_prdcts_attributes_id .= $attributes_price_array['products_attributes_id'].",";

}

if (tep_not_null($list_of_prdcts_attributes_id) && $this->cg_id != '0') {

$select_list_of_prdcts_attributes_ids = "(" . substr($list_of_prdcts_attributes_id, 0 , -1) . ")";

$pag_query = tep_db_query("select products_attributes_id, options_values_price, price_prefix from " . TABLE_PRODUCTS_ATTRIBUTES_GROUPS . " where products_attributes_id IN " . $select_list_of_prdcts_attributes_ids . " AND customers_group_id = '" . $this->cg_id . "'");

while ($pag_array = tep_db_fetch_array($pag_query)) {

$cg_attr_prices[] = $pag_array;

}

...

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×