0

I'm trying to add a weight limitation for a payment module in oscommerce. So if the total items in cart has a weight above 2kg I don't want to display it as an payment option.

I tried to do something like this:

    for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
        if ($order->products[$i]['weight'] > '2') {
          $this->enabled = false;
        }
}

When I echo it, the total weight is zero.

I manage to exclude the payment option when the cart contains above 25 items with with the following code:

$total_units = 0;
for ($i=0, $j=count($order->products); $i<$j; $i++) {
$total_units += $order->products[$i]['qty'];
}
if ($total_units > '25') {
          $this->enabled = false;
        }
double-beep
  • 5,031
  • 17
  • 33
  • 41
  • 1
    Your code triggers if any one item is over 2kg, not if the sum of all items is over 2kg. You need to add up the weights like you did for the counts. – Alex Howansky Nov 05 '17 at 17:22
  • I totally missed this, thanks for pointing it out. It worked on the first attempt. Thanks! – Fredrik Johansson Nov 05 '17 at 17:32
  • I noticed it worked partially, if I have more than one item of a product it only add the weight for one of those products. How can I modify it so it calculate all items in the cart. The item counts shows correct, even if there is more than one of the same product. – Fredrik Johansson Nov 05 '17 at 17:44
  • 1
    `products[$i]['weight']` is probably the per-unit weight? Just multiply it by `products[$i]['qty']` if so. – Alex Howansky Nov 05 '17 at 17:57
  • Thank you so much! It seems to work like a charm. – Fredrik Johansson Nov 05 '17 at 19:05

0 Answers0