Make plugin work
This commit is contained in:
@@ -28,34 +28,175 @@ class PrijsPerPostcode
|
||||
$uri = $_SERVER["REQUEST_URI"];
|
||||
if_needed_place_postcode_form($uri);
|
||||
|
||||
add_action("wp_footer", "set_checkout_fields_with_javascript");
|
||||
function set_checkout_fields_with_javascript()
|
||||
add_action("woocommerce_product_query", [
|
||||
$this,
|
||||
"custom_show_products_by_tag",
|
||||
]);
|
||||
add_action("template_redirect", [$this, "redirect_if_missing_tag"]);
|
||||
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,
|
||||
3,
|
||||
);
|
||||
}
|
||||
|
||||
public function set_checkout_fields_with_javascript()
|
||||
{
|
||||
// Only run on the checkout page
|
||||
// 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 = $_SESSION["postcode"];
|
||||
$address_1 =
|
||||
$_SESSION["straatnaam"] . " " . $_SESSION["huisnummer"];
|
||||
$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">
|
||||
jQuery(document).ready(function($){
|
||||
// Set the values for the checkout fields
|
||||
$('#billing_city').val('<?php echo esc_js($woonplaats); ?>');
|
||||
$('#billing_postcode').val('<?php echo esc_js($postcode); ?>');
|
||||
$('#billing_city').val('<?php echo esc_js(
|
||||
$woonplaats,
|
||||
); ?>');
|
||||
$('#billing_postcode').val('<?php echo esc_js(
|
||||
$postcode,
|
||||
); ?>');
|
||||
$('#billing_address_1').val('<?php echo esc_js(
|
||||
$address_1,
|
||||
$address,
|
||||
); ?>');
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
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",
|
||||
"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 ($excluded_term) {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
new PrijsPerPostcode();
|
||||
|
||||
@@ -69,7 +69,8 @@ function send_postcode_data()
|
||||
}
|
||||
|
||||
if (data.status === "success"){
|
||||
postcodeModal.close();
|
||||
//postcodeModal.close();
|
||||
location.reload();
|
||||
}
|
||||
}catch(err){
|
||||
console.error("Fetch Failed:", err);
|
||||
@@ -159,7 +160,8 @@ function handle_postcode_modal($data)
|
||||
$_SESSION["huisnummer"] = $params["huisnummer"];
|
||||
$_SESSION["straatnaam"] = $result["straatnaam"];
|
||||
$_SESSION["woonplaats"] = $result["woonplaats"];
|
||||
$_SESSION["lokaak_tarief"] = postcode_in_range(
|
||||
|
||||
$_SESSION["lokaal_tarief"] = postcode_in_range(
|
||||
$params["postcode"],
|
||||
5000,
|
||||
5800,
|
||||
|
||||
Reference in New Issue
Block a user