41 lines
998 B
PHP
41 lines
998 B
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Cart\Plugin\Shipping;
|
|
|
|
use Esun\Cart\Model\ShippingEstimate;
|
|
use Magento\OfflineShipping\Model\Carrier\Flatrate;
|
|
use Magento\Quote\Model\Quote\Address\RateRequest;
|
|
use Magento\Shipping\Model\Rate\Result;
|
|
|
|
class FlatratePlugin
|
|
{
|
|
public function __construct(
|
|
private readonly ShippingEstimate $shippingEstimate
|
|
) {
|
|
}
|
|
|
|
public function afterCollectRates(
|
|
Flatrate $subject,
|
|
Result|bool $result,
|
|
RateRequest $request
|
|
): Result|bool {
|
|
if (!$result instanceof Result) {
|
|
return $result;
|
|
}
|
|
|
|
$isFreeShipping = (bool)$request->getFreeShipping()
|
|
|| $this->shippingEstimate->isFreeShippingForItems((array)$request->getAllItems());
|
|
if (!$isFreeShipping) {
|
|
return $result;
|
|
}
|
|
|
|
foreach ($result->getAllRates() as $rate) {
|
|
$rate->setPrice(0.0);
|
|
$rate->setCost(0.0);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|