order = $order; $this->setDeliveryOptions($deliveryOptions); $this->carrier = $this->deliveryOptions->getCarrier() ?? WCPN_Data::DEFAULT_CARRIER; $this->shipmentOptions = $this->deliveryOptions->getShipmentOptions(); $this->shippingCountry = WCX_Order::get_prop($order, 'shipping_country'); $this->extraOptions = WCX_Order::get_meta($order, WCPOST_Admin::META_SHIPMENT_OPTIONS_EXTRA); $this->setAllData(); } /** * @param bool $inGrams * * @return float */ public function getWeight($inGrams = false): float { return $inGrams ? WCPN_Export::convertWeightToGrams($this->weight) : $this->weight; } /** * @return int */ public function getDigitalStampRangeWeight(): int { return $this->digitalStampRangeWeight; } /** * @return bool */ public function hasAgeCheck(): bool { return $this->ageCheck; } /** * @return int */ public function getColloAmount(): int { return $this->colloAmount; } /** * @return bool */ public function isInsured(): bool { return $this->insured; } /** * @return int */ public function getInsuranceAmount(): int { return $this->insuranceAmount; } /** * @return mixed|string */ public function getLabelDescription(): string { return $this->labelDescription; } /** * @return bool */ public function hasLargeFormat(): bool { return $this->largeFormat; } /** * @return bool */ public function hasOnlyRecipient(): bool { return $this->onlyRecipient; } /** * @return string */ public function getPackageType(): string { return $this->packageType; } /** * @return bool */ public function hasReturnShipment(): bool { return $this->returnShipment; } /** * @return bool */ public function hasSignature(): bool { return $this->signature; } /** * @throws \Exception */ private function setAllData(): void { $this->setPackageType(); $this->setColloAmount(); $this->setLabelDescription(); $this->setAgeCheck(); // $this->setLargeFormat(); $this->setOnlyRecipient(); $this->setReturnShipment(); $this->setSignature(); $this->setInsuranceData(); $this->setWeight(); $this->setDigitalStampRangeWeight(); } /** * @return void */ private function setWeight(): void { $weight = $this->extraOptions['weight'] ?? null; if (null === $weight && $this->order->meta_exists(WCPOST_Admin::META_ORDER_WEIGHT)) { $weight = $this->order->get_meta(WCPOST_Admin::META_ORDER_WEIGHT); } $this->weight = (float) $weight; } /** * @return void */ private function setAgeCheck(): void { $settingName = "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_AGE_CHECK; $ageCheckFromShipmentOptions = $this->shipmentOptions->hasAgeCheck(); $ageCheckOfProduct = $this->getAgeCheckOfProduct(); $ageCheckFromSettings = (bool) WCPOST()->setting_collection->getByName($settingName); $this->ageCheck = $ageCheckFromShipmentOptions ?? $ageCheckOfProduct ?? $ageCheckFromSettings; } /** * Gets product age check value based on if it was explicitly set to either true or false. It defaults to inheriting from the default export settings. * * @return ?bool */ private function getAgeCheckOfProduct(): ?bool { $hasAgeCheck = null; foreach ($this->order->get_items() as $item) { $product = $item->get_product(); if (! $product) { continue; } $productAgeCheck = WCX_Product::get_meta($product, WCPOST_Admin::META_AGE_CHECK, true); if ($productAgeCheck === WCPOST_Admin::PRODUCT_OPTIONS_ENABLED) { return true; } if ($productAgeCheck === WCPOST_Admin::PRODUCT_OPTIONS_DISABLED) { $hasAgeCheck = false; } } return $hasAgeCheck; } /** * @return void */ private function setColloAmount(): void { $this->colloAmount = (int) ($this->extraOptions['collo_amount'] ?? self::DEFAULT_COLLO_AMOUNT); } /** * @return void */ private function setDigitalStampRangeWeight(): void { $savedWeight = $this->extraOptions["digital_stamp_weight"] ?? null; $orderWeight = $this->getWeight(true); $weight = (float) ($savedWeight ?? $orderWeight); $results = Arr::where( WCPN_Data::getDigitalStampRanges(), static function ($range) use ($weight) { return $weight > $range['min']; } ); if (empty($results)) { $digitalStampRangeWeight = Arr::first(WCPN_Data::getDigitalStampRanges())['average']; } else { $digitalStampRangeWeight = Arr::last($results)['average']; } $this->digitalStampRangeWeight = $digitalStampRangeWeight; } /** * Sets insured and insuranceAmount. * * @return void */ private function setInsuranceData(): void { $isInsured = false; $insuranceAmount = 0; $isDefaultInsured = (bool) $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED); $isDefaultInsuredFromPrice = $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_FROM_PRICE); $isDefaultInsuredForBE = (bool) $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_FOR_BE); $orderTotalExceedsInsuredFromPrice = (float) $this->order->get_total() >= (float) $isDefaultInsuredFromPrice; $insuranceFromDeliveryOptions = $this->shipmentOptions->getInsurance(); $isBelgium = AbstractConsignment::CC_BE === $this->getShippingCountry(); $carrier = ConsignmentFactory::createByCarrierName($this->carrier); $amountPossibilities = $carrier::INSURANCE_POSSIBILITIES_LOCAL; if ($insuranceFromDeliveryOptions && $insuranceFromDeliveryOptions >= $amountPossibilities[self::FIRST_INSURANCE]) { $isInsured = (bool) $insuranceFromDeliveryOptions; $insuranceAmount = $insuranceFromDeliveryOptions; } elseif ($isDefaultInsured && $isBelgium) { $isInsured = $insuranceFromDeliveryOptions === 0 ? false : $isDefaultInsuredForBE; $insuranceAmount = $isInsured ? self::DEFAULT_BELGIAN_INSURANCE : 0; } elseif ($isDefaultInsured && $orderTotalExceedsInsuredFromPrice && $insuranceFromDeliveryOptions !== 0) { $isInsured = true; $insuranceAmount = $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_AMOUNT); } $this->insured = $isInsured; $this->insuranceAmount = (int) $insuranceAmount; } /** * @return void */ private function setLabelDescription(): void { $defaultValue = "Order: " . $this->order->get_id(); $valueFromSetting = WCPOST()->setting_collection->getByName(WCPOST_Settings::SETTING_LABEL_DESCRIPTION); $valueFromOrder = $this->shipmentOptions->getLabelDescription(); $this->labelDescription = (string) ($valueFromOrder ?? $valueFromSetting ?? $defaultValue); } /** * @return void */ private function setLargeFormat(): void { $this->largeFormat = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( $this->shipmentOptions->hasLargeFormat(), "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_LARGE_FORMAT ); } /** * @return void */ private function setOnlyRecipient(): void { $this->onlyRecipient = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( $this->shipmentOptions->hasOnlyRecipient(), "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_ONLY_RECIPIENT ); } /** * @return void */ private function setSignature(): void { $this->signature = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( $this->shipmentOptions->hasSignature(), "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_SIGNATURE ); } /** * @return void * @throws \Exception */ private function setPackageType(): void { $packageType = WCPOST()->export->getPackageTypeFromOrder($this->order, $this->deliveryOptions); $this->packageType = $packageType; } /** * @return void */ private function setReturnShipment(): void { $this->returnShipment = (bool) WCPN_Export::getChosenOrDefaultShipmentOption( $this->shipmentOptions->isReturn(), "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_RETURN ); } /** * @param string $settingName * * @return mixed */ private function getCarrierSetting(string $settingName) { return WCPOST()->setting_collection->getByName("{$this->carrier}_" . $settingName); } /** * @return string */ public function getShippingCountry(): string { return $this->shippingCountry; } /** * @return \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter */ public function getDeliveryOptions(): AbstractDeliveryOptionsAdapter { return $this->deliveryOptions; } /** * @return array */ public function getExtraOptions(): array { return $this->extraOptions; } /** * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter|array|null * * @throws \Exception */ private function setDeliveryOptions($deliveryOptions = null): void { if (is_a($deliveryOptions, AbstractDeliveryOptionsAdapter::class)) { $this->deliveryOptions = $deliveryOptions; } else { $this->deliveryOptions = WCPOST_Admin::getDeliveryOptionsFromOrder($this->order, (array) $deliveryOptions); } } }