currencySwitcherFacade = $currencySwitcherFacade; } /** * Computes custom rate cost for carrier using cart contents. * * @param Carrier\Options $options Carrier options. * @param float $cartPrice Price. * @param float|int $cartWeight Weight. * @param bool $isFreeShippingCouponApplied Is free shipping coupon applied?. * * @return ?float */ public function getShippingRateCost( Carrier\Options $options, float $cartPrice, $cartWeight, bool $isFreeShippingCouponApplied ): ?float { $cost = null; $carrierOptions = $options->toArray(); if ( isset( $carrierOptions['weight_limits'] ) ) { foreach ( $carrierOptions['weight_limits'] as $weightLimit ) { if ( $cartWeight <= $weightLimit['weight'] ) { $cost = $weightLimit['price']; break; } } } if ( null === $cost ) { return null; } $freeShippingLimit = null; if ( $carrierOptions['free_shipping_limit'] ) { $freeShippingLimit = $this->currencySwitcherFacade->getConvertedPrice( $carrierOptions['free_shipping_limit'] ); if ( $cartPrice >= $freeShippingLimit ) { $cost = 0; } } if ( 0 !== $cost && $isFreeShippingCouponApplied && $options->hasCouponFreeShippingActive() ) { $cost = 0; } $filterParameters = [ 'carrier_id' => $carrierOptions['id'], 'free_shipping_limit' => $freeShippingLimit, 'weight_limits' => $carrierOptions['weight_limits'], ]; /** * Filter shipping rate cost in checkout * * @since 1.4.1 */ return (float) apply_filters( 'packeta_shipping_price', (float) $cost, $filterParameters ); } /** * Tells if free shipping coupon is applied. * * @param WC_Cart|WC_Order $cartOrOrder Cart or order. * * @return bool */ public function isFreeShippingCouponApplied( $cartOrOrder ): bool { $coupons = $cartOrOrder->get_coupons(); foreach ( $coupons as $coupon ) { if ( $coupon->get_free_shipping() ) { return true; } } return false; } }