| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sendy\WooCommerce\Modules; |
| 4 |
|
| 5 |
use Sendy\WooCommerce\Plugin; |
| 6 |
use Sendy\WooCommerce\ShippingMethods\PickupPointDelivery; |
| 7 |
use Sendy\WooCommerce\Utils\View; |
| 8 |
use WP_Error; |
| 9 |
|
| 10 |
final class Checkout |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('wp_enqueue_scripts', [$this, 'enqueue_assets']); |
| 15 |
add_action('woocommerce_review_order_after_shipping', [$this, 'render_pickup_point_field'], 10); |
| 16 |
|
| 17 |
add_action('wp_ajax_sendy_set_pickup_point', [$this, 'store_selected_pickup_point_in_session']); |
| 18 |
add_action('wp_ajax_nopriv_sendy_set_pickup_point', [$this, 'store_selected_pickup_point_in_session']); |
| 19 |
|
| 20 |
add_action('woocommerce_checkout_create_order', [$this, 'store_selected_pickup_point_in_order'], 10, 3); |
| 21 |
|
| 22 |
add_action('woocommerce_order_details_after_customer_address', [$this, 'show_selected_pickup_point_on_confirmation'], 10, 2); |
| 23 |
|
| 24 |
add_action('woocommerce_after_checkout_validation', [$this, 'validate_pickup_point'], 10, 2); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Load the assets, but only on the checkout page |
| 29 |
*/ |
| 30 |
public function enqueue_assets(): void |
| 31 |
{ |
| 32 |
if (is_checkout()) { |
| 33 |
wp_enqueue_script('wp-util'); |
| 34 |
wp_enqueue_script('sendy-api', 'https://app.sendy.nl/embed/api.js', [], Plugin::VERSION, ['in_footer' => true]); |
| 35 |
wp_enqueue_script('sendy-checkout', SENDY_WC_PLUGIN_DIR_URL . '/resources/js/checkout.js', ['jquery', 'sendy-api'], Plugin::asset_version('/resources/js/checkout.js'), ['in_footer' => true]); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Render the button to select a pick-up point in the checkout |
| 41 |
*/ |
| 42 |
public function render_pickup_point_field(): void |
| 43 |
{ |
| 44 |
if (in_array('sendy_pickup_point', wc_get_chosen_shipping_method_ids())) { |
| 45 |
if (! is_null(sendy_shipping_method_instance_id())) { |
| 46 |
$carrier = $this->get_carrier_for_shipping_method(sendy_shipping_method_instance_id()); |
| 47 |
|
| 48 |
$selectedPickupPoint = WC()->session->get("sendy_selected_parcelshop_" . sendy_shipping_method_instance_id()); |
| 49 |
|
| 50 |
echo wp_kses( |
| 51 |
View::fromTemplate('checkout/pickup_point_selection.php')->render([ |
| 52 |
'carrier' => $carrier, |
| 53 |
'instance_id' => sendy_shipping_method_instance_id(), |
| 54 |
'selected_pickup_point' => $selectedPickupPoint, |
| 55 |
]), |
| 56 |
View::ALLOWED_TAGS, |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Store the selected pickup point in the session |
| 64 |
*/ |
| 65 |
public function store_selected_pickup_point_in_session(): void |
| 66 |
{ |
| 67 |
if (empty($_REQUEST['nonce']) || ! wp_verify_nonce(sanitize_key($_REQUEST['nonce']), 'sendy_store_pickup_point')) { |
| 68 |
wp_send_json_error('Nonce verification failed'); |
| 69 |
} |
| 70 |
|
| 71 |
$data = [ |
| 72 |
'id' => sanitize_text_field(wp_unslash($_REQUEST['id'] ?? '')), |
| 73 |
'name' => sanitize_text_field(wp_unslash($_REQUEST['name'] ?? '')), |
| 74 |
'street' => sanitize_text_field(wp_unslash($_REQUEST['street'] ?? '')), |
| 75 |
'number' => sanitize_text_field(wp_unslash($_REQUEST['number'] ?? '')), |
| 76 |
'postal_code' => sanitize_text_field(wp_unslash($_REQUEST['postal_code'] ?? '')), |
| 77 |
'city' => sanitize_text_field(wp_unslash($_REQUEST['city'] ?? '')), |
| 78 |
]; |
| 79 |
|
| 80 |
WC()->session->set('sendy_selected_parcelshop_' . sanitize_key($_REQUEST['instance_id'] ?? ''), $data); |
| 81 |
|
| 82 |
wp_send_json_success(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Add the selected pickup point to the order metadata |
| 87 |
*/ |
| 88 |
public function store_selected_pickup_point_in_order(\WC_Order $order): void |
| 89 |
{ |
| 90 |
$instanceId = sendy_shipping_method_instance_id(); |
| 91 |
|
| 92 |
if ($data = WC()->session->get("sendy_selected_parcelshop_{$instanceId}")) { |
| 93 |
|
| 94 |
$order->update_meta_data('_sendy_pickup_point_id', $data['id']); |
| 95 |
$order->update_meta_data('_sendy_pickup_point_data', $data); |
| 96 |
|
| 97 |
WC()->session->set("sendy_selected_parcelshop_{$instanceId}", null); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Display the address of the selected pickup point on the order confirmation page |
| 103 |
*/ |
| 104 |
public function show_selected_pickup_point_on_confirmation(string $addressType, \WC_Order $order): void |
| 105 |
{ |
| 106 |
if ($addressType === 'shipping' && $order->meta_exists('_sendy_pickup_point_id')) { |
| 107 |
echo wp_kses( |
| 108 |
View::fromTemplate('checkout/order_confirmation.php')->render([ |
| 109 |
'pickup_point' => $order->get_meta('_sendy_pickup_point_data'), |
| 110 |
]), |
| 111 |
View::ALLOWED_TAGS, |
| 112 |
); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Validate if the pickup point is selected |
| 118 |
*/ |
| 119 |
public function validate_pickup_point(array $fields, WP_Error $errors): void |
| 120 |
{ |
| 121 |
if (in_array('sendy_pickup_point', wc_get_chosen_shipping_method_ids())) { |
| 122 |
$instanceId = sendy_shipping_method_instance_id(); |
| 123 |
|
| 124 |
if (empty($instanceId)) { |
| 125 |
$errors->add('sendy_pickup_point_error', __('Please select a pick-up point.', 'sendy')); |
| 126 |
} else { |
| 127 |
$selectedPickupPoint = WC()->session->get("sendy_selected_parcelshop_{$instanceId}"); |
| 128 |
|
| 129 |
if (empty($selectedPickupPoint)) { |
| 130 |
$errors->add('sendy_pickup_point_error', __('Please select a pick-up point.', 'sendy')); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Get the carrier for the shipping method |
| 138 |
* |
| 139 |
* This method is used to load the correct pick-up points for a carrier in the checkout |
| 140 |
*/ |
| 141 |
private function get_carrier_for_shipping_method(int $instance_id): ?string |
| 142 |
{ |
| 143 |
return (new PickupPointDelivery($instance_id))->get_option('carrier'); |
| 144 |
} |
| 145 |
} |
| 146 |
|