Compare commits
2 Commits
ce165ebfc6
...
30b43a9396
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
30b43a9396 | ||
|
|
782fee8ac4 |
@@ -45,6 +45,11 @@
|
||||
}
|
||||
|
||||
&::backdrop {
|
||||
backdrop-filter: blur(2px);
|
||||
background-color: hsl(40deg 100 30 /0.5);
|
||||
}
|
||||
|
||||
.error_message_modal_postcode {
|
||||
min-height: 1lh;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
session_start();
|
||||
require_once "session_dialog.php";
|
||||
/*
|
||||
* Plugin Name: Prijs per Postcode
|
||||
* Plugin Name: Goedkoop Tilburg
|
||||
* Description: veschillende prijzen per postdoce. een range posctcodes zijn "lokaal" andere zijn overig
|
||||
* Author: Remo Zaros
|
||||
* Version: 0.0.1
|
||||
@@ -33,6 +33,14 @@ class PrijsPerPostcode
|
||||
"custom_show_products_by_tag",
|
||||
]);
|
||||
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,
|
||||
@@ -52,12 +60,10 @@ class PrijsPerPostcode
|
||||
|
||||
public function set_checkout_fields_with_javascript()
|
||||
{
|
||||
// Only run off the checkout page
|
||||
if (!is_checkout() || is_wc_endpoint_url()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Define the values you want to set
|
||||
$woonplaats = $_SESSION["woonplaats"];
|
||||
$postcode = $formatted_postcode = preg_replace(
|
||||
"/(\d+)([A-Z]+)/",
|
||||
@@ -70,7 +76,6 @@ class PrijsPerPostcode
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
jQuery(document).ready(function($){
|
||||
// Set the values for the checkout fields
|
||||
$('#billing_city').val('<?php echo esc_js(
|
||||
$woonplaats,
|
||||
); ?>');
|
||||
@@ -87,19 +92,15 @@ class PrijsPerPostcode
|
||||
|
||||
public function custom_show_products_by_tag($q)
|
||||
{
|
||||
// Only target main product queries on the frontend
|
||||
if (
|
||||
!is_admin() &&
|
||||
$q->is_main_query() &&
|
||||
(is_shop() || is_product_category() || is_home())
|
||||
) {
|
||||
// Replace this with your actual boolean logic
|
||||
$show_lokaal = $_SESSION["lokaal_tarief"] ?? false;
|
||||
|
||||
// Determine which tag to show
|
||||
$tag_to_show = $show_lokaal ? "lokaal" : "algemeen";
|
||||
|
||||
// Set the tax query to only include the chosen tag
|
||||
$tax_query = [
|
||||
[
|
||||
"taxonomy" => "product_tag",
|
||||
@@ -198,5 +199,49 @@ class PrijsPerPostcode
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
new PrijsPerPostcode();
|
||||
|
||||
@@ -12,6 +12,9 @@ function if_needed_place_postcode_form($uri)
|
||||
add_action("wp_enqueue_scripts", "modal_styles");
|
||||
add_action("wp_footer", "send_postcode_data");
|
||||
if (!has_postcode()) {
|
||||
if (!is_admin()) {
|
||||
WC()->cart->empty_cart();
|
||||
}
|
||||
add_action("wp_footer", "show_modal");
|
||||
}
|
||||
}
|
||||
@@ -65,7 +68,20 @@ function send_postcode_data()
|
||||
console.log("Data returnd", data);
|
||||
|
||||
if (data.status === "error"){
|
||||
const err = data.message;
|
||||
let errmsg
|
||||
|
||||
switch (err) {
|
||||
case "Huisnummer not found":
|
||||
errmsg = "Adres niet gevonden.";
|
||||
break;
|
||||
case "Multiple addresses match this huisnummer; add huisletter and/or huisnummertoevoeging":
|
||||
errmsg = "Huisnummertovoeging mist.";
|
||||
break;
|
||||
default:
|
||||
errmsg = "Gegevens niet correct.";
|
||||
}
|
||||
document.querySelector("#error_message_modal_postcode").innerHTML = errmsg;
|
||||
}
|
||||
|
||||
if (data.status === "success"){
|
||||
@@ -108,7 +124,7 @@ function render_dialog_html()
|
||||
<button id="postcode_modal_submit" type="submit">verzend</button>
|
||||
</div>
|
||||
<form>
|
||||
<div class="error_message" id="error_message"></div>
|
||||
<div class="error_message_modal_postcode" id="error_message_modal_postcode"></div>
|
||||
</dialog>
|
||||
<?php
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user