News:

Looking for documentation? Take a look on our wiki

Main Menu

[PATCH] Cart weight calculation

Started by SinyaWeo, April 30, 2013, 18:40:49 PM

Previous topic - Next topic

SinyaWeo

Hi gentlemen,

here is the patch for cart weight calculation in 'components/com_virtuemart/helpers/cart.php' for VIrtueMart 2.0.20b.

Needed it for price change depending on the cart weight. Hope it helps.

Milan

[attachment cleanup by admin]

Milbo

Whats is wrong with vmShipmentMethod::get_weight($product["virtuemart_product_id"]) * $product->quantity']; ??

What does your patch do? It just does not use this function and simplifies what is already there. We use this function to combine products with different weight units into the unit used by the shop.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

SinyaWeo

Hello,

vmShipmentMethod::get_weight($product["virtuemart_product_id"]) * $product->quantity']; IMHO calculates only weight of the one product times quantity.

My patch counts the weight of the whole cart with all the products in it.

I also did it because I saw @todo comment in the source.

Milan


Milbo

Ahh, you are right. In fact we have the thing running in the shipment plugin and it is provided as function in vmpsplugin, so all shipment/payment plugins have it.

But we should execute it in the cart, so we could have more fun with it. :-) Search for getOrderWeight in vmpsplugin.php (BE/plugins), so you can first write an hack for yourself. Thanks for your post, so I got hte idea to move it directly into the cart.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Masterofspeed

Hello,

I really don't understand what to do with the hack. The txt file doesn't make it clear....

I have the problem, that if an article is 20 gramms, it costs 1 euro to send, but if I order another article of 1 kilo, the shipment of 20 gramms must disappear. And now it won't, its still available.

How can I fix it?
Thanks in advance.

Joomla 2.5.14 and VM 2.0.22a

SinyaWeo

Quote from: Milbo on May 01, 2013, 21:33:13 PM
But we should execute it in the cart, so we could have more fun with it. :-) Search for getOrderWeight in vmpsplugin.php (BE/plugins), so you can first write an hack for yourself. Thanks for your post, so I got hte idea to move it directly into the cart.

Seems like all you have to do is to take the function and move it to mod_virtuemart_cart.php:
/**
* Get the total weight for the order, based on which the proper shipping rate
* can be selected.
*
* @param object $cart Cart object
* @return float Total weight for the order
* @author Oscar van Eijk
*/
protected function getOrderWeight (VirtueMartCart $cart, $to_weight_unit) {

static $weight = 0.0;
if(count($cart->products)>0 and empty($weight)){
foreach ($cart->products as $product) {
vmdebug('getOrderWeight',$product->product_weight);
$weight += (ShopFunctions::convertWeigthUnit ($product->product_weight, $product->product_weight_uom, $to_weight_unit) * $product->quantity);
}
}
return $weight;
}


It works for me.

Masterofspeed

Thanks for the advice.

Where exactly do I place that code?

After the //** @todo WEIGHT CALCULATION that is in the cart.php?
I have VM 2.0.22a installed.

When I paste the code in Drew=amweaver I got a php error hint in red. Any idea what that may be?

SinyaWeo

Quote from: Masterofspeed on August 17, 2013, 16:13:56 PM
Thanks for the advice.

Where exactly do I place that code?

After the //** @todo WEIGHT CALCULATION that is in the cart.php?
I have VM 2.0.22a installed.

When I paste the code in Drew=amweaver I got a php error hint in red. Any idea what that may be?

The answer was for Milbo.

For you it will be on line number 34, right after line with:
$cart = VirtueMartCart::getCart(false);


Do not put there whole function, but only following lines:
static $weight = 0.0;
if(count($cart->products)>0 and empty($weight)){
  foreach ($cart->products as $product) {
$weight += (ShopFunctions::convertWeigthUnit ($product->product_weight, $product->product_weight_uom, $to_weight_unit) * $product->quantity);
  }
}


Then you will have $weight variable with cart weight stored in it.

Jörgen

Hello SinyaWeo

I can´t get this last code to work.
QuoteFor you it will be on line number 34, right after line with:
Code: [Select]

$cart = VirtueMartCart::getCart(false);


Do not put there whole function, but only following lines:
Code: [Select]

   static $weight = 0.0;
   if(count($cart->products)>0 and empty($weight)){
     foreach ($cart->products as $product) {
      $weight += (ShopFunctions::convertWeigthUnit ($product->product_weight, $product->product_weight_uom, $to_weight_unit) * $product->quantity);
     }
   }


Then you will have $weight variable with cart weight stored in it.

I see two problems.

  • It doesn´t give the weight for child products
  • It doesn´t update with Ajax. You need to reload the page.

Any other suggestions ?


Jörgen @ Kreativ Fotografi
Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.

SinyaWeo

Quote from: Jörgen on August 18, 2013, 11:39:48 AM
Hello SinyaWeo

I can´t get this last code to work.

I see two problems.

  • It doesn´t give the weight for child products
  • It doesn´t update with Ajax. You need to reload the page.

Any other suggestions ?


Jörgen @ Kreativ Fotografi

For Ajax maybe the function has to reside in components/com_virtuemart/helpers/cart.php somewhere in prepareAjaxData function.

About child products I don't know how to solve it. I'm not using this feature so I didn't noticed. But maybe when the function is moved to prepareAjaxData it will solve itself ;-)

Jörgen

Hello thanks for a quick reply.

I will check and get back on this.

Jörgen @ kreativ fotografi
Joomla 3.9.18
Virtuemart 3.4.x
Olympiantheme Hera (customized)
This reflects current status when viewing old post.

djolens81

Guys,

please help me about product weight in cart. I'm trying to set weight to change with update of product quantity in cart page, and on the bottom sum of all product weights. I tried to implement your script, but i don't know how to manage to display $weight variable on page. I'm noob with php and i can' figure it how to do it. I got the feeling that i'm so close to solution, but i'm stuck right now.

PLEASE HELP ME !!


Joomla 2.5.14
VM 2.0.22c

[attachment cleanup by admin]

djolens81