| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Metabox |
| 4 |
* |
| 5 |
* @package Packetery\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace Packetery\Module\Order; |
| 11 |
|
| 12 |
use Packetery\Core\Entity; |
| 13 |
use Packetery\Core\Helper; |
| 14 |
use Packetery\Core\Validator; |
| 15 |
use Packetery\Module; |
| 16 |
use Packetery\Module\Carrier\EntityRepository; |
| 17 |
use Packetery\Module\Exception\InvalidCarrierException; |
| 18 |
use Packetery\Module\FormFactory; |
| 19 |
use Packetery\Module\FormValidators; |
| 20 |
use Packetery\Module\Log; |
| 21 |
use Packetery\Module\MessageManager; |
| 22 |
use Packetery\Module\Options; |
| 23 |
use Packetery\Module\Plugin; |
| 24 |
use Packetery\Module\WidgetOptionsBuilder; |
| 25 |
use Packetery\Latte\Engine; |
| 26 |
use Packetery\Nette\Forms\Form; |
| 27 |
use Packetery\Nette\Http\Request; |
| 28 |
use WC_Data_Exception; |
| 29 |
use WC_Order; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class Metabox |
| 33 |
* |
| 34 |
* @package Packetery\Order |
| 35 |
*/ |
| 36 |
class Metabox { |
| 37 |
|
| 38 |
public const FIELD_WEIGHT = 'packetery_weight'; |
| 39 |
private const FIELD_ORIGINAL_WEIGHT = 'packetery_original_weight'; |
| 40 |
public const FIELD_WIDTH = 'packetery_width'; |
| 41 |
public const FIELD_LENGTH = 'packetery_length'; |
| 42 |
public const FIELD_HEIGHT = 'packetery_height'; |
| 43 |
public const FIELD_ADULT_CONTENT = 'packetery_adult_content'; |
| 44 |
public const FIELD_COD = 'packetery_COD'; |
| 45 |
public const FIELD_VALUE = 'packetery_value'; |
| 46 |
public const FIELD_DELIVER_ON = 'packetery_deliver_on'; |
| 47 |
|
| 48 |
/** |
| 49 |
* PacketeryLatte engine. |
| 50 |
* |
| 51 |
* @var Engine |
| 52 |
*/ |
| 53 |
private $latte_engine; |
| 54 |
|
| 55 |
/** |
| 56 |
* Message manager. |
| 57 |
* |
| 58 |
* @var MessageManager |
| 59 |
*/ |
| 60 |
private $message_manager; |
| 61 |
|
| 62 |
/** |
| 63 |
* Helper. |
| 64 |
* |
| 65 |
* @var Helper |
| 66 |
*/ |
| 67 |
private $helper; |
| 68 |
|
| 69 |
/** |
| 70 |
* HTTP request. |
| 71 |
* |
| 72 |
* @var Request |
| 73 |
*/ |
| 74 |
private $request; |
| 75 |
|
| 76 |
/** |
| 77 |
* Order form. |
| 78 |
* |
| 79 |
* @var Form |
| 80 |
*/ |
| 81 |
private $order_form; |
| 82 |
|
| 83 |
/** |
| 84 |
* Options provider. |
| 85 |
* |
| 86 |
* @var Options\Provider |
| 87 |
*/ |
| 88 |
private $optionsProvider; |
| 89 |
|
| 90 |
/** |
| 91 |
* Form factory. |
| 92 |
* |
| 93 |
* @var FormFactory |
| 94 |
*/ |
| 95 |
private $formFactory; |
| 96 |
|
| 97 |
/** |
| 98 |
* Repository. |
| 99 |
* |
| 100 |
* @var Repository |
| 101 |
*/ |
| 102 |
private $orderRepository; |
| 103 |
|
| 104 |
/** |
| 105 |
* Log page. |
| 106 |
* |
| 107 |
* @var Log\Page |
| 108 |
*/ |
| 109 |
private $logPage; |
| 110 |
|
| 111 |
/** |
| 112 |
* OrderFacade. |
| 113 |
* |
| 114 |
* @var AttributeMapper |
| 115 |
*/ |
| 116 |
private $mapper; |
| 117 |
|
| 118 |
/** |
| 119 |
* Widget options builder. |
| 120 |
* |
| 121 |
* @var WidgetOptionsBuilder |
| 122 |
*/ |
| 123 |
private $widgetOptionsBuilder; |
| 124 |
|
| 125 |
/** |
| 126 |
* Carrier repository. |
| 127 |
* |
| 128 |
* @var EntityRepository |
| 129 |
*/ |
| 130 |
private $carrierRepository; |
| 131 |
|
| 132 |
/** |
| 133 |
* Order validator. |
| 134 |
* |
| 135 |
* @var Validator\Order |
| 136 |
*/ |
| 137 |
private $orderValidator; |
| 138 |
|
| 139 |
/** |
| 140 |
* Order detail common logic. |
| 141 |
* |
| 142 |
* @var DetailCommonLogic |
| 143 |
*/ |
| 144 |
private $detailCommonLogic; |
| 145 |
|
| 146 |
/** |
| 147 |
* Metabox constructor. |
| 148 |
* |
| 149 |
* @param Engine $latte_engine PacketeryLatte engine. |
| 150 |
* @param MessageManager $message_manager Message manager. |
| 151 |
* @param Helper $helper Helper. |
| 152 |
* @param Request $request Http request. |
| 153 |
* @param Options\Provider $optionsProvider Options provider. |
| 154 |
* @param FormFactory $formFactory Form factory. |
| 155 |
* @param Repository $orderRepository Order repository. |
| 156 |
* @param Log\Page $logPage Log page. |
| 157 |
* @param AttributeMapper $mapper AttributeMapper. |
| 158 |
* @param WidgetOptionsBuilder $widgetOptionsBuilder Widget options builder. |
| 159 |
* @param EntityRepository $carrierRepository Carrier repository. |
| 160 |
* @param Validator\Order $orderValidator Order validator. |
| 161 |
* @param DetailCommonLogic $detailCommonLogic Detail common logic. |
| 162 |
*/ |
| 163 |
public function __construct( |
| 164 |
Engine $latte_engine, |
| 165 |
MessageManager $message_manager, |
| 166 |
Helper $helper, |
| 167 |
Request $request, |
| 168 |
Options\Provider $optionsProvider, |
| 169 |
FormFactory $formFactory, |
| 170 |
Repository $orderRepository, |
| 171 |
Log\Page $logPage, |
| 172 |
AttributeMapper $mapper, |
| 173 |
WidgetOptionsBuilder $widgetOptionsBuilder, |
| 174 |
EntityRepository $carrierRepository, |
| 175 |
Validator\Order $orderValidator, |
| 176 |
DetailCommonLogic $detailCommonLogic |
| 177 |
) { |
| 178 |
$this->latte_engine = $latte_engine; |
| 179 |
$this->message_manager = $message_manager; |
| 180 |
$this->helper = $helper; |
| 181 |
$this->request = $request; |
| 182 |
$this->optionsProvider = $optionsProvider; |
| 183 |
$this->formFactory = $formFactory; |
| 184 |
$this->orderRepository = $orderRepository; |
| 185 |
$this->logPage = $logPage; |
| 186 |
$this->mapper = $mapper; |
| 187 |
$this->widgetOptionsBuilder = $widgetOptionsBuilder; |
| 188 |
$this->carrierRepository = $carrierRepository; |
| 189 |
$this->orderValidator = $orderValidator; |
| 190 |
$this->detailCommonLogic = $detailCommonLogic; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Registers related hooks. |
| 195 |
*/ |
| 196 |
public function register(): void { |
| 197 |
add_action( |
| 198 |
'admin_init', |
| 199 |
function () { |
| 200 |
$this->order_form = $this->formFactory->create(); |
| 201 |
$this->add_fields(); |
| 202 |
} |
| 203 |
); |
| 204 |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Add metaboxes |
| 209 |
*/ |
| 210 |
public function add_meta_boxes(): void { |
| 211 |
$orderId = $this->detailCommonLogic->getOrderId(); |
| 212 |
if ( null === $orderId ) { |
| 213 |
return; |
| 214 |
} |
| 215 |
|
| 216 |
try { |
| 217 |
$order = $this->orderRepository->getById( $orderId ); |
| 218 |
if ( null === $order ) { |
| 219 |
return; |
| 220 |
} |
| 221 |
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch |
| 222 |
} catch ( InvalidCarrierException $exception ) { |
| 223 |
// Let's display message in the box. |
| 224 |
} |
| 225 |
|
| 226 |
add_meta_box( |
| 227 |
'packetery_metabox', |
| 228 |
__( 'Packeta', 'packeta' ), |
| 229 |
array( |
| 230 |
$this, |
| 231 |
'render_metabox', |
| 232 |
), |
| 233 |
Module\Helper::isHposEnabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order', |
| 234 |
'side', |
| 235 |
'high' |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Adds packetery fields to order form. |
| 241 |
*/ |
| 242 |
public function add_fields(): void { |
| 243 |
$this->order_form->addHidden( 'packetery_order_metabox_nonce' ); |
| 244 |
$this->order_form->addText( self::FIELD_WEIGHT, __( 'Weight (kg)', 'packeta' ) ) |
| 245 |
->setRequired( false ) |
| 246 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); |
| 247 |
$this->order_form->addHidden( self::FIELD_ORIGINAL_WEIGHT ); |
| 248 |
$this->order_form->addText( self::FIELD_WIDTH, __( 'Width (mm)', 'packeta' ) ) |
| 249 |
->setRequired( false ) |
| 250 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); |
| 251 |
$this->order_form->addText( self::FIELD_LENGTH, __( 'Length (mm)', 'packeta' ) ) |
| 252 |
->setRequired( false ) |
| 253 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); |
| 254 |
$this->order_form->addText( self::FIELD_HEIGHT, __( 'Height (mm)', 'packeta' ) ) |
| 255 |
->setRequired( false ) |
| 256 |
->addRule( $this->order_form::FLOAT, __( 'Provide numeric value!', 'packeta' ) ); |
| 257 |
$this->order_form->addCheckbox( self::FIELD_ADULT_CONTENT, __( 'Adult content', 'packeta' ) ) |
| 258 |
->setRequired( false ); |
| 259 |
$this->order_form->addText( self::FIELD_COD, __( 'Cash on delivery', 'packeta' ) ) |
| 260 |
->setRequired( false ) |
| 261 |
->addRule( $this->order_form::FLOAT ); |
| 262 |
$this->order_form->addText( self::FIELD_VALUE, __( 'Order value', 'packeta' ) ) |
| 263 |
->setRequired( false ) |
| 264 |
->addRule( $this->order_form::FLOAT ); |
| 265 |
$this->order_form->addText( self::FIELD_DELIVER_ON, __( 'Planned dispatch', 'packeta' ) ) |
| 266 |
->setHtmlAttribute( 'autocomplete', 'off' ) |
| 267 |
->setRequired( false ) |
| 268 |
// translators: %s: Represents minimal date for delayed delivery. |
| 269 |
->addRule( [ FormValidators::class, 'dateIsLater' ], __( 'Date must be later than %s', 'packeta' ), wp_date( Helper::DATEPICKER_FORMAT ) ); |
| 270 |
|
| 271 |
foreach ( Attribute::$pickupPointAttrs as $pickupPointAttr ) { |
| 272 |
$this->order_form->addHidden( $pickupPointAttr['name'] ); |
| 273 |
} |
| 274 |
|
| 275 |
foreach ( Attribute::$homeDeliveryAttrs as $homeDeliveryAttr ) { |
| 276 |
$this->order_form->addHidden( $homeDeliveryAttr['name'] ); |
| 277 |
} |
| 278 |
|
| 279 |
$this->order_form->addButton( 'packetery_pick_pickup_point', __( 'Choose pickup point', 'packeta' ) ); |
| 280 |
$this->order_form->addButton( 'packetery_pick_address', __( 'Check shipping address', 'packeta' ) ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Renders metabox |
| 285 |
*/ |
| 286 |
public function render_metabox(): void { |
| 287 |
$orderId = $this->detailCommonLogic->getOrderId(); |
| 288 |
if ( null === $orderId ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
$wcOrder = $this->orderRepository->getWcOrderById( $orderId ); |
| 293 |
if ( null === $wcOrder ) { |
| 294 |
return; |
| 295 |
} |
| 296 |
|
| 297 |
try { |
| 298 |
$order = $this->orderRepository->getByWcOrder( $wcOrder ); |
| 299 |
} catch ( InvalidCarrierException $exception ) { |
| 300 |
$this->latte_engine->render( |
| 301 |
PACKETERY_PLUGIN_DIR . '/template/order/metabox-form-error.latte', |
| 302 |
[ |
| 303 |
'errorMessage' => $exception->getMessage(), |
| 304 |
] |
| 305 |
); |
| 306 |
|
| 307 |
return; |
| 308 |
} |
| 309 |
if ( null === $order ) { |
| 310 |
return; |
| 311 |
} |
| 312 |
$packetId = $order->getPacketId(); |
| 313 |
|
| 314 |
$showLogsLink = null; |
| 315 |
if ( $this->logPage->hasAnyRows( (int) $order->getNumber() ) ) { |
| 316 |
$showLogsLink = $this->logPage->createLogListUrl( (int) $order->getNumber() ); |
| 317 |
} |
| 318 |
|
| 319 |
$packetClaimUrl = null; |
| 320 |
if ( $order->isPacketClaimCreationPossible() ) { |
| 321 |
$packetClaimUrl = $this->getOrderActionLink( $order, PacketActionsCommonLogic::ACTION_SUBMIT_PACKET_CLAIM ); |
| 322 |
} |
| 323 |
|
| 324 |
$packetClaimCancelUrl = null; |
| 325 |
$packetClaimTrackingUrl = null; |
| 326 |
if ( $order->getPacketClaimId() ) { |
| 327 |
$packetClaimCancelUrl = $this->getOrderActionLink( |
| 328 |
$order, |
| 329 |
PacketActionsCommonLogic::ACTION_CANCEL_PACKET, |
| 330 |
[ |
| 331 |
PacketActionsCommonLogic::PARAM_PACKET_ID => $order->getPacketClaimId(), |
| 332 |
] |
| 333 |
); |
| 334 |
$packetClaimTrackingUrl = $this->helper->get_tracking_url( $order->getPacketClaimId() ); |
| 335 |
} |
| 336 |
|
| 337 |
if ( $packetId ) { |
| 338 |
$packetCancelLink = $this->getOrderActionLink( |
| 339 |
$order, |
| 340 |
PacketActionsCommonLogic::ACTION_CANCEL_PACKET, |
| 341 |
[ |
| 342 |
PacketActionsCommonLogic::PARAM_PACKET_ID => $packetId, |
| 343 |
] |
| 344 |
); |
| 345 |
$this->latte_engine->render( |
| 346 |
PACKETERY_PLUGIN_DIR . '/template/order/metabox-common.latte', |
| 347 |
[ |
| 348 |
'order' => $order, |
| 349 |
'showSubmitPacketButton' => false, |
| 350 |
'packetCancelLink' => $packetCancelLink, |
| 351 |
'packetTrackingUrl' => $this->helper->get_tracking_url( $packetId ), |
| 352 |
'packetClaimTrackingUrl' => $packetClaimTrackingUrl, |
| 353 |
'showLogsLink' => $showLogsLink, |
| 354 |
'packetClaimUrl' => $packetClaimUrl, |
| 355 |
'packetClaimCancelUrl' => $packetClaimCancelUrl, |
| 356 |
'translations' => [ |
| 357 |
'packetTrackingOnline' => __( 'Packet tracking online', 'packeta' ), |
| 358 |
'packetClaimTrackingOnline' => __( 'Packet claim tracking', 'packeta' ), |
| 359 |
'showLogs' => __( 'Show logs', 'packeta' ), |
| 360 |
// translators: %s: Order number. |
| 361 |
'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ), |
| 362 |
// translators: %s: Packet number. |
| 363 |
'reallyCancelPacket' => sprintf( __( 'Do you really wish to cancel parcel number %s?', 'packeta' ), $packetId ), |
| 364 |
// translators: %s: Packet claim number. |
| 365 |
'reallyCancelPacketClaim' => sprintf( __( 'Do you really wish to cancel packet claim number %s?', 'packeta' ), $order->getPacketClaimId() ), |
| 366 |
|
| 367 |
'cancelPacket' => __( 'Cancel packet', 'packeta' ), |
| 368 |
'createPacketClaim' => __( 'Create packet claim', 'packeta' ), |
| 369 |
'printPacketLabel' => __( 'Print packet label', 'packeta' ), |
| 370 |
'printPacketClaimLabel' => __( 'Print packet claim label', 'packeta' ), |
| 371 |
'cancelPacketClaim' => __( 'Cancel packet claim', 'packeta' ), |
| 372 |
'packetClaimPassword' => __( 'Packet claim password', 'packeta' ), |
| 373 |
'submissionPassword' => __( 'submission password', 'packeta' ), |
| 374 |
], |
| 375 |
] |
| 376 |
); |
| 377 |
|
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
$this->order_form->setDefaults( |
| 382 |
[ |
| 383 |
'packetery_order_metabox_nonce' => wp_create_nonce(), |
| 384 |
self::FIELD_WEIGHT => $order->getFinalWeight(), |
| 385 |
self::FIELD_ORIGINAL_WEIGHT => $order->getFinalWeight(), |
| 386 |
self::FIELD_WIDTH => $order->getWidth(), |
| 387 |
self::FIELD_LENGTH => $order->getLength(), |
| 388 |
self::FIELD_HEIGHT => $order->getHeight(), |
| 389 |
self::FIELD_ADULT_CONTENT => $order->containsAdultContent(), |
| 390 |
self::FIELD_COD => $order->getCod(), |
| 391 |
self::FIELD_VALUE => $order->getValue(), |
| 392 |
self::FIELD_DELIVER_ON => $this->helper->getStringFromDateTime( $order->getDeliverOn(), Helper::DATEPICKER_FORMAT ), |
| 393 |
] |
| 394 |
); |
| 395 |
|
| 396 |
$prev_invalid_values = get_transient( 'packetery_metabox_nette_form_prev_invalid_values' ); |
| 397 |
if ( $prev_invalid_values ) { |
| 398 |
$this->order_form->setValues( $prev_invalid_values ); |
| 399 |
$this->order_form->validate(); |
| 400 |
} |
| 401 |
delete_transient( 'packetery_metabox_nette_form_prev_invalid_values' ); |
| 402 |
|
| 403 |
$showSubmitPacketButton = $this->orderValidator->isValid( $order ); |
| 404 |
$packetSubmitUrl = $this->getOrderActionLink( $order, PacketActionsCommonLogic::ACTION_SUBMIT_PACKET ); |
| 405 |
|
| 406 |
$showWidgetButton = $order->isPickupPointDelivery(); |
| 407 |
$widgetButtonError = null; |
| 408 |
$shippingCountry = $order->getShippingCountry(); |
| 409 |
$showHdWidget = $order->isHomeDelivery() && in_array( $shippingCountry, Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ); |
| 410 |
if ( |
| 411 |
null === $shippingCountry || |
| 412 |
! $this->carrierRepository->isValidForCountry( $order->getCarrier()->getId(), $shippingCountry ) |
| 413 |
) { |
| 414 |
if ( $order->isPickupPointDelivery() ) { |
| 415 |
$showWidgetButton = false; |
| 416 |
if ( empty( $shippingCountry ) ) { |
| 417 |
$widgetButtonError = __( |
| 418 |
'The pickup point cannot be changed because the shipping address has no country set. First, change the country of delivery in the shipping address.', |
| 419 |
'packeta' |
| 420 |
); |
| 421 |
} else { |
| 422 |
$widgetButtonError = sprintf( |
| 423 |
// translators: %s is country code. |
| 424 |
__( |
| 425 |
'The pickup point cannot be changed because the selected carrier does not deliver to country "%s". First, change the country of delivery in the shipping address.', |
| 426 |
'packeta' |
| 427 |
), |
| 428 |
$shippingCountry |
| 429 |
); |
| 430 |
} |
| 431 |
} elseif ( in_array( $order->getCarrier()->getCountry(), Entity\Carrier::ADDRESS_VALIDATION_COUNTRIES, true ) ) { |
| 432 |
$showHdWidget = false; |
| 433 |
if ( empty( $shippingCountry ) ) { |
| 434 |
$widgetButtonError = __( |
| 435 |
'The address cannot be validated because the shipping address has no country set. First, change the country of delivery in the shipping address.', |
| 436 |
'packeta' |
| 437 |
); |
| 438 |
} else { |
| 439 |
$widgetButtonError = sprintf( |
| 440 |
// translators: %s is country code. |
| 441 |
__( |
| 442 |
'The address cannot be validated because the selected carrier does not deliver to country "%s". First, change the country of delivery in the shipping address.', |
| 443 |
'packeta' |
| 444 |
), |
| 445 |
$shippingCountry |
| 446 |
); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
$this->latte_engine->render( |
| 452 |
PACKETERY_PLUGIN_DIR . '/template/order/metabox-form.latte', |
| 453 |
[ |
| 454 |
'form' => $this->order_form, |
| 455 |
'order' => $order, |
| 456 |
'showWidgetButton' => $showWidgetButton, |
| 457 |
'widgetButtonError' => $widgetButtonError, |
| 458 |
'showHdWidget' => $showHdWidget, |
| 459 |
'showSubmitPacketButton' => $showSubmitPacketButton, |
| 460 |
'packetCancelLink' => null, |
| 461 |
'packetTrackingUrl' => null, |
| 462 |
'packetSubmitUrl' => $packetSubmitUrl, |
| 463 |
'packetClaimTrackingUrl' => $packetClaimTrackingUrl, |
| 464 |
'packetClaimUrl' => $packetClaimUrl, |
| 465 |
'packetClaimCancelUrl' => $packetClaimCancelUrl, |
| 466 |
'orderCurrency' => get_woocommerce_currency_symbol( $order->getCurrency() ), |
| 467 |
'isCodPayment' => $wcOrder->get_payment_method() === $this->optionsProvider->getCodPaymentMethod(), |
| 468 |
'logo' => plugin_dir_url( PACKETERY_PLUGIN_DIR . '/packeta.php' ) . 'public/packeta-symbol.png', |
| 469 |
'showLogsLink' => $showLogsLink, |
| 470 |
'hasOrderManualWeight' => $order->hasManualWeight(), |
| 471 |
'isPacketaPickupPoint' => $order->isPacketaInternalPickupPoint(), |
| 472 |
'translations' => [ |
| 473 |
'showLogs' => __( 'Show logs', 'packeta' ), |
| 474 |
'weightIsManual' => __( 'Weight is manually set. To calculate weight remove field content and save.', 'packeta' ), |
| 475 |
'submitPacket' => __( 'Submit to packeta', 'packeta' ), |
| 476 |
'packetClaimTrackingOnline' => __( 'Packet claim tracking', 'packeta' ), |
| 477 |
'printPacketClaimLabel' => __( 'Print packet claim label', 'packeta' ), |
| 478 |
'cancelPacketClaim' => __( 'Cancel packet claim', 'packeta' ), |
| 479 |
'packetClaimPassword' => __( 'Packet claim password', 'packeta' ), |
| 480 |
'submissionPassword' => __( 'submission password', 'packeta' ), |
| 481 |
// translators: %s: Order number. |
| 482 |
'reallyCancelPacketHeading' => sprintf( __( 'Order #%s', 'packeta' ), $order->getCustomNumber() ), |
| 483 |
// translators: %s: Packet claim number. |
| 484 |
'reallyCancelPacketClaim' => sprintf( __( 'Do you really wish to cancel packet claim number %s?', 'packeta' ), $order->getPacketClaimId() ), |
| 485 |
], |
| 486 |
] |
| 487 |
); |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Saves added packetery form fields to order metas. |
| 492 |
* |
| 493 |
* @param Entity\Order $order Order. |
| 494 |
* |
| 495 |
* @return void |
| 496 |
* @throws WC_Data_Exception When invalid data are passed during shipping address update. |
| 497 |
*/ |
| 498 |
public function saveFields( Entity\Order $order ): void { |
| 499 |
$orderId = (int) $order->getNumber(); |
| 500 |
if ( |
| 501 |
( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || |
| 502 |
null === $this->request->getPost( 'packetery_order_metabox_nonce' ) |
| 503 |
) { |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
if ( false === $this->order_form->isValid() ) { |
| 508 |
set_transient( 'packetery_metabox_nette_form_prev_invalid_values', $this->order_form->getValues( true ) ); |
| 509 |
$this->message_manager->flash_message( __( 'Packeta: entered data is not valid!', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 510 |
|
| 511 |
return; |
| 512 |
} |
| 513 |
|
| 514 |
$orderFormValues = $this->order_form->getValues( 'array' ); |
| 515 |
|
| 516 |
if ( ! wp_verify_nonce( $orderFormValues['packetery_order_metabox_nonce'] ) ) { |
| 517 |
$this->message_manager->flash_message( __( 'Session has expired! Please try again.', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 518 |
|
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
if ( ! current_user_can( 'edit_post', $orderId ) ) { |
| 523 |
$this->message_manager->flash_message( __( 'You do not have sufficient rights to make changes!', 'packeta' ), MessageManager::TYPE_ERROR ); |
| 524 |
|
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
$propsToSave = [ |
| 529 |
self::FIELD_WIDTH => ( is_numeric( $orderFormValues[ self::FIELD_WIDTH ] ) ? (float) number_format( $orderFormValues[ self::FIELD_WIDTH ], 0, '.', '' ) : null ), |
| 530 |
self::FIELD_LENGTH => ( is_numeric( $orderFormValues[ self::FIELD_LENGTH ] ) ? (float) number_format( $orderFormValues[ self::FIELD_LENGTH ], 0, '.', '' ) : null ), |
| 531 |
self::FIELD_HEIGHT => ( is_numeric( $orderFormValues[ self::FIELD_HEIGHT ] ) ? (float) number_format( $orderFormValues[ self::FIELD_HEIGHT ], 0, '.', '' ) : null ), |
| 532 |
]; |
| 533 |
|
| 534 |
if ( ! is_numeric( $orderFormValues[ self::FIELD_WEIGHT ] ) ) { |
| 535 |
$propsToSave[ self::FIELD_WEIGHT ] = null; |
| 536 |
} elseif ( (float) $orderFormValues[ self::FIELD_WEIGHT ] !== (float) $orderFormValues[ self::FIELD_ORIGINAL_WEIGHT ] ) { |
| 537 |
$propsToSave[ self::FIELD_WEIGHT ] = (float) $orderFormValues[ self::FIELD_WEIGHT ]; |
| 538 |
} |
| 539 |
|
| 540 |
if ( $orderFormValues[ Attribute::POINT_ID ] && $order->isPickupPointDelivery() ) { |
| 541 |
/** |
| 542 |
* Cannot be null due to the condition at the beginning of the method. |
| 543 |
* |
| 544 |
* @var WC_Order $wcOrder |
| 545 |
*/ |
| 546 |
$wcOrder = $this->orderRepository->getWcOrderById( (int) $orderId ); |
| 547 |
foreach ( Attribute::$pickupPointAttrs as $pickupPointAttr ) { |
| 548 |
$pickupPointValue = $orderFormValues[ $pickupPointAttr['name'] ]; |
| 549 |
|
| 550 |
if ( Attribute::CARRIER_ID === $pickupPointAttr['name'] ) { |
| 551 |
if ( ! empty( $orderFormValues[ Attribute::CARRIER_ID ] ) ) { |
| 552 |
$pickupPointValue = $orderFormValues[ Attribute::CARRIER_ID ]; |
| 553 |
} else { |
| 554 |
$pickupPointValue = $order->getCarrier()->getId(); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
$propsToSave[ $pickupPointAttr['name'] ] = $pickupPointValue; |
| 559 |
|
| 560 |
if ( $this->optionsProvider->replaceShippingAddressWithPickupPointAddress() ) { |
| 561 |
$this->mapper->toWcOrderShippingAddress( $wcOrder, $pickupPointAttr['name'], (string) $pickupPointValue ); |
| 562 |
} |
| 563 |
} |
| 564 |
$wcOrder->save(); |
| 565 |
} |
| 566 |
|
| 567 |
if ( '1' === $orderFormValues[ Attribute::ADDRESS_IS_VALIDATED ] && $order->isHomeDelivery() ) { |
| 568 |
$address = $this->mapper->toValidatedAddress( $orderFormValues ); |
| 569 |
$order->setDeliveryAddress( $address ); |
| 570 |
$order->setAddressValidated( true ); |
| 571 |
} |
| 572 |
|
| 573 |
$order->setAdultContent( $orderFormValues[ self::FIELD_ADULT_CONTENT ] ); |
| 574 |
$order->setCod( is_numeric( $orderFormValues[ self::FIELD_COD ] ) ? Helper::simplifyFloat( $orderFormValues[ self::FIELD_COD ], 10 ) : null ); |
| 575 |
$order->setValue( is_numeric( $orderFormValues[ self::FIELD_VALUE ] ) ? Helper::simplifyFloat( $orderFormValues[ self::FIELD_VALUE ], 10 ) : null ); |
| 576 |
$order->setDeliverOn( $this->helper->getDateTimeFromString( $orderFormValues[ self::FIELD_DELIVER_ON ] ) ); |
| 577 |
|
| 578 |
$orderSize = $this->mapper->toOrderSize( $order, $propsToSave ); |
| 579 |
$order->setSize( $orderSize ); |
| 580 |
|
| 581 |
$pickupPoint = $this->mapper->toOrderEntityPickupPoint( $order, $propsToSave ); |
| 582 |
$order->setPickupPoint( $pickupPoint ); |
| 583 |
|
| 584 |
$this->orderRepository->save( $order ); |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* Creates pickup point picker settings. |
| 589 |
* |
| 590 |
* @return array|null |
| 591 |
*/ |
| 592 |
public function getPickupPointWidgetSettings(): ?array { |
| 593 |
$order = $this->detailCommonLogic->getOrder(); |
| 594 |
if ( null === $order || false === $order->isPickupPointDelivery() || null === $order->getShippingCountry() ) { |
| 595 |
return null; |
| 596 |
} |
| 597 |
|
| 598 |
$widgetOptions = $this->widgetOptionsBuilder->createPickupPointForAdmin( $order ); |
| 599 |
|
| 600 |
return [ |
| 601 |
'packeteryApiKey' => $this->optionsProvider->get_api_key(), |
| 602 |
'pickupPointAttrs' => Attribute::$pickupPointAttrs, |
| 603 |
'widgetOptions' => $widgetOptions, |
| 604 |
]; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Creates address picker settings. |
| 609 |
* |
| 610 |
* @return array|null |
| 611 |
*/ |
| 612 |
public function getAddressWidgetSettings(): ?array { |
| 613 |
$order = $this->detailCommonLogic->getOrder(); |
| 614 |
if ( null === $order || false === $order->isHomeDelivery() ) { |
| 615 |
return null; |
| 616 |
} |
| 617 |
|
| 618 |
$widgetOptions = $this->widgetOptionsBuilder->createAddressForAdmin( $order ); |
| 619 |
|
| 620 |
return [ |
| 621 |
'packeteryApiKey' => $this->optionsProvider->get_api_key(), |
| 622 |
'homeDeliveryAttrs' => Attribute::$homeDeliveryAttrs, |
| 623 |
'widgetOptions' => $widgetOptions, |
| 624 |
'translations' => [ |
| 625 |
'addressValidationIsOutOfOrder' => __( 'Address validation is out of order.', 'packeta' ), |
| 626 |
'invalidAddressCountrySelected' => __( 'The selected country does not correspond to the destination country.', 'packeta' ), |
| 627 |
], |
| 628 |
]; |
| 629 |
} |
| 630 |
|
| 631 |
/** |
| 632 |
* Gets order action link. |
| 633 |
* |
| 634 |
* @param Entity\Order $order Order. |
| 635 |
* @param string $action Action. |
| 636 |
* @param array $extraParams Extra params. |
| 637 |
* |
| 638 |
* @return string |
| 639 |
*/ |
| 640 |
private function getOrderActionLink( Entity\Order $order, string $action, array $extraParams = [] ): string { |
| 641 |
$baseParams = [ |
| 642 |
PacketActionsCommonLogic::PARAM_ORDER_ID => $order->getNumber(), |
| 643 |
PacketActionsCommonLogic::PARAM_REDIRECT_TO => PacketActionsCommonLogic::REDIRECT_TO_ORDER_DETAIL, |
| 644 |
Plugin::PARAM_PACKETERY_ACTION => $action, |
| 645 |
Plugin::PARAM_NONCE => wp_create_nonce( PacketActionsCommonLogic::createNonceAction( $action, $order->getNumber() ) ), |
| 646 |
]; |
| 647 |
|
| 648 |
return add_query_arg( |
| 649 |
array_merge( $baseParams, $extraParams ), |
| 650 |
admin_url( 'admin.php' ) |
| 651 |
); |
| 652 |
} |
| 653 |
|
| 654 |
} |
| 655 |
|