work in live now.
This commit is contained in:
@@ -3,10 +3,10 @@
|
|||||||
session_start();
|
session_start();
|
||||||
require_once "session_dialog.php";
|
require_once "session_dialog.php";
|
||||||
/*
|
/*
|
||||||
* Plugin Name: Goedkoop Tilburg
|
* Plugin Name: prijzen per poscode range
|
||||||
* Description: veschillende prijzen per postdoce. een range posctcodes zijn "lokaal" andere zijn overig
|
* Description: posctcodes in de 5000-5800 range krijgen een lokaal tarief aangeboden.
|
||||||
* Author: Remo Zaros
|
* Author: Remo Zaros
|
||||||
* Version: 0.0.1
|
* Version: 0.9.1
|
||||||
* Text Domeain: prijs-per-postcode
|
* Text Domeain: prijs-per-postcode
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -28,39 +28,43 @@ class PrijsPerPostcode
|
|||||||
$uri = $_SERVER["REQUEST_URI"];
|
$uri = $_SERVER["REQUEST_URI"];
|
||||||
if_needed_place_postcode_form($uri);
|
if_needed_place_postcode_form($uri);
|
||||||
|
|
||||||
add_action("woocommerce_product_query", [
|
//add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
|
||||||
$this,
|
add_action(
|
||||||
"custom_show_products_by_tag",
|
"woocommerce_variation_options_pricing",
|
||||||
]);
|
[$this, "add_local_price_field"],
|
||||||
add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
|
|
||||||
add_action("template_redirect", [
|
|
||||||
$this,
|
|
||||||
"force_404_for_specific_product_tags",
|
|
||||||
]);
|
|
||||||
add_action("template_redirect", [
|
|
||||||
$this,
|
|
||||||
"redirect_checkout_if_lokaaltarief_not_set",
|
|
||||||
]);
|
|
||||||
add_action("wp_footer", [$this, "set_checkout_fields_with_javascript"]);
|
|
||||||
add_filter("woocommerce_checkout_fields", [
|
|
||||||
$this,
|
|
||||||
"make_checkout_fields_readonly",
|
|
||||||
]);
|
|
||||||
add_filter("woocommerce_product_related_posts_query", [
|
|
||||||
$this,
|
|
||||||
"custom_filter_related_products_by_session_tag",
|
|
||||||
]);
|
|
||||||
add_filter(
|
|
||||||
"get_the_terms",
|
|
||||||
[$this, "hide_lokaal_and_overige_regios_tags"],
|
|
||||||
10,
|
10,
|
||||||
3,
|
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()
|
public function set_checkout_fields_with_javascript()
|
||||||
{
|
{
|
||||||
if (!is_checkout() || is_wc_endpoint_url()) {
|
if (
|
||||||
|
!is_checkout() ||
|
||||||
|
(is_wc_endpoint_url() && !is_wc_endpoint_url("order-received"))
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,173 +78,142 @@ class PrijsPerPostcode
|
|||||||
$_SESSION["straatnaam"] . " " . strtoupper($_SESSION["huisnummer"]);
|
$_SESSION["straatnaam"] . " " . strtoupper($_SESSION["huisnummer"]);
|
||||||
// Output the JavaScript
|
// Output the JavaScript
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript" id="fill_address_fields">
|
||||||
|
|
||||||
|
|
||||||
jQuery(document).ready(function($) {
|
jQuery(document).ready(function($) {
|
||||||
$('#billing_city').val('<?php echo esc_js(
|
fillCheckoutFields();
|
||||||
$woonplaats,
|
$(document.body).on('updated_checkout', fillCheckoutFields);
|
||||||
); ?>');
|
|
||||||
$('#billing_postcode').val('<?php echo esc_js(
|
|
||||||
$postcode,
|
|
||||||
); ?>');
|
|
||||||
$('#billing_address_1').val('<?php echo esc_js(
|
|
||||||
$address,
|
|
||||||
); ?>');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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>
|
</script>
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
|
|
||||||
public function custom_show_products_by_tag($q)
|
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 (
|
if (
|
||||||
!is_admin() &&
|
$product->is_type("variation") &&
|
||||||
$q->is_main_query() &&
|
isset($_SESSION["postcode_is_local"]) &&
|
||||||
(is_shop() || is_product_category() || is_home())
|
$_SESSION["postcode_is_local"] === true
|
||||||
) {
|
) {
|
||||||
$show_lokaal = $_SESSION["lokaal_tarief"] ?? false;
|
$local_price = get_post_meta(
|
||||||
|
$product->get_id(),
|
||||||
$tag_to_show = $show_lokaal ? "lokaal" : "algemeen";
|
"_local_price",
|
||||||
|
true,
|
||||||
$tax_query = [
|
|
||||||
[
|
|
||||||
"taxonomy" => "product_tag",
|
|
||||||
"field" => "slug",
|
|
||||||
"terms" => [$tag_to_show],
|
|
||||||
"operator" => "IN",
|
|
||||||
],
|
|
||||||
];
|
|
||||||
$q->set("tax_query", $tax_query);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function redirect_if_missing_tag()
|
|
||||||
{
|
|
||||||
// Only run on frontend single product pages
|
|
||||||
if (is_admin() || !is_singular("product")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$product_id = get_the_ID();
|
|
||||||
$product = wc_get_product($product_id);
|
|
||||||
|
|
||||||
$show_lokaal = $_SESSION["lokaal_tarief"] ?? false;
|
|
||||||
$tag_to_show = $show_lokaal ? "lokaal" : "algemeen";
|
|
||||||
|
|
||||||
if (!$product || !has_term($tag_to_show, "product_tag", $product_id)) {
|
|
||||||
wp_redirect(home_url("/shop")); // Works with Dutch permalink
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hide_lokaal_and_overige_regios_tags(
|
|
||||||
$terms,
|
|
||||||
$post_id,
|
|
||||||
$taxonomy,
|
|
||||||
) {
|
|
||||||
if ($taxonomy === "product_tag" && !is_admin()) {
|
|
||||||
$tags_to_hide = ["lokaal", "algemeen"];
|
|
||||||
return array_filter($terms, function ($term) use ($tags_to_hide) {
|
|
||||||
return !in_array($term->slug, $tags_to_hide);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return $terms;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function make_checkout_fields_readonly($fields)
|
|
||||||
{
|
|
||||||
$readonly_fields = [
|
|
||||||
"billing_address_1",
|
|
||||||
"billing_city",
|
|
||||||
"billing_postcode",
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($readonly_fields as $field) {
|
|
||||||
if (isset($fields["billing"][$field])) {
|
|
||||||
$fields["billing"][$field]["custom_attributes"][
|
|
||||||
"readonly"
|
|
||||||
] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function custom_filter_related_products_by_session_tag($query)
|
|
||||||
{
|
|
||||||
// Start session if not already started
|
|
||||||
if (session_status() === PHP_SESSION_NONE) {
|
|
||||||
session_start();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine which tag to exclude based on session
|
|
||||||
$show_lokaal = $_SESSION["lokaal_tarief"] ?? false;
|
|
||||||
$exclude_tag = $show_lokaal ? "algemeen" : "lokaal";
|
|
||||||
|
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
// Get term_taxonomy_id for the tag to exclude
|
|
||||||
$excluded_term = $wpdb->get_var(
|
|
||||||
$wpdb->prepare(
|
|
||||||
"SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} tt
|
|
||||||
JOIN {$wpdb->terms} t ON tt.term_id = t.term_id
|
|
||||||
WHERE tt.taxonomy = 'product_tag' AND t.slug = %s",
|
|
||||||
$exclude_tag,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
if ($local_price !== "") {
|
||||||
if ($excluded_term) {
|
return wc_price($local_price);
|
||||||
$query[
|
|
||||||
"join"
|
|
||||||
] .= " LEFT JOIN {$wpdb->term_relationships} exclude_tr ON exclude_tr.object_id = p.ID ";
|
|
||||||
$query["where"] .= $wpdb->prepare(
|
|
||||||
" AND ( exclude_tr.term_taxonomy_id != %d OR exclude_tr.term_taxonomy_id IS NULL )",
|
|
||||||
$excluded_term,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $query;
|
|
||||||
}
|
|
||||||
|
|
||||||
function redirect_checkout_if_lokaaltarief_not_set()
|
|
||||||
{
|
|
||||||
// Check if we are on the checkout, product, or cart page
|
|
||||||
if (is_checkout() || is_product() || is_cart()) {
|
|
||||||
// Prevent redirect on checkout endpoints (order-received, etc.)
|
|
||||||
if (is_wc_endpoint_url()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the session variable is NOT set
|
|
||||||
if (!isset($_SESSION["lokaal_tarief"])) {
|
|
||||||
wc_clear_notices();
|
|
||||||
wc_add_notice(
|
|
||||||
__(
|
|
||||||
"Local rate not available. Redirecting to shop.",
|
|
||||||
"woocommerce",
|
|
||||||
),
|
|
||||||
"notice",
|
|
||||||
);
|
|
||||||
wp_safe_redirect(wc_get_page_permalink("shop"));
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function force_404_for_specific_product_tags()
|
|
||||||
{
|
|
||||||
// Check if it's a product tag archive
|
|
||||||
if (is_tax("product_tag")) {
|
|
||||||
$tag = get_queried_object();
|
|
||||||
|
|
||||||
// List of tag slugs to block
|
|
||||||
$blocked_tags = ["lokaal", "algemeen"];
|
|
||||||
|
|
||||||
// If the current tag is in the blocked list, trigger 404
|
|
||||||
if (in_array($tag->slug, $blocked_tags)) {
|
|
||||||
global $wp_query;
|
|
||||||
$wp_query->set_404();
|
|
||||||
status_header(404);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return $price_html;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,12 +3,7 @@
|
|||||||
session_start();
|
session_start();
|
||||||
function if_needed_place_postcode_form($uri)
|
function if_needed_place_postcode_form($uri)
|
||||||
{
|
{
|
||||||
if (
|
if (strpos($uri, "/winkel/") !== false) {
|
||||||
strpos($uri, "/shop") !== false ||
|
|
||||||
strpos($uri, "/winkel") !== false ||
|
|
||||||
strpos($uri, "/product") !== false
|
|
||||||
) {
|
|
||||||
render_dialog_html();
|
|
||||||
add_action("wp_enqueue_scripts", "modal_styles");
|
add_action("wp_enqueue_scripts", "modal_styles");
|
||||||
add_action("wp_footer", "send_postcode_data");
|
add_action("wp_footer", "send_postcode_data");
|
||||||
if (!has_postcode()) {
|
if (!has_postcode()) {
|
||||||
@@ -16,6 +11,7 @@ function if_needed_place_postcode_form($uri)
|
|||||||
WC()->cart->empty_cart();
|
WC()->cart->empty_cart();
|
||||||
}
|
}
|
||||||
add_action("wp_footer", "show_modal");
|
add_action("wp_footer", "show_modal");
|
||||||
|
render_dialog_html();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -31,7 +27,7 @@ function modal_styles()
|
|||||||
function show_modal()
|
function show_modal()
|
||||||
{
|
{
|
||||||
?>
|
?>
|
||||||
<script>
|
<script id="postcode_modal">
|
||||||
const postcodeModal = document.querySelector("#postcode_modal");
|
const postcodeModal = document.querySelector("#postcode_modal");
|
||||||
postcodeModal.showModal();
|
postcodeModal.showModal();
|
||||||
</script>
|
</script>
|
||||||
@@ -43,20 +39,24 @@ function send_postcode_data()
|
|||||||
?>
|
?>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
const postcodeModal = document.querySelector("#postcode_modal");
|
const postcodeModal = document.querySelector("#postcode_modal");
|
||||||
const submitBtn = document.querySelector("#postcode_modal_form");
|
const modalForm = document.querySelector("#postcode_modal_form");
|
||||||
|
|
||||||
submitBtn.addEventListener('submit', async (e) => {
|
modalForm.addEventListener('submit', async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const formData = new FormData(e.target);
|
const formData = new FormData(e.target);
|
||||||
const data = Object.fromEntries(formData.entries());
|
const data = Object.fromEntries(formData.entries());
|
||||||
const json = JSON.stringify(data);
|
const json = JSON.stringify(data);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const resp = await fetch('<? echo get_rest_url(null, "postcode-modal/v1/submit"); ?>', {
|
const resp = await fetch('<?php echo get_rest_url(
|
||||||
|
null,
|
||||||
|
"postcode-modal/v1/submit",
|
||||||
|
); ?>', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
credentials: 'same-origin',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-WP-Nonce': '<? echo wp_create_nonce("wp_rest"); ?>'
|
'X-WP-Nonce': '<?php echo wp_create_nonce("wp_rest"); ?>'
|
||||||
},
|
},
|
||||||
body: json
|
body: json
|
||||||
});
|
});
|
||||||
@@ -85,8 +85,8 @@ function send_postcode_data()
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data.status === "success"){
|
if (data.status === "success"){
|
||||||
//postcodeModal.close();
|
postcodeModal.close();
|
||||||
location.reload();
|
//location.reload();
|
||||||
}
|
}
|
||||||
}catch(err){
|
}catch(err){
|
||||||
console.error("Fetch Failed:", err);
|
console.error("Fetch Failed:", err);
|
||||||
@@ -102,7 +102,7 @@ function render_dialog_html()
|
|||||||
?>
|
?>
|
||||||
<dialog id="postcode_modal" class="postcode_modal" closedby="none">
|
<dialog id="postcode_modal" class="postcode_modal" closedby="none">
|
||||||
<h2>Vul je postcode en huisnummer in.</h2>
|
<h2>Vul je postcode en huisnummer in.</h2>
|
||||||
<form id="postcode_modal_form" method="post" action="">
|
<form id="postcode_modal_form" method="post" action="" novalidation>
|
||||||
<div class="form_fields">
|
<div class="form_fields">
|
||||||
<div>
|
<div>
|
||||||
<input type="text" name="postcode"
|
<input type="text" name="postcode"
|
||||||
@@ -114,7 +114,7 @@ function render_dialog_html()
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<input type="text" name="huisnummer"
|
<input type="text" name="huisnummer"
|
||||||
pattern="\d+([-\s]?[a-zA-Z]+)?"
|
pattern="/\d+([-\\s]?[a-zA-Z]+)?/"
|
||||||
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
|
title="Voer een geldig huisnummer in (bijv. 1, 1A, 1-A, 1a)."
|
||||||
placeholder= "12a"
|
placeholder= "12a"
|
||||||
size="5"
|
size="5"
|
||||||
@@ -123,8 +123,9 @@ function render_dialog_html()
|
|||||||
</div>
|
</div>
|
||||||
<button id="postcode_modal_submit" type="submit">verzend</button>
|
<button id="postcode_modal_submit" type="submit">verzend</button>
|
||||||
</div>
|
</div>
|
||||||
<form>
|
</form>
|
||||||
<div class="error_message_modal_postcode" id="error_message_modal_postcode"></div>
|
<div class="error_message_modal_postcode" id="error_message_modal_postcode" aria-live="polite"></div>
|
||||||
|
<div class="loader_modal"></div>
|
||||||
</dialog>
|
</dialog>
|
||||||
<?php
|
<?php
|
||||||
}
|
}
|
||||||
@@ -177,7 +178,7 @@ function handle_postcode_modal($data)
|
|||||||
$_SESSION["straatnaam"] = $result["straatnaam"];
|
$_SESSION["straatnaam"] = $result["straatnaam"];
|
||||||
$_SESSION["woonplaats"] = $result["woonplaats"];
|
$_SESSION["woonplaats"] = $result["woonplaats"];
|
||||||
|
|
||||||
$_SESSION["lokaal_tarief"] = postcode_in_range(
|
$_SESSION["postcode_is_local"] = postcode_in_range(
|
||||||
$params["postcode"],
|
$params["postcode"],
|
||||||
5000,
|
5000,
|
||||||
5800,
|
5800,
|
||||||
|
|||||||
Reference in New Issue
Block a user