Files
wp-postcode-plugin/prijs-per-postcode.php
2026-05-12 16:24:50 +02:00

221 lines
6.1 KiB
PHP

<?php
session_start();
require_once "session_dialog.php";
/*
* Plugin Name: prijzen per poscode range
* Description: posctcodes in de 5000-5800 range krijgen een lokaal tarief aangeboden.
* Author: Remo Zaros
* Version: 0.9.1
* Text Domeain: prijs-per-postcode
*/
if (!defined("ABSPATH")) {
echo "big bag of potatoes";
exit();
}
class PrijsPerPostcode
{
public function __construct()
{
add_action("init", [$this, "init"]);
add_action("rest_api_init", "register_modal_api");
}
public function init()
{
$uri = $_SERVER["REQUEST_URI"];
if_needed_place_postcode_form($uri);
//add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
add_action(
"woocommerce_variation_options_pricing",
[$this, "add_local_price_field"],
10,
3,
);
add_action(
"woocommerce_save_product_variation",
[$this, "save_local_price_field"],
10,
2,
);
add_action("woocommerce_before_calculate_totals", [
$this,
"use_local_price_if_local_postcode",
]);
add_filter(
"woocommerce_get_price_html",
[$this, "display_local_price_on_product"],
10,
2,
);
add_action("template_redirect", [
$this,
"controleer_postcode_op_woocommerce_paginas",
]);
add_action("wp_footer", [$this, "set_checkout_fields_with_javascript"]);
}
public function set_checkout_fields_with_javascript()
{
if (
!is_checkout() ||
(is_wc_endpoint_url() && !is_wc_endpoint_url("order-received"))
) {
return;
}
$woonplaats = $_SESSION["woonplaats"];
$postcode = $formatted_postcode = preg_replace(
"/(\d+)([A-Z]+)/",
'$1 $2',
strtoupper($_SESSION["postcode"]),
);
$address =
$_SESSION["straatnaam"] . " " . strtoupper($_SESSION["huisnummer"]);
// Output the JavaScript
?>
<script type="text/javascript" id="fill_address_fields">
jQuery(document).ready(function($) {
fillCheckoutFields();
$(document.body).on('updated_checkout', fillCheckoutFields);
});
function fillCheckoutFields() {
if (typeof wp !== 'undefined' && wp.data && wp.data.dispatch) {
const store = 'wc/store/cart';
wp.data.dispatch(store).setShippingAddress({
first_name: '',
last_name: '',
address_1: '<?php echo esc_js($address); ?>',
address_2: '',
city: '<?php echo esc_js($woonplaats); ?>',
state: '',
postcode: '<?php echo esc_js($postcode); ?>',
country: 'NL',
phone: '',
email: ''
});
setTimeout(() => {
$('#shipping-postcode, #shipping-city, #shipping-address_1')
.prop('readonly', true)
.css('background', '#f9f9f9');
}, 500);
jQuery(document.body).trigger('update_checkout');
} else {
console.error('WooCommerce Blocks API is niet beschikbaar');
}
}
</script>
<?php
}
public function add_local_price_field($loop, $variation_data, $variation)
{
woocommerce_wp_text_input([
"id" => "_local_price[" . $loop . "]",
"label" =>
__("Lokale Prijs", "woocommerce") .
" (" .
get_woocommerce_currency_symbol() .
")",
"value" => get_post_meta($variation->ID, "_local_price", true),
"data_type" => "price",
"wrapper_class" => "form-row form-row-first", // Left half
]);
}
public function save_local_price_field($variation_id, $i)
{
$local_price = $_POST["_local_price"][$i];
if (isset($local_price)) {
update_post_meta(
$variation_id,
"_local_price",
wc_clean($local_price),
);
}
}
public function use_local_price_if_local_postcode($cart)
{
if (is_admin() && !defined("DOING_AJAX")) {
return;
}
foreach ($cart->get_cart() as $cart_item) {
$product = $cart_item["data"];
$variation_id = $product->is_type("variation")
? $product->get_id()
: 0;
if (
$variation_id &&
isset($_SESSION["postcode_is_local"]) &&
$_SESSION["postcode_is_local"]
) {
$local_price = get_post_meta(
$variation_id,
"_local_price",
true,
);
if ($local_price) {
$product->set_price($local_price);
}
}
}
}
public function controleer_postcode_op_woocommerce_paginas()
{
if (is_admin() || defined("DOING_AJAX")) {
return;
}
if (
(is_product() ||
is_product_category() ||
is_product_tag() ||
is_cart() ||
is_checkout() ||
is_account_page()) &&
!is_shop()
) {
if (!isset($_SESSION["postcode_is_local"])) {
wp_redirect(home_url("/winkel/"));
exit();
}
}
}
public function display_local_price_on_product($price_html, $product)
{
if (
$product->is_type("variation") &&
isset($_SESSION["postcode_is_local"]) &&
$_SESSION["postcode_is_local"] === true
) {
$local_price = get_post_meta(
$product->get_id(),
"_local_price",
true,
);
if ($local_price !== "") {
return wc_price($local_price);
}
}
return $price_html;
}
}
new PrijsPerPostcode();