PluginProbe
Packeta / 1.5.2
Packeta v1.5.2
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / RateCalculator.php

RateCalculator.php in Packeta 1.5.2, at src/Packetery/Module/RateCalculator.php

118 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class RateCalculator.
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module;
11
12 use Packetery\Module\Carrier;
13 use WC_Cart;
14 use WC_Order;
15
16 /**
17 * Class RateCalculator. Calculates cost for WooCommerce shipping rate.
18 *
19 * @package Packetery
20 */
21 class RateCalculator {
22
23 /**
24 * Currency switcher facade.
25 *
26 * @var CurrencySwitcherFacade
27 */
28 private $currencySwitcherFacade;
29
30 /**
31 * RateCalculator constructor.
32 *
33 * @param CurrencySwitcherFacade $currencySwitcherFacade Currency switcher facade.
34 */
35 public function __construct(
36 CurrencySwitcherFacade $currencySwitcherFacade
37 ) {
38 $this->currencySwitcherFacade = $currencySwitcherFacade;
39 }
40
41 /**
42 * Computes custom rate cost for carrier using cart contents.
43 *
44 * @param Carrier\Options $options Carrier options.
45 * @param float $cartPrice Price.
46 * @param float|int $cartWeight Weight.
47 * @param bool $isFreeShippingCouponApplied Is free shipping coupon applied?.
48 *
49 * @return ?float
50 */
51 public function getShippingRateCost(
52 Carrier\Options $options,
53 float $cartPrice,
54 $cartWeight,
55 bool $isFreeShippingCouponApplied
56 ): ?float {
57 $cost = null;
58 $carrierOptions = $options->toArray();
59
60 if ( isset( $carrierOptions['weight_limits'] ) ) {
61 foreach ( $carrierOptions['weight_limits'] as $weightLimit ) {
62 if ( $cartWeight <= $weightLimit['weight'] ) {
63 $cost = $weightLimit['price'];
64 break;
65 }
66 }
67 }
68
69 if ( null === $cost ) {
70 return null;
71 }
72
73 $freeShippingLimit = null;
74 if ( $carrierOptions['free_shipping_limit'] ) {
75 $freeShippingLimit = $this->currencySwitcherFacade->getConvertedPrice( $carrierOptions['free_shipping_limit'] );
76 if ( $cartPrice >= $freeShippingLimit ) {
77 $cost = 0;
78 }
79 }
80
81 if ( 0 !== $cost && $isFreeShippingCouponApplied && $options->hasCouponFreeShippingActive() ) {
82 $cost = 0;
83 }
84
85 $filterParameters = [
86 'carrier_id' => $carrierOptions['id'],
87 'free_shipping_limit' => $freeShippingLimit,
88 'weight_limits' => $carrierOptions['weight_limits'],
89 ];
90
91 /**
92 * Filter shipping rate cost in checkout
93 *
94 * @since 1.4.1
95 */
96 return (float) apply_filters( 'packeta_shipping_price', (float) $cost, $filterParameters );
97 }
98
99 /**
100 * Tells if free shipping coupon is applied.
101 *
102 * @param WC_Cart|WC_Order $cartOrOrder Cart or order.
103 *
104 * @return bool
105 */
106 public function isFreeShippingCouponApplied( $cartOrOrder ): bool {
107 $coupons = $cartOrOrder->get_coupons();
108 foreach ( $coupons as $coupon ) {
109 if ( method_exists( $coupon, 'get_free_shipping' ) && $coupon->get_free_shipping() ) {
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 }
118