| @@ -11,11 +11,10 @@ | ||
| 11 | 11 | |
| 12 | 12 | use Packetery\Core\Entity; |
| 13 | 13 | use Packetery\Core\Entity\Order; |
| 14 | 14 | use Packetery\Module\Carrier\PacketaPickupPointsConfig; |
| 15 | -use Packetery\Module\Options\FeatureFlagManager; | |
| 16 | -use Packetery\Module\Order\Repository; | |
| 17 | -use WC_Order; | |
| 15 | +use Packetery\Module\Framework\WpAdapter; | |
| 16 | +use Packetery\Module\Options\FlagManager\FeatureFlagProvider; | |
| 18 | 17 | |
| 19 | 18 | /** |
| 20 | 19 | * Class WidgetOptionsBuilder |
| 21 | 20 | * |
| @@ -30,46 +29,27 @@ | ||
| 30 | 29 | */ |
| 31 | 30 | private $pickupPointsConfig; |
| 32 | 31 | |
| 33 | 32 | /** |
| 34 | - * RateCalculator. | |
| 33 | + * Feature flag. | |
| 35 | 34 | * |
| 36 | - * @var RateCalculator | |
| 35 | + * @var FeatureFlagProvider | |
| 37 | 36 | */ |
| 38 | - private $rateCalculator; | |
| 37 | + private $featureFlagProvider; | |
| 39 | 38 | |
| 40 | 39 | /** |
| 41 | - * Order repository. | |
| 42 | - * | |
| 43 | - * @var Repository | |
| 40 | + * @var WpAdapter | |
| 44 | 41 | */ |
| 45 | - private $orderRepository; | |
| 42 | + private $wpAdapter; | |
| 46 | 43 | |
| 47 | - /** | |
| 48 | - * Feature flag. | |
| 49 | - * | |
| 50 | - * @var FeatureFlagManager | |
| 51 | - */ | |
| 52 | - private $featureFlag; | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * WidgetOptionsBuilder constructor. | |
| 56 | - * | |
| 57 | - * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config. | |
| 58 | - * @param RateCalculator $rateCalculator RateCalculator. | |
| 59 | - * @param Repository $orderRepository Order repository. | |
| 60 | - * @param FeatureFlagManager $featureFlag Feature flag. | |
| 61 | - */ | |
| 62 | 44 | public function __construct( |
| 63 | 45 | PacketaPickupPointsConfig $pickupPointsConfig, |
| 64 | - RateCalculator $rateCalculator, | |
| 65 | - Repository $orderRepository, | |
| 66 | - FeatureFlagManager $featureFlag | |
| 46 | + FeatureFlagProvider $featureFlagProvider, | |
| 47 | + WpAdapter $wpAdapter | |
| 67 | 48 | ) { |
| 68 | - $this->pickupPointsConfig = $pickupPointsConfig; | |
| 69 | - $this->rateCalculator = $rateCalculator; | |
| 70 | - $this->orderRepository = $orderRepository; | |
| 71 | - $this->featureFlag = $featureFlag; | |
| 49 | + $this->pickupPointsConfig = $pickupPointsConfig; | |
| 50 | + $this->featureFlagProvider = $featureFlagProvider; | |
| 51 | + $this->wpAdapter = $wpAdapter; | |
| 72 | 52 | } |
| 73 | 53 | |
| 74 | 54 | /** |
| 75 | 55 | * Gets widget vendors param. |
| @@ -75,13 +55,13 @@ | ||
| 75 | 55 | * Gets widget vendors param. |
| 76 | 56 | * |
| 77 | 57 | * @param string $carrierId Carrier id. |
| 78 | 58 | * @param string $country Country. |
| 79 | - * @param array|null $vendorGroups Vendor groups. | |
| 59 | + * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings. | |
| 80 | 60 | * |
| 81 | - * @return array|null | |
| 61 | + * @return array | |
| 82 | 62 | */ |
| 83 | - private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): ?array { | |
| 63 | + private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array { | |
| 84 | 64 | if ( is_numeric( $carrierId ) ) { |
| 85 | 65 | return [ |
| 86 | 66 | [ |
| 87 | 67 | 'carrierId' => $carrierId, |
| @@ -89,17 +69,19 @@ | ||
| 89 | 69 | ], |
| 90 | 70 | ]; |
| 91 | 71 | } |
| 92 | 72 | |
| 93 | - $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers(); | |
| 94 | - if ( ! empty( $vendorCarriers[ $carrierId ] ) ) { | |
| 95 | - $vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ]; | |
| 73 | + if ( ! isset( $vendorGroups ) || count( $vendorGroups ) === 0 ) { | |
| 74 | + if ( $this->pickupPointsConfig->isCompoundCarrierId( $carrierId ) ) { | |
| 75 | + $vendorGroups = $this->pickupPointsConfig->getCompoundCarrierVendorGroups( $carrierId ); | |
| 76 | + } else { | |
| 77 | + $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers(); | |
| 78 | + if ( isset( $vendorCarriers[ $carrierId ] ) ) { | |
| 79 | + $vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ]; | |
| 80 | + } | |
| 81 | + } | |
| 96 | 82 | } |
| 97 | 83 | |
| 98 | - if ( empty( $vendorGroups ) ) { | |
| 99 | - return null; | |
| 100 | - } | |
| 101 | - | |
| 102 | 84 | $vendorsParam = []; |
| 103 | 85 | foreach ( $vendorGroups as $code ) { |
| 104 | 86 | $groupSettings = [ |
| 105 | 87 | 'selected' => true, |
| @@ -104,9 +86,9 @@ | ||
| 104 | 86 | $groupSettings = [ |
| 105 | 87 | 'selected' => true, |
| 106 | 88 | 'country' => $country, |
| 107 | 89 | ]; |
| 108 | - if ( Entity\Carrier::VENDOR_GROUP_ZPOINT !== $code ) { | |
| 90 | + if ( $code !== Entity\Carrier::VENDOR_GROUP_ZPOINT ) { | |
| 109 | 91 | $groupSettings['group'] = $code; |
| 110 | 92 | } |
| 111 | 93 | $vendorsParam[] = $groupSettings; |
| 112 | 94 | } |
| @@ -123,9 +105,9 @@ | ||
| 123 | 105 | * @return string|null |
| 124 | 106 | */ |
| 125 | 107 | private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string { |
| 126 | 108 | if ( $isPickupPoints ) { |
| 127 | - return ( is_numeric( $carrierId ) ? $carrierId : Carrier\Repository::INTERNAL_PICKUP_POINTS_ID ); | |
| 109 | + return ( is_numeric( $carrierId ) ? $carrierId : Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ); | |
| 128 | 110 | } |
| 129 | 111 | |
| 130 | 112 | return null; |
| 131 | 113 | } |
| @@ -133,28 +115,25 @@ | ||
| 133 | 115 | /** |
| 134 | 116 | * Gets carrier configuration for widgets in frontend. |
| 135 | 117 | * |
| 136 | 118 | * @param Entity\Carrier $carrier Carrier configuration. |
| 137 | - * @param float|null $defaultPrice Default price. | |
| 138 | 119 | * @param string $optionId Option id. |
| 139 | 120 | * |
| 140 | 121 | * @return array |
| 141 | 122 | */ |
| 142 | - public function getCarrierForCheckout( Entity\Carrier $carrier, ?float $defaultPrice, string $optionId ): array { | |
| 123 | + public function getCarrierForCheckout( Entity\Carrier $carrier, string $optionId ): array { | |
| 143 | 124 | $carrierConfigForWidget = [ |
| 144 | 125 | 'id' => $carrier->getId(), |
| 145 | 126 | 'is_pickup_points' => (int) $carrier->hasPickupPoints(), |
| 146 | - 'defaultPrice' => $defaultPrice, | |
| 147 | - 'defaultCurrency' => get_woocommerce_currency(), | |
| 148 | 127 | ]; |
| 149 | 128 | |
| 150 | - $carrierOption = get_option( $optionId ); | |
| 129 | + $carrierOption = $this->wpAdapter->getOption( $optionId ); | |
| 151 | 130 | if ( $carrier->hasPickupPoints() ) { |
| 152 | - if ( $this->featureFlag->isSplitActive() ) { | |
| 131 | + if ( $this->featureFlagProvider->isSplitActive() ) { | |
| 153 | 132 | $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam( |
| 154 | - (string) $carrier->getId(), | |
| 133 | + $carrier->getId(), | |
| 155 | 134 | $carrier->getCountry(), |
| 156 | - ( ( $carrierOption && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null ) | |
| 135 | + ( ( ( $carrierOption !== null && $carrierOption !== false ) && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null ) | |
| 157 | 136 | ); |
| 158 | 137 | } else { |
| 159 | 138 | $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() ); |
| 160 | 139 | } |
| @@ -161,9 +140,9 @@ | ||
| 161 | 140 | } |
| 162 | 141 | |
| 163 | 142 | if ( ! $carrier->hasPickupPoints() ) { |
| 164 | 143 | $addressValidation = 'none'; |
| 165 | - if ( $carrierOption && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) { | |
| 144 | + if ( ( $carrierOption !== null && $carrierOption !== false ) && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) { | |
| 166 | 145 | $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation ); |
| 167 | 146 | } |
| 168 | 147 | |
| 169 | 148 | $carrierConfigForWidget['address_validation'] = $addressValidation; |
| @@ -176,62 +155,32 @@ | ||
| 176 | 155 | * Creates pickup point widget options in backend. |
| 177 | 156 | * |
| 178 | 157 | * @param Order $order Order. |
| 179 | 158 | * |
| 180 | - * @return array|null | |
| 159 | + * @return array | |
| 181 | 160 | */ |
| 182 | 161 | public function createPickupPointForAdmin( Order $order ): array { |
| 183 | - if ( $order->isExternalCarrier() ) { | |
| 184 | - $carrierId = $order->getCarrierId(); | |
| 185 | - } else { | |
| 186 | - $carrierId = $this->pickupPointsConfig->getCompoundCarrierIdByCountry( $order->getShippingCountry() ); | |
| 187 | - } | |
| 188 | - | |
| 189 | - $defaultPrice = null; | |
| 190 | - if ( null !== $carrierId ) { | |
| 191 | - /** | |
| 192 | - * Cannot be null due to the conditions in the caller method. | |
| 193 | - * | |
| 194 | - * @var WC_Order $wcOrder | |
| 195 | - */ | |
| 196 | - $wcOrder = $this->orderRepository->getWcOrderById( (int) $order->getNumber() ); | |
| 197 | - $defaultPrice = $this->rateCalculator->getShippingRateCost( | |
| 198 | - Carrier\Options::createByCarrierId( $carrierId ), | |
| 199 | - $wcOrder->get_total() - $wcOrder->get_shipping_total(), | |
| 200 | - $order->getWeight(), | |
| 201 | - $this->rateCalculator->isFreeShippingCouponApplied( $wcOrder ) | |
| 202 | - ); | |
| 203 | - } | |
| 204 | - | |
| 205 | 162 | $widgetOptions = [ |
| 206 | - 'country' => $order->getShippingCountry(), | |
| 207 | - 'language' => substr( get_user_locale(), 0, 2 ), | |
| 208 | - 'appIdentity' => Plugin::getAppIdentity(), | |
| 209 | - 'weight' => $order->getFinalWeight(), | |
| 210 | - 'defaultPrice' => $defaultPrice, | |
| 211 | - 'defaultCurrency' => $order->getCurrency(), | |
| 163 | + 'country' => $order->getShippingCountry(), | |
| 164 | + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 165 | + 'appIdentity' => Plugin::getAppIdentity(), | |
| 166 | + 'weight' => $order->getFinalWeight(), | |
| 212 | 167 | ]; |
| 213 | 168 | |
| 214 | - // TODO: update later when carrier will not be nullable. | |
| 215 | - $orderCarrier = $order->getCarrier(); | |
| 216 | - if ( $orderCarrier ) { | |
| 217 | - $widgetOptions['defaultCurrency'] = $orderCarrier->getCurrency(); | |
| 218 | - } | |
| 219 | - | |
| 220 | - if ( $this->featureFlag->isSplitActive() ) { | |
| 221 | - // In backend, we want all pickup points in that country for packeta carrier. | |
| 222 | - if ( $order->getCarrierId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) { | |
| 169 | + if ( $this->featureFlagProvider->isSplitActive() ) { | |
| 170 | + // In backend, we want all pickup points in that country for Packeta carrier. | |
| 171 | + if ( $order->getCarrier()->getId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) { | |
| 223 | 172 | $widgetOptions['vendors'] = $this->getWidgetVendorsParam( |
| 224 | - $order->getCarrierId(), | |
| 173 | + $order->getCarrier()->getId(), | |
| 225 | 174 | $order->getShippingCountry(), |
| 226 | 175 | null |
| 227 | 176 | ); |
| 228 | 177 | } |
| 229 | 178 | } else { |
| 230 | - $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrierId() ); | |
| 179 | + $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrier()->getId() ); | |
| 231 | 180 | } |
| 232 | 181 | |
| 233 | - if ( $order->containsAdultContent() ) { | |
| 182 | + if ( $order->containsAdultContent() === true ) { | |
| 234 | 183 | $widgetOptions += [ 'livePickupPoint' => true ]; |
| 235 | 184 | } |
| 236 | 185 | |
| 237 | 186 | return $widgetOptions; |
| @@ -252,9 +201,9 @@ | ||
| 252 | 201 | */ |
| 253 | 202 | $deliveryAddress = $order->getDeliveryAddress(); |
| 254 | 203 | $widgetOptions = [ |
| 255 | 204 | 'country' => $order->getShippingCountry(), |
| 256 | - 'language' => substr( get_user_locale(), 0, 2 ), | |
| 205 | + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 257 | 206 | 'layout' => 'hd', |
| 258 | 207 | 'appIdentity' => Plugin::getAppIdentity(), |
| 259 | 208 | 'street' => $deliveryAddress->getStreet(), |
| 260 | 209 | 'city' => $deliveryAddress->getCity(), |
| @@ -260,21 +209,19 @@ | ||
| 260 | 209 | 'city' => $deliveryAddress->getCity(), |
| 261 | 210 | 'postcode' => $deliveryAddress->getZip(), |
| 262 | 211 | ]; |
| 263 | 212 | |
| 264 | - if ( $deliveryAddress->getHouseNumber() ) { | |
| 213 | + if ( $deliveryAddress->getHouseNumber() !== null ) { | |
| 265 | 214 | $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ]; |
| 266 | 215 | } |
| 267 | 216 | |
| 268 | - if ( $deliveryAddress->getCounty() ) { | |
| 217 | + if ( $deliveryAddress->getCounty() !== null ) { | |
| 269 | 218 | $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ]; |
| 270 | 219 | } |
| 271 | 220 | |
| 272 | - // TODO: Redo in carrier refactor. | |
| 273 | - if ( $order->getCarrier() && is_numeric( $order->getCarrier()->getId() ) ) { | |
| 221 | + if ( is_numeric( $order->getCarrier()->getId() ) ) { | |
| 274 | 222 | $widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ]; |
| 275 | 223 | } |
| 276 | 224 | |
| 277 | 225 | return $widgetOptions; |
| 278 | 226 | } |
| 279 | - | |
| 280 | 227 | } |