| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class WidgetOptionsBuilder |
| 4 |
* |
| 5 |
* @package Packetery |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module; |
| 11 |
|
| 12 |
use Packetery\Core\Entity; |
| 13 |
use Packetery\Core\Entity\Order; |
| 14 |
use Packetery\Module\Carrier\PacketaPickupPointsConfig; |
| 15 |
use Packetery\Module\Options\FeatureFlagManager; |
| 16 |
use Packetery\Module\Order\Repository; |
| 17 |
use WC_Order; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class WidgetOptionsBuilder |
| 21 |
* |
| 22 |
* @package Packetery |
| 23 |
*/ |
| 24 |
class WidgetOptionsBuilder { |
| 25 |
|
| 26 |
/** |
| 27 |
* Internal pickup points config. |
| 28 |
* |
| 29 |
* @var PacketaPickupPointsConfig |
| 30 |
*/ |
| 31 |
private $pickupPointsConfig; |
| 32 |
|
| 33 |
/** |
| 34 |
* RateCalculator. |
| 35 |
* |
| 36 |
* @var RateCalculator |
| 37 |
*/ |
| 38 |
private $rateCalculator; |
| 39 |
|
| 40 |
/** |
| 41 |
* Order repository. |
| 42 |
* |
| 43 |
* @var Repository |
| 44 |
*/ |
| 45 |
private $orderRepository; |
| 46 |
|
| 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 |
public function __construct( |
| 63 |
PacketaPickupPointsConfig $pickupPointsConfig, |
| 64 |
RateCalculator $rateCalculator, |
| 65 |
Repository $orderRepository, |
| 66 |
FeatureFlagManager $featureFlag |
| 67 |
) { |
| 68 |
$this->pickupPointsConfig = $pickupPointsConfig; |
| 69 |
$this->rateCalculator = $rateCalculator; |
| 70 |
$this->orderRepository = $orderRepository; |
| 71 |
$this->featureFlag = $featureFlag; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Gets widget vendors param. |
| 76 |
* |
| 77 |
* @param string $carrierId Carrier id. |
| 78 |
* @param string $country Country. |
| 79 |
* @param array|null $vendorGroups Vendor groups. |
| 80 |
* |
| 81 |
* @return array|null |
| 82 |
*/ |
| 83 |
private function getWidgetVendorsParam( string $carrierId, string $country, ?array $vendorGroups ): ?array { |
| 84 |
if ( is_numeric( $carrierId ) ) { |
| 85 |
return [ |
| 86 |
[ |
| 87 |
'carrierId' => $carrierId, |
| 88 |
'selected' => true, |
| 89 |
], |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
$vendorCarriers = $this->pickupPointsConfig->getVendorCarriers(); |
| 94 |
if ( ! empty( $vendorCarriers[ $carrierId ] ) ) { |
| 95 |
$vendorGroups = [ $vendorCarriers[ $carrierId ]->getGroup() ]; |
| 96 |
} |
| 97 |
|
| 98 |
if ( empty( $vendorGroups ) ) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
$vendorsParam = []; |
| 103 |
foreach ( $vendorGroups as $code ) { |
| 104 |
$groupSettings = [ |
| 105 |
'selected' => true, |
| 106 |
'country' => $country, |
| 107 |
]; |
| 108 |
if ( Entity\Carrier::VENDOR_GROUP_ZPOINT !== $code ) { |
| 109 |
$groupSettings['group'] = $code; |
| 110 |
} |
| 111 |
$vendorsParam[] = $groupSettings; |
| 112 |
} |
| 113 |
|
| 114 |
return $vendorsParam; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Gets widget carriers param. |
| 119 |
* |
| 120 |
* @param bool $isPickupPoints Is context pickup point related. |
| 121 |
* @param string $carrierId Carrier id. |
| 122 |
* |
| 123 |
* @return string|null |
| 124 |
*/ |
| 125 |
private function getCarriersParam( bool $isPickupPoints, string $carrierId ): ?string { |
| 126 |
if ( $isPickupPoints ) { |
| 127 |
return ( is_numeric( $carrierId ) ? $carrierId : Carrier\Repository::INTERNAL_PICKUP_POINTS_ID ); |
| 128 |
} |
| 129 |
|
| 130 |
return null; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Gets carrier configuration for widgets in frontend. |
| 135 |
* |
| 136 |
* @param Entity\Carrier $carrier Carrier configuration. |
| 137 |
* @param float|null $defaultPrice Default price. |
| 138 |
* @param string $optionId Option id. |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function getCarrierForCheckout( Entity\Carrier $carrier, ?float $defaultPrice, string $optionId ): array { |
| 143 |
$carrierConfigForWidget = [ |
| 144 |
'id' => $carrier->getId(), |
| 145 |
'is_pickup_points' => (int) $carrier->hasPickupPoints(), |
| 146 |
'defaultPrice' => $defaultPrice, |
| 147 |
'defaultCurrency' => get_woocommerce_currency(), |
| 148 |
]; |
| 149 |
|
| 150 |
$carrierOption = get_option( $optionId ); |
| 151 |
if ( $carrier->hasPickupPoints() ) { |
| 152 |
if ( $this->featureFlag->isSplitActive() ) { |
| 153 |
$carrierConfigForWidget['vendors'] = $this->getWidgetVendorsParam( |
| 154 |
(string) $carrier->getId(), |
| 155 |
$carrier->getCountry(), |
| 156 |
( ( $carrierOption && isset( $carrierOption['vendor_groups'] ) ) ? $carrierOption['vendor_groups'] : null ) |
| 157 |
); |
| 158 |
} else { |
| 159 |
$carrierConfigForWidget['carriers'] = $this->getCarriersParam( true, $carrier->getId() ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! $carrier->hasPickupPoints() ) { |
| 164 |
$addressValidation = 'none'; |
| 165 |
if ( $carrierOption && in_array( $carrier->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) { |
| 166 |
$addressValidation = ( $carrierOption['address_validation'] ?? $addressValidation ); |
| 167 |
} |
| 168 |
|
| 169 |
$carrierConfigForWidget['address_validation'] = $addressValidation; |
| 170 |
} |
| 171 |
|
| 172 |
return $carrierConfigForWidget; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Creates pickup point widget options in backend. |
| 177 |
* |
| 178 |
* @param Order $order Order. |
| 179 |
* |
| 180 |
* @return array|null |
| 181 |
*/ |
| 182 |
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 |
$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(), |
| 212 |
]; |
| 213 |
|
| 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 ) { |
| 223 |
$widgetOptions['vendors'] = $this->getWidgetVendorsParam( |
| 224 |
$order->getCarrierId(), |
| 225 |
$order->getShippingCountry(), |
| 226 |
null |
| 227 |
); |
| 228 |
} |
| 229 |
} else { |
| 230 |
$widgetOptions['carriers'] = $this->getCarriersParam( $order->isPickupPointDelivery(), $order->getCarrierId() ); |
| 231 |
} |
| 232 |
|
| 233 |
if ( $order->containsAdultContent() ) { |
| 234 |
$widgetOptions += [ 'livePickupPoint' => true ]; |
| 235 |
} |
| 236 |
|
| 237 |
return $widgetOptions; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Creates address validation widget options in backend. |
| 242 |
* |
| 243 |
* @param Order $order Order. |
| 244 |
* |
| 245 |
* @return array |
| 246 |
*/ |
| 247 |
public function createAddressForAdmin( Order $order ): array { |
| 248 |
/** |
| 249 |
* Delivery address is always present in this case. |
| 250 |
* |
| 251 |
* @var Entity\Address $deliveryAddress |
| 252 |
*/ |
| 253 |
$deliveryAddress = $order->getDeliveryAddress(); |
| 254 |
$widgetOptions = [ |
| 255 |
'country' => $order->getShippingCountry(), |
| 256 |
'language' => substr( get_user_locale(), 0, 2 ), |
| 257 |
'layout' => 'hd', |
| 258 |
'appIdentity' => Plugin::getAppIdentity(), |
| 259 |
'street' => $deliveryAddress->getStreet(), |
| 260 |
'city' => $deliveryAddress->getCity(), |
| 261 |
'postcode' => $deliveryAddress->getZip(), |
| 262 |
]; |
| 263 |
|
| 264 |
if ( $deliveryAddress->getHouseNumber() ) { |
| 265 |
$widgetOptions += [ 'houseNumber' => $deliveryAddress->getHouseNumber() ]; |
| 266 |
} |
| 267 |
|
| 268 |
if ( $deliveryAddress->getCounty() ) { |
| 269 |
$widgetOptions += [ 'county' => $deliveryAddress->getCounty() ]; |
| 270 |
} |
| 271 |
|
| 272 |
// TODO: Redo in carrier refactor. |
| 273 |
if ( $order->getCarrier() && is_numeric( $order->getCarrier()->getId() ) ) { |
| 274 |
$widgetOptions += [ 'carrierId' => $order->getCarrier()->getId() ]; |
| 275 |
} |
| 276 |
|
| 277 |
return $widgetOptions; |
| 278 |
} |
| 279 |
|
| 280 |
} |
| 281 |
|