226 lines
6.0 KiB
PHP
226 lines
6.0 KiB
PHP
<?php
|
|
|
|
require_once plugin_dir_path(__FILE__) . "session_dialog.php";
|
|
require_once plugin_dir_path(__FILE__) . "admin.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.5
|
|
* Text Domeain: prijs-per-postcode
|
|
*/
|
|
|
|
if (!defined("ABSPATH")) {
|
|
echo "big bag of potatoes";
|
|
exit();
|
|
}
|
|
|
|
class PrijsPerPostcode
|
|
{
|
|
public function __construct()
|
|
{
|
|
add_action("init", [$this, "init"], 1);
|
|
add_action("rest_api_init", "register_modal_api");
|
|
}
|
|
|
|
public function init()
|
|
{
|
|
if (session_status() == PHP_SESSION_NONE) {
|
|
ob_start();
|
|
@session_start();
|
|
}
|
|
$uri = $_SERVER["REQUEST_URI"];
|
|
init_postcode_handlers($uri);
|
|
init_postode_admin();
|
|
|
|
add_filter("woocommerce_sale_flash", "__return_null");
|
|
|
|
//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_filter(
|
|
"woocommerce_variation_is_visible",
|
|
[$this, "filter_variation_by_local_price"],
|
|
10,
|
|
4,
|
|
);
|
|
add_filter(
|
|
"gettext",
|
|
[$this, "change_variation_regular_price_label"],
|
|
99,
|
|
3,
|
|
);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
function filter_variation_by_local_price(
|
|
$visible,
|
|
$variation_id,
|
|
$parent_id,
|
|
$variation,
|
|
) {
|
|
// Ensure $variation is a valid object
|
|
if (!$variation instanceof WC_Product_Variation) {
|
|
$variation = wc_get_product($variation_id);
|
|
}
|
|
|
|
if (!$variation) {
|
|
return false;
|
|
}
|
|
|
|
$is_local = isset($_SESSION["postcode_is_local"])
|
|
? $_SESSION["postcode_is_local"]
|
|
: false;
|
|
$price = $is_local
|
|
? $variation->get_meta("_local_price", true)
|
|
: $variation->get_regular_price();
|
|
|
|
if (empty($price) || floatval($price) == 0) {
|
|
return false;
|
|
}
|
|
|
|
return $visible;
|
|
}
|
|
|
|
public function change_variation_regular_price_label(
|
|
$translated_text,
|
|
$text,
|
|
$domain,
|
|
) {
|
|
if (
|
|
"woocommerce" === $domain &&
|
|
is_admin() &&
|
|
isset($_REQUEST["action"]) &&
|
|
"woocommerce_load_variations" === $_REQUEST["action"]
|
|
) {
|
|
if ($translated_text === "Reguliere prijs (%s)") {
|
|
$translated_text = "Prijs overige regios (%s)";
|
|
}
|
|
}
|
|
return $translated_text;
|
|
}
|
|
}
|
|
|
|
new PrijsPerPostcode();
|