Skip to content

Commit

Permalink
fix for free shipping coupon
Browse files Browse the repository at this point in the history
  • Loading branch information
trincoingels committed Feb 7, 2024
1 parent 5d032a9 commit 0cfdb2c
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions Model/Carrier/Parcelpro.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public function collectRates(RateRequest $request)
->getQuote()->getGrandTotal();
}

$freeBoxes = $this->getFreeBoxesCount($request);

if ($_pricIncl) {
$total = $grandTotal; // Verzendkosten berekenen op basis van bedrag incl. BTW
}
Expand Down Expand Up @@ -218,8 +220,8 @@ public function collectRates(RateRequest $request)
}
$method->setMethod($key);
$method->setMethodTitle($pricerule['titel']);
$method->setPrice($request->getFreeShipping() === true ? 0 : $shippingPrice);
$method->setCost($request->getFreeShipping() === true ? 0 : $shippingPrice);
$method->setPrice($request->getFreeShipping() === true || $request->getPackageQty() == $freeBoxes ? 0 : $shippingPrice);
$method->setCost($request->getFreeShipping() === true || $request->getPackageQty() == $freeBoxes ? 0 : $shippingPrice);
$result->append($method);
}

Expand All @@ -244,8 +246,8 @@ public function collectRates(RateRequest $request)

$method->setMethod($key . "_" . $counter);
$method->setMethodTitle($pricerule['titel']);
$method->setPrice($request->getFreeShipping() === true ? 0 : $shippingPrice);
$method->setCost($request->getFreeShipping() === true ? 0 : $shippingPrice);
$method->setPrice($request->getFreeShipping() === true || $request->getPackageQty() == $freeBoxes ? 0 : $shippingPrice);
$method->setCost($request->getFreeShipping() === true || $request->getPackageQty() == $freeBoxes ? 0 : $shippingPrice);
$result->append($method);
}
}
Expand Down Expand Up @@ -347,4 +349,42 @@ private function isBeforeLastShippingTime($rawLastTime): bool
$now = new DateTime();
return $now < $parsed;
}

/**
* @param RateRequest $request
* @return int
*/
private function getFreeBoxesCount(RateRequest $request)
{
$freeBoxes = 0;
if ($request->getAllItems()) {
foreach ($request->getAllItems() as $item) {
if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
continue;
}

if ($item->getHasChildren() && $item->isShipSeparately()) {
$freeBoxes += $this->getFreeBoxesCountFromChildren($item);
} elseif ($item->getFreeShipping()) {
$freeBoxes += $item->getQty();
}
}
}
return $freeBoxes;
}

/**
* @param mixed $item
* @return mixed
*/
private function getFreeBoxesCountFromChildren($item)
{
$freeBoxes = 0;
foreach ($item->getChildren() as $child) {
if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
$freeBoxes += $item->getQty() * $child->getQty();
}
}
return $freeBoxes;
}
}

0 comments on commit 0cfdb2c

Please sign in to comment.