| @@ -11,9 +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; | |
| 15 | +use Packetery\Module\Framework\WpAdapter; | |
| 16 | +use Packetery\Module\Options\FlagManager\FeatureFlagProvider; | |
| 16 | 17 | |
| 17 | 18 | /** |
| 18 | 19 | * Class WidgetOptionsBuilder |
| 19 | 20 | * |
| @@ -30,24 +31,25 @@ | ||
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * Feature flag. |
| 33 | 34 | * |
| 34 | - * @var FeatureFlagManager | |
| 35 | + * @var FeatureFlagProvider | |
| 35 | 36 | */ |
| 36 | - private $featureFlag; | |
| 37 | + private $featureFlagProvider; | |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | - * WidgetOptionsBuilder constructor. | |
| 40 | - * | |
| 41 | - * @param PacketaPickupPointsConfig $pickupPointsConfig Internal pickup points config. | |
| 42 | - * @param FeatureFlagManager $featureFlag Feature flag. | |
| 40 | + * @var WpAdapter | |
| 43 | 41 | */ |
| 42 | + private $wpAdapter; | |
| 43 | + | |
| 44 | 44 | public function __construct( |
| 45 | 45 | PacketaPickupPointsConfig $pickupPointsConfig, |
| 46 | - FeatureFlagManager $featureFlag | |
| 46 | + FeatureFlagProvider $featureFlagProvider, | |
| 47 | + WpAdapter $wpAdapter | |
| 47 | 48 | ) { |
| 48 | - $this->pickupPointsConfig = $pickupPointsConfig; | |
| 49 | - $this->featureFlag = $featureFlag; | |
| 49 | + $this->pickupPointsConfig = $pickupPointsConfig; | |
| 50 | + $this->featureFlagProvider = $featureFlagProvider; | |
| 51 | + $this->wpAdapter = $wpAdapter; | |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | 54 | /** |
| 53 | 55 | * Gets widget vendors param. |
| @@ -53,13 +55,13 @@ | ||
| 53 | 55 | * Gets widget vendors param. |
| 54 | 56 | * |
| 55 | 57 | * @param string $carrierId Carrier id. |
| 56 | 58 | * @param string $country Country. |
| 57 | - * @param array|null $vendorGroups Vendor groups. | |
| 59 | + * @param array|null $vendorGroups Vendor groups when checked in compound carrier settings. | |
| 58 | 60 | * |
| 59 | - * @return array|null | |
| 61 | + * @return array | |
| 60 | 62 | */ |
| 61 | - private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): ?array { | |
| 63 | + private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): array { | |
| 62 | 64 | if ( is_numeric( $carrierId ) ) { |
| 63 | 65 | return [ |
| 64 | 66 | [ |
| 65 | 67 | 'carrierId' => $carrierId, |
| @@ -67,17 +69,19 @@ | ||
| 67 | 69 | ], |
| 68 | 70 | ]; |
| 69 | 71 | } |
| 70 | 72 | |
| 71 | - $vendorCarriers = $this->pickupPointsConfig->getVendorCarriers(); | |
| 72 | - if ( ! empty( $vendorCarriers[ $carrierId ] ) ) { | |
| 73 | - $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 | + } | |
| 74 | 82 | } |
| 75 | 83 | |
| 76 | - if ( empty( $vendorGroups ) ) { | |
| 77 | - return null; | |
| 78 | - } | |
| 79 | - | |
| 80 | 84 | $vendorsParam = []; |
| 81 | 85 | foreach ( $vendorGroups as $code ) { |
| 82 | 86 | $groupSettings = [ |
| 83 | 87 | 'selected' => true, |
| @@ -82,9 +86,9 @@ | ||
| 82 | 86 | $groupSettings = [ |
| 83 | 87 | 'selected' => true, |
| 84 | 88 | 'country' => $country, |
| 85 | 89 | ]; |
| 86 | - if ( Entity\Carrier::VENDOR_GROUP_ZPOINT !== $code ) { | |
| 90 | + if ( $code !== Entity\Carrier::VENDOR_GROUP_ZPOINT ) { | |
| 87 | 91 | $groupSettings['group'] = $code; |
| 88 | 92 | } |
| 89 | 93 | $vendorsParam[] = $groupSettings; |
| 90 | 94 | } |
| @@ -111,28 +115,25 @@ | ||
| 111 | 115 | /** |
| 112 | 116 | * Gets carrier configuration for widgets in frontend. |
| 113 | 117 | * |
| 114 | 118 | * @param Entity\Carrier $carrier Carrier configuration. |
| 115 | - * @param float|null $defaultPrice Default price. | |
| 116 | 119 | * @param string $optionId Option id. |
| 117 | 120 | * |
| 118 | 121 | * @return array |
| 119 | 122 | */ |
| 120 | - public function getCarrierForCheckout( Entity\Carrier $carrier, ?float $defaultPrice, string $optionId ): array { | |
| 123 | + public function getCarrierForCheckout( Entity\Carrier $carrier, string $optionId ): array { | |
| 121 | 124 | $carrierConfigForWidget = [ |
| 122 | 125 | 'id' => $carrier->getId(), |
| 123 | 126 | 'is_pickup_points' => (int) $carrier->hasPickupPoints(), |
| 124 | - 'defaultPrice' => $defaultPrice, | |
| 125 | - 'defaultCurrency' => get_woocommerce_currency(), | |
| 126 | 127 | ]; |
| 127 | 128 | |
| 128 | - $carrierOption = get_option( $optionId ); | |
| 129 | + $carrierOption = $this->wpAdapter->getOption( $optionId ); | |
| 129 | 130 | if ( $carrier->hasPickupPoints() ) { |
| 130 | - if ( $this->featureFlag->isSplitActive() ) { | |
| 131 | + if ( $this->featureFlagProvider->isSplitActive() ) { | |
| 131 | 132 | $carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam( |
| 132 | 133 | $carrier->getId(), |
| 133 | 134 | $carrier->getCountry(), |
| 134 | - ( ( $carrierOption && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null ) | |
| 135 | + ( ( ( $carrierOption !== null && $carrierOption !== false ) && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null ) | |
| 135 | 136 | ); |
| 136 | 137 | } else { |
| 137 | 138 | $carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() ); |
| 138 | 139 | } |
| @@ -139,9 +140,9 @@ | ||
| 139 | 140 | } |
| 140 | 141 | |
| 141 | 142 | if ( ! $carrier->hasPickupPoints() ) { |
| 142 | 143 | $addressValidation = 'none'; |
| 143 | - 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 ) ) { | |
| 144 | 145 | $addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation ); |
| 145 | 146 | } |
| 146 | 147 | |
| 147 | 148 | $carrierConfigForWidget['address_validation'] = $addressValidation; |
| @@ -154,20 +155,20 @@ | ||
| 154 | 155 | * Creates pickup point widget options in backend. |
| 155 | 156 | * |
| 156 | 157 | * @param Order $order Order. |
| 157 | 158 | * |
| 158 | - * @return array|null | |
| 159 | + * @return array | |
| 159 | 160 | */ |
| 160 | 161 | public function createPickupPointForAdmin( Order $order ): array { |
| 161 | 162 | $widgetOptions = [ |
| 162 | 163 | 'country' => $order->getShippingCountry(), |
| 163 | - 'language' => substr( get_user_locale(), 0, 2 ), | |
| 164 | + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 164 | 165 | 'appIdentity' => Plugin::getAppIdentity(), |
| 165 | 166 | 'weight' => $order->getFinalWeight(), |
| 166 | 167 | ]; |
| 167 | 168 | |
| 168 | - if ( $this->featureFlag->isSplitActive() ) { | |
| 169 | - // In backend, we want all pickup points in that country for packeta carrier. | |
| 169 | + if ( $this->featureFlagProvider->isSplitActive() ) { | |
| 170 | + // In backend, we want all pickup points in that country for Packeta carrier. | |
| 170 | 171 | if ( $order->getCarrier()->getId() !== Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) { |
| 171 | 172 | $widgetOptions['vendors'] = $this->getWidgetVendorsParam( |
| 172 | 173 | $order->getCarrier()->getId(), |
| 173 | 174 | $order->getShippingCountry(), |
| @@ -177,9 +178,9 @@ | ||
| 177 | 178 | } else { |
| 178 | 179 | $widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrier()->getId() ); |
| 179 | 180 | } |
| 180 | 181 | |
| 181 | - if ( $order->containsAdultContent() ) { | |
| 182 | + if ( $order->containsAdultContent() === true ) { | |
| 182 | 183 | $widgetOptions += [ 'livePickupPoint' => true ]; |
| 183 | 184 | } |
| 184 | 185 | |
| 185 | 186 | return $widgetOptions; |
| @@ -200,9 +201,9 @@ | ||
| 200 | 201 | */ |
| 201 | 202 | $deliveryAddress = $order->getDeliveryAddress(); |
| 202 | 203 | $widgetOptions = [ |
| 203 | 204 | 'country' => $order->getShippingCountry(), |
| 204 | - 'language' => substr( get_user_locale(), 0, 2 ), | |
| 205 | + 'language' => substr( $this->wpAdapter->getUserLocale(), 0, 2 ), | |
| 205 | 206 | 'layout' => 'hd', |
| 206 | 207 | 'appIdentity' => Plugin::getAppIdentity(), |
| 207 | 208 | 'street' => $deliveryAddress->getStreet(), |
| 208 | 209 | 'city' => $deliveryAddress->getCity(), |
| @@ -208,13 +209,13 @@ | ||
| 208 | 209 | 'city' => $deliveryAddress->getCity(), |
| 209 | 210 | 'postcode' => $deliveryAddress->getZip(), |
| 210 | 211 | ]; |
| 211 | 212 | |
| 212 | - if ( $deliveryAddress->getHouseNumber() ) { | |
| 213 | + if ( $deliveryAddress->getHouseNumber() !== null ) { | |
| 213 | 214 | $widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ]; |
| 214 | 215 | } |
| 215 | 216 | |
| 216 | - if ( $deliveryAddress->getCounty() ) { | |
| 217 | + if ( $deliveryAddress->getCounty() !== null ) { | |
| 217 | 218 | $widgetOptions += [ 'county' => $deliveryAddress->getCounty() ]; |
| 218 | 219 | } |
| 219 | 220 | |
| 220 | 221 | if ( is_numeric( $order->getCarrier()->getId() ) ) { |
| @@ -222,6 +223,5 @@ | ||
| 222 | 223 | } |
| 223 | 224 | |
| 224 | 225 | return $widgetOptions; |
| 225 | 226 | } |
| 226 | - | |
| 227 | 227 | } |