VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Coding Central => Topic started by: SinyaWeo on April 30, 2013, 18:40:49 PM

Title: [PATCH] Cart weight calculation
Post by: SinyaWeo on April 30, 2013, 18:40:49 PM
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]
Title: Re: [PATCH] Cart weight calculation
Post by: Milbo on April 30, 2013, 22:23:14 PM
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.
Title: Re: [PATCH] Cart weight calculation
Post by: SinyaWeo on April 30, 2013, 23:29:02 PM
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

Title: Re: [PATCH] Cart weight calculation
Post by: Milbo on May 01, 2013, 21:33:13 PM
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.
Title: Re: [PATCH] Cart weight calculation
Post by: Masterofspeed on August 16, 2013, 13:01:14 PM
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
Title: Re: [PATCH] Cart weight calculation
Post by: SinyaWeo on August 17, 2013, 10:38:19 AM
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.
Title: Re: [PATCH] Cart weight calculation
Post by: 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?
Title: Re: [PATCH] Cart weight calculation
Post by: SinyaWeo on August 17, 2013, 17:51:08 PM
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.
Title: Re: [PATCH] Cart weight calculation
Post by: Jörgen on August 18, 2013, 11:39:48 AM
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.

Any other suggestions ?


Jörgen @ Kreativ Fotografi
Title: Re: [PATCH] Cart weight calculation
Post by: SinyaWeo on August 18, 2013, 12:06:21 PM
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 ;-)
Title: Re: [PATCH] Cart weight calculation
Post by: Jörgen on August 18, 2013, 13:17:42 PM
Hello thanks for a quick reply.

I will check and get back on this.

Jörgen @ kreativ fotografi
Title: Re: [PATCH] Cart weight calculation
Post by: djolens81 on September 21, 2013, 17:25:27 PM
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]
Title: Re: [PATCH] Cart weight calculation
Post by: djolens81 on September 22, 2013, 11:03:35 AM
Guys please, anyone??  :(