Remove the PHP class
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Plugin Name: Prijzen per poscodeereeks.
|
* Plugin Name: Prijs per poscodeereeks.
|
||||||
* Description: Producten worden gefiltered aan de hand van opgegegeven postcodereeksen.Gebruikers met een postcode die in een opgegegeven postcodereeks valt zal een lokale prijs zien. Waar elke gebruiker met een postcode die buiten eem opgegeven reeks valt zal de reguliere prijs te zien krijgen.
|
* Description: Producten worden gefiltered aan de hand van opgegegeven postcodereeksen.Gebruikers met een postcode die in een opgegegeven postcodereeks valt zal een lokale prijs zien. Waar elke gebruiker met een postcode die buiten eem opgegeven reeks valt zal de reguliere prijs te zien krijgen.
|
||||||
* Author: Remo Zaros
|
* Author: Remo Zaros
|
||||||
* Version: 0.9.82
|
* Version: 0.9.090
|
||||||
* Text Domeain: prijs-per-postcode
|
* Text Domeain: prijs-per-postcode
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -17,6 +17,7 @@ if (!defined("ABSPATH")) {
|
|||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
register_activation_hook( __FILE__, 'create_pppr_salt' );
|
register_activation_hook( __FILE__, 'create_pppr_salt' );
|
||||||
if ( ! defined( 'WP_PPPR_SALT' ) ) {
|
if ( ! defined( 'WP_PPPR_SALT' ) ) {
|
||||||
$salt_file = __DIR__ . '/wp-pppr-salt.php';
|
$salt_file = __DIR__ . '/wp-pppr-salt.php';
|
||||||
@@ -28,66 +29,25 @@ if ( ! defined( 'WP_PPPR_SALT' ) ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class PrijsPerPostcode
|
add_filter("woocommerce_sale_flash", "__return_null");
|
||||||
{
|
add_filter('woocommerce_show_variation_price', '__return_true');
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
add_action("init", [$this, "init"], 1);
|
|
||||||
add_action("rest_api_init", "register_modal_api");
|
|
||||||
}
|
|
||||||
|
|
||||||
public function init()
|
|
||||||
{
|
|
||||||
|
|
||||||
$uri = $_SERVER["REQUEST_URI"];
|
add_action("init", function() {
|
||||||
init_postcode_handlers($uri);
|
$uri = $_SERVER["REQUEST_URI"];
|
||||||
init_postode_admin();
|
init_postcode_handlers($uri);
|
||||||
|
// set the admin page
|
||||||
|
init_postode_admin();
|
||||||
|
}, 1
|
||||||
|
);
|
||||||
|
|
||||||
add_filter("woocommerce_sale_flash", "__return_null");
|
|
||||||
add_filter('woocommerce_show_variation_price', '__return_true');
|
|
||||||
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(
|
// Set endpoint (postcode-modal/v1/submit) for the modal form.
|
||||||
"woocommerce_get_price_html",
|
add_action("rest_api_ini", "register_modal_api");
|
||||||
[$this, "display_local_price_on_product"],
|
|
||||||
10,
|
|
||||||
2,
|
|
||||||
);
|
|
||||||
add_action("template_redirect", [
|
|
||||||
$this,
|
|
||||||
"check_postcode_on_every_woocommerce_page",
|
|
||||||
]);
|
|
||||||
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,
|
|
||||||
);
|
|
||||||
|
|
||||||
}
|
/*Add local orice field to variation product.*/
|
||||||
|
add_action("woocommerce_variation_options_pricing",
|
||||||
public function add_local_price_field($loop, $variation_data, $variation)
|
function($loop, $variation_data, $variation)
|
||||||
{
|
{
|
||||||
woocommerce_wp_text_input([
|
woocommerce_wp_text_input([
|
||||||
"id" => "_local_price[" . $loop . "]",
|
"id" => "_local_price[" . $loop . "]",
|
||||||
@@ -100,9 +60,13 @@ class PrijsPerPostcode
|
|||||||
"data_type" => "price",
|
"data_type" => "price",
|
||||||
"wrapper_class" => "form-row form-row-first", // Left half
|
"wrapper_class" => "form-row form-row-first", // Left half
|
||||||
]);
|
]);
|
||||||
}
|
}, 10, 3,
|
||||||
|
);
|
||||||
|
|
||||||
public function save_local_price_field($variation_id, $i)
|
|
||||||
|
/*Save the local price on product save */
|
||||||
|
add_action("woocommerce_save_product_variation",
|
||||||
|
function ($variation_id, $i)
|
||||||
{
|
{
|
||||||
$local_price = $_POST["_local_price"][$i];
|
$local_price = $_POST["_local_price"][$i];
|
||||||
if (isset($local_price)) {
|
if (isset($local_price)) {
|
||||||
@@ -112,84 +76,30 @@ class PrijsPerPostcode
|
|||||||
wc_clean($local_price),
|
wc_clean($local_price),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}, 10, 2,
|
||||||
|
);
|
||||||
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($_COOKIE["PPPR"]) &&
|
|
||||||
pppr_is_local()
|
|
||||||
) {
|
|
||||||
$local_price = get_post_meta(
|
|
||||||
$variation_id,
|
|
||||||
"_local_price",
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
if ($local_price) {
|
|
||||||
$product->set_price($local_price);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function check_postcode_on_every_woocommerce_page()
|
|
||||||
{
|
|
||||||
if (is_admin() || defined("DOING_AJAX")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Replace "Reguliere prijs" with "Prijs overige regios" in variation prodict admin
|
||||||
|
add_filter( "gettext", function( $translated_text, $text, $domain,) {
|
||||||
if (
|
if (
|
||||||
(is_product() ||
|
"woocommerce" === $domain &&
|
||||||
is_product_category() ||
|
is_admin() &&
|
||||||
is_product_tag() ||
|
isset($_REQUEST["action"]) &&
|
||||||
is_cart() ||
|
"woocommerce_load_variations" === $_REQUEST["action"]
|
||||||
is_checkout() ||
|
|
||||||
is_account_page()) &&
|
|
||||||
!is_shop()
|
|
||||||
) {
|
) {
|
||||||
if (!verify_pppr_cookie_string()) {
|
if ($translated_text === "Reguliere prijs (%s)") {
|
||||||
if (!WC()->cart->is_empty()) {
|
$translated_text = "Prijs overige regios (%s)";
|
||||||
WC()->cart->empty_cart();
|
|
||||||
}
|
|
||||||
wp_safe_redirect($this->get_shop_url());
|
|
||||||
exit();
|
|
||||||
}else{
|
|
||||||
update_expire_pppr_cookie();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return $translated_text;
|
||||||
|
}, 99, 3,
|
||||||
|
);
|
||||||
|
|
||||||
public function display_local_price_on_product($price_html, $product)
|
|
||||||
{
|
|
||||||
if (
|
|
||||||
$product->is_type("variation") &&
|
|
||||||
isset($_COOKIE["PPPR"]) &&
|
|
||||||
pppr_is_local()
|
|
||||||
) {
|
|
||||||
$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(
|
/* Filter out variations whwre the price is either 0 or ''
|
||||||
|
*/
|
||||||
|
add_filter("woocommerce_variation_is_visible",
|
||||||
|
function (
|
||||||
$visible,
|
$visible,
|
||||||
$variation_id,
|
$variation_id,
|
||||||
$parent_id,
|
$parent_id,
|
||||||
@@ -213,31 +123,100 @@ class PrijsPerPostcode
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $visible;
|
return $visible;
|
||||||
}
|
}, 10, 4,
|
||||||
|
);
|
||||||
|
|
||||||
public function change_variation_regular_price_label(
|
|
||||||
$translated_text,
|
/* Dynamically recalculates the cart total by overriding the standard price
|
||||||
$text,
|
* with a region-specific _local_price for variations when
|
||||||
$domain,
|
* the user's location matches the PPPR cookie criteria
|
||||||
) {
|
*/
|
||||||
if (
|
add_action("woocommerce_before_calculate_totals",
|
||||||
"woocommerce" === $domain &&
|
function($cart)
|
||||||
is_admin() &&
|
{
|
||||||
isset($_REQUEST["action"]) &&
|
if (is_admin() && !defined("DOING_AJAX")) {
|
||||||
"woocommerce_load_variations" === $_REQUEST["action"]
|
return;
|
||||||
) {
|
}
|
||||||
if ($translated_text === "Reguliere prijs (%s)") {
|
|
||||||
$translated_text = "Prijs overige regios (%s)";
|
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($_COOKIE["PPPR"]) && pppr_is_local() ) {
|
||||||
|
$local_price = get_post_meta(
|
||||||
|
$variation_id,
|
||||||
|
"_local_price",
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($local_price) {
|
||||||
|
$product->set_price($local_price);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $translated_text;
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
public function get_shop_url () {
|
|
||||||
return (function_exists('wcml_get_store_url'))
|
add_filter( "woocommerce_get_price_html",
|
||||||
? wcml_get_store_url() // WPML WooCommerce Multilingual
|
function($price_html, $product)
|
||||||
: get_permalink(wc_get_page_id('shop')); // Fallback
|
{
|
||||||
}
|
if (
|
||||||
|
$product->is_type("variation") &&
|
||||||
|
isset($_COOKIE["PPPR"]) &&
|
||||||
|
pppr_is_local()
|
||||||
|
) {
|
||||||
|
$local_price = get_post_meta(
|
||||||
|
$product->get_id(),
|
||||||
|
"_local_price",
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
if ($local_price !== "") {
|
||||||
|
return wc_price($local_price);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $price_html;
|
||||||
|
}, 10, 2,
|
||||||
|
);
|
||||||
|
|
||||||
|
/* Enforces region-locking by clearing the cart and redirecting users to
|
||||||
|
* the shop page if their location cookie is invalid or expired when
|
||||||
|
* accessing key WooCommerce pages.
|
||||||
|
*/
|
||||||
|
add_action("template_redirect",
|
||||||
|
function ()
|
||||||
|
{
|
||||||
|
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 (!verify_pppr_cookie_string()) {
|
||||||
|
if (!WC()->cart->is_empty()) {
|
||||||
|
WC()->cart->empty_cart();
|
||||||
|
}
|
||||||
|
wp_safe_redirect(get_shop_url());
|
||||||
|
exit();
|
||||||
|
}else{
|
||||||
|
update_expire_pppr_cookie();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
function get_shop_url () {
|
||||||
|
return (function_exists('wcml_get_store_url'))
|
||||||
|
? wcml_get_store_url() // WPML WooCommerce Multilingual
|
||||||
|
: get_permalink(wc_get_page_id('shop')); // Fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
new PrijsPerPostcode();
|
|
||||||
|
|||||||
Reference in New Issue
Block a user