| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace PostNL\WooCommerce\Includes\Admin; |
| 6 |
|
| 7 |
use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter; |
| 8 |
use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment; |
| 9 |
use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment; |
| 10 |
use OrderSettings; |
| 11 |
use WC_Order; |
| 12 |
use WCPN_Country_Codes; |
| 13 |
use WCPN_Data; |
| 14 |
use WCPN_Export; |
| 15 |
use WCPN_Settings_Data; |
| 16 |
|
| 17 |
class OrderSettingsRows |
| 18 |
{ |
| 19 |
private const HOME_COUNTRY_ONLY_ROWS = [ |
| 20 |
self::OPTION_SHIPMENT_OPTIONS_AGE_CHECK, |
| 21 |
self::OPTION_SHIPMENT_OPTIONS_ONLY_RECIPIENT, |
| 22 |
self::OPTION_SHIPMENT_OPTIONS_SIGNATURE, |
| 23 |
]; |
| 24 |
|
| 25 |
private const OPTION_CARRIER = "[carrier]"; |
| 26 |
private const OPTION_DELIVERY_TYPE = "[delivery_type]"; |
| 27 |
private const OPTION_EXTRA_OPTIONS_COLLO_AMOUNT = "[extra_options][collo_amount]"; |
| 28 |
private const OPTION_EXTRA_OPTIONS_WEIGHT = "[extra_options][weight]"; |
| 29 |
private const OPTION_PACKAGE_TYPE = "[package_type]"; |
| 30 |
private const OPTION_SHIPMENT_OPTIONS_INSURED = "[shipment_options][insured]"; |
| 31 |
private const OPTION_SHIPMENT_OPTIONS_INSURED_AMOUNT = "[shipment_options][insured_amount]"; |
| 32 |
private const OPTION_SHIPMENT_OPTIONS_LABEL_DESCRIPTION = "[shipment_options][label_description]"; |
| 33 |
private const OPTION_SHIPMENT_OPTIONS_ONLY_RECIPIENT = "[shipment_options][only_recipient]"; |
| 34 |
private const OPTION_SHIPMENT_OPTIONS_RETURN_SHIPMENT = "[shipment_options][return_shipment]"; |
| 35 |
private const OPTION_SHIPMENT_OPTIONS_SIGNATURE = "[shipment_options][signature]"; |
| 36 |
private const OPTION_SHIPMENT_OPTIONS_AGE_CHECK = "[shipment_options][age_check]"; |
| 37 |
|
| 38 |
private const CONDITION_CARRIER_DEFAULT = [ |
| 39 |
"parent_name" => self::OPTION_CARRIER, |
| 40 |
"type" => "show", |
| 41 |
"parent_value" => WCPN_Data::DEFAULT_CARRIER, |
| 42 |
"set_value" => WCPN_Settings_Data::DISABLED, |
| 43 |
]; |
| 44 |
|
| 45 |
private const CONDITION_DELIVERY_TYPE_DELIVERY = [ |
| 46 |
"parent_name" => self::OPTION_DELIVERY_TYPE, |
| 47 |
"type" => "show", |
| 48 |
"parent_value" => [ |
| 49 |
AbstractConsignment::DELIVERY_TYPE_MORNING_NAME, |
| 50 |
AbstractConsignment::DELIVERY_TYPE_STANDARD_NAME, |
| 51 |
AbstractConsignment::DELIVERY_TYPE_EVENING_NAME, |
| 52 |
], |
| 53 |
"set_value" => WCPN_Settings_Data::DISABLED, |
| 54 |
]; |
| 55 |
|
| 56 |
private const CONDITION_PACKAGE_TYPE_PACKAGE = [ |
| 57 |
"parent_name" => self::OPTION_PACKAGE_TYPE, |
| 58 |
"type" => "show", |
| 59 |
"parent_value" => AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME, |
| 60 |
]; |
| 61 |
|
| 62 |
private const CONDITION_FORCE_ENABLED_ON_AGE_CHECK = [ |
| 63 |
"parent_name" => self::OPTION_SHIPMENT_OPTIONS_AGE_CHECK, |
| 64 |
"type" => "disable", |
| 65 |
"set_value" => WCPN_Settings_Data::ENABLED, |
| 66 |
"parent_value" => WCPN_Settings_Data::DISABLED, |
| 67 |
]; |
| 68 |
|
| 69 |
/** |
| 70 |
* @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions |
| 71 |
* @param \WC_Order $order |
| 72 |
* |
| 73 |
* @return array[] |
| 74 |
* @throws \Exception |
| 75 |
*/ |
| 76 |
public static function getOptionsRows( |
| 77 |
AbstractDeliveryOptionsAdapter $deliveryOptions, |
| 78 |
WC_Order $order |
| 79 |
): array { |
| 80 |
$orderSettings = new OrderSettings($deliveryOptions, $order); |
| 81 |
$isHomeCountry = WCPN_Data::isHomeCountry($order->get_shipping_country()); |
| 82 |
$isEuCountry = WCPN_Country_Codes::isEuCountry($order->get_shipping_country()); |
| 83 |
$hasMultiplePackageTypes = count(WCPN_Data::getPackageTypes()) > 1; |
| 84 |
$isPackageTypeDisabled = ! $hasMultiplePackageTypes || $deliveryOptions->isPickup() || ! $isHomeCountry; |
| 85 |
|
| 86 |
$rows = [ |
| 87 |
[ |
| 88 |
"name" => self::OPTION_CARRIER, |
| 89 |
"label" => __("Carrier", "woocommerce-postnl"), |
| 90 |
"type" => "select", |
| 91 |
"options" => WCPN_Data::CARRIERS_HUMAN, |
| 92 |
"custom_attributes" => ["disabled" => "disabled"], |
| 93 |
"value" => $deliveryOptions->getCarrier() ?? PostNLConsignment::CARRIER_NAME, |
| 94 |
], |
| 95 |
[ |
| 96 |
"name" => self::OPTION_DELIVERY_TYPE, |
| 97 |
"label" => __("Delivery type", "woocommerce-postnl"), |
| 98 |
"type" => "select", |
| 99 |
"options" => WCPN_Data::getDeliveryTypesHuman(), |
| 100 |
"custom_attributes" => ["disabled" => "disabled"], |
| 101 |
"value" => $deliveryOptions->getDeliveryType(), |
| 102 |
], |
| 103 |
[ |
| 104 |
"name" => self::OPTION_PACKAGE_TYPE, |
| 105 |
"label" => __("Shipment type", "woocommerce-postnl"), |
| 106 |
"type" => "select", |
| 107 |
"options" => array_combine(WCPN_Data::getPackageTypes(), WCPN_Data::getPackageTypesHuman()), |
| 108 |
"value" => WCPOST()->export->getPackageTypeFromOrder($order, $deliveryOptions), |
| 109 |
"custom_attributes" => $isPackageTypeDisabled |
| 110 |
? ["disabled" => "disabled"] |
| 111 |
: [], |
| 112 |
], |
| 113 |
]; |
| 114 |
|
| 115 |
// Only add extra options and shipment options to home country shipments. |
| 116 |
if ($isHomeCountry) { |
| 117 |
$rows = array_merge($rows, self::getAdditionalOptionsRows($orderSettings)); |
| 118 |
} |
| 119 |
|
| 120 |
$rows[] = [ |
| 121 |
"name" => self::OPTION_SHIPMENT_OPTIONS_LABEL_DESCRIPTION, |
| 122 |
"type" => "text", |
| 123 |
"label" => __("Custom ID (top left on label)", "woocommerce-postnl"), |
| 124 |
"value" => $orderSettings->getLabelDescription(), |
| 125 |
]; |
| 126 |
|
| 127 |
return $rows; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Filters out rows that should not be shown if the shipment is sent to the home country. |
| 132 |
* |
| 133 |
* @param string $cc |
| 134 |
* @param array $rows |
| 135 |
* |
| 136 |
* @return array |
| 137 |
*/ |
| 138 |
public static function filterRowsByCountry(string $cc, array $rows): array |
| 139 |
{ |
| 140 |
if (WCPN_Data::DEFAULT_COUNTRY_CODE === $cc) { |
| 141 |
return $rows; |
| 142 |
} |
| 143 |
|
| 144 |
return array_filter( |
| 145 |
$rows, |
| 146 |
function ($row) { |
| 147 |
return ! in_array($row['name'], self::HOME_COUNTRY_ONLY_ROWS); |
| 148 |
} |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @param \OrderSettings $orderSettings |
| 154 |
* |
| 155 |
* @return array[] |
| 156 |
*/ |
| 157 |
private static function getAdditionalOptionsRows(OrderSettings $orderSettings): array |
| 158 |
{ |
| 159 |
return [ |
| 160 |
[ |
| 161 |
"name" => self::OPTION_EXTRA_OPTIONS_COLLO_AMOUNT, |
| 162 |
"label" => __("Number of labels", "woocommerce-postnl"), |
| 163 |
"type" => "number", |
| 164 |
"value" => $orderSettings->getColloAmount(), |
| 165 |
"custom_attributes" => [ |
| 166 |
"min" => "1", |
| 167 |
"max" => "10", |
| 168 |
], |
| 169 |
"condition" => [ |
| 170 |
self::CONDITION_PACKAGE_TYPE_PACKAGE, |
| 171 |
], |
| 172 |
], |
| 173 |
[ |
| 174 |
"name" => self::OPTION_SHIPMENT_OPTIONS_ONLY_RECIPIENT, |
| 175 |
"type" => "toggle", |
| 176 |
"label" => __("Home address only", "woocommerce-postnl"), |
| 177 |
"help_text" => __( |
| 178 |
"If you don't want the parcel to be delivered at the neighbours, choose this option.", |
| 179 |
"woocommerce-postnl" |
| 180 |
), |
| 181 |
"value" => $orderSettings->hasOnlyRecipient(), |
| 182 |
"condition" => [ |
| 183 |
self::CONDITION_PACKAGE_TYPE_PACKAGE, |
| 184 |
self::CONDITION_DELIVERY_TYPE_DELIVERY, |
| 185 |
self::CONDITION_CARRIER_DEFAULT, |
| 186 |
self::CONDITION_FORCE_ENABLED_ON_AGE_CHECK, |
| 187 |
], |
| 188 |
], |
| 189 |
[ |
| 190 |
"name" => self::OPTION_SHIPMENT_OPTIONS_SIGNATURE, |
| 191 |
"type" => "toggle", |
| 192 |
"label" => __("Signature on delivery", "woocommerce-postnl"), |
| 193 |
"value" => $orderSettings->hasSignature(), |
| 194 |
"help_text" => __( |
| 195 |
"The parcel will be offered at the delivery address. If the recipient is not at home, the parcel will be delivered to the neighbours. In both cases, a signature will be required.", |
| 196 |
"woocommerce-postnl" |
| 197 |
), |
| 198 |
"condition" => [ |
| 199 |
self::CONDITION_PACKAGE_TYPE_PACKAGE, |
| 200 |
self::CONDITION_DELIVERY_TYPE_DELIVERY, |
| 201 |
self::CONDITION_CARRIER_DEFAULT, |
| 202 |
self::CONDITION_FORCE_ENABLED_ON_AGE_CHECK, |
| 203 |
], |
| 204 |
], |
| 205 |
[ |
| 206 |
"name" => self::OPTION_SHIPMENT_OPTIONS_AGE_CHECK, |
| 207 |
"type" => "toggle", |
| 208 |
"label" => __("Age check 18+", "woocommerce-postnl"), |
| 209 |
"help_text" => __( |
| 210 |
"The age check is intended for parcel shipments for which the recipient must show 18+ by means of a proof of identity. With this shipping option Signature for receipt and Delivery only at recipient are included. The age 18+ is further excluded from the delivery options morning and evening delivery.", |
| 211 |
"woocommerce-postnl" |
| 212 |
), |
| 213 |
"value" => $orderSettings->hasAgeCheck(), |
| 214 |
"condition" => [ |
| 215 |
self::CONDITION_PACKAGE_TYPE_PACKAGE, |
| 216 |
self::CONDITION_DELIVERY_TYPE_DELIVERY, |
| 217 |
self::CONDITION_CARRIER_DEFAULT, |
| 218 |
], |
| 219 |
], |
| 220 |
[ |
| 221 |
"name" => self::OPTION_SHIPMENT_OPTIONS_RETURN_SHIPMENT, |
| 222 |
"type" => "toggle", |
| 223 |
"label" => __("Return if no answer", "woocommerce-postnl"), |
| 224 |
"value" => $orderSettings->hasReturnShipment(), |
| 225 |
"help_text" => __( |
| 226 |
"By default, a parcel will be offered twice. After two unsuccessful delivery attempts, the parcel will be available at the nearest pickup point for two weeks. There it can be picked up by the recipient with the note that was left by the courier. If you want to receive the parcel back directly and NOT forward it to the pickup point, enable this option.", |
| 227 |
"woocommerce-postnl" |
| 228 |
), |
| 229 |
"condition" => [ |
| 230 |
self::CONDITION_PACKAGE_TYPE_PACKAGE, |
| 231 |
self::CONDITION_DELIVERY_TYPE_DELIVERY, |
| 232 |
self::CONDITION_CARRIER_DEFAULT, |
| 233 |
], |
| 234 |
], |
| 235 |
[ |
| 236 |
"name" => self::OPTION_SHIPMENT_OPTIONS_INSURED, |
| 237 |
"type" => "toggle", |
| 238 |
"label" => __("Insured", "woocommerce-postnl"), |
| 239 |
"value" => $orderSettings->isInsured(), |
| 240 |
"condition" => [ |
| 241 |
self::CONDITION_PACKAGE_TYPE_PACKAGE, |
| 242 |
self::CONDITION_DELIVERY_TYPE_DELIVERY, |
| 243 |
[ |
| 244 |
"parent_name" => self::OPTION_CARRIER, |
| 245 |
"type" => "disable", |
| 246 |
"parent_value" => WCPN_Data::DEFAULT_CARRIER, |
| 247 |
"set_value" => WCPN_Settings_Data::DISABLED, |
| 248 |
], |
| 249 |
], |
| 250 |
], |
| 251 |
[ |
| 252 |
"name" => self::OPTION_SHIPMENT_OPTIONS_INSURED_AMOUNT, |
| 253 |
"type" => "select", |
| 254 |
"label" => __("Insurance amount", "woocommerce-postnl"), |
| 255 |
"options" => WCPN_Data::getInsuranceAmounts(), |
| 256 |
"value" => $orderSettings->getInsuranceAmount(), |
| 257 |
"condition" => [ |
| 258 |
self::CONDITION_PACKAGE_TYPE_PACKAGE, |
| 259 |
self::CONDITION_DELIVERY_TYPE_DELIVERY, |
| 260 |
self::OPTION_SHIPMENT_OPTIONS_INSURED, |
| 261 |
], |
| 262 |
], |
| 263 |
]; |
| 264 |
} |
| 265 |
} |
| 266 |
|