161 lines
4.3 KiB
PHP
161 lines
4.3 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"];
|
|
add_filter("woocommerce_sale_flash", "__return_null");
|
|
init_postcode_handlers($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",
|
|
]);
|
|
}
|
|
|
|
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();
|