I wrote PHP code to display a Woocommerce notice - "free shipping for $40 & under" only once on shop page and once on cart page, but also in cart should not be more than 40$. The code works, but i am not sure that it is the best way to do it:
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$shop_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") ."://$_SERVER[HTTP_HOST]/shop/";
$cart_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") ."://$_SERVER[HTTP_HOST]/cart/";
$first_time_shop = false;
if ((!isset($_COOKIE["mynoticeshop"]))&&($actual_link == $shop_link))
{
$first_time_shop = true;
setcookie("mynoticeshop", "mynoticesaleshop", time() + 600, $shop_link);
}
if ($first_time_shop) {
function sp_custom_notice() {
$subtotal = WC()->cart->subtotal;
$free_shipping_threshold = 40;
if ($subtotal < $free_shipping_threshold) {
wc_add_notice( 'free shipping for $40 & under', 'notice' );
}
}
add_filter( 'woocommerce_init', 'sp_custom_notice' );
}
$first_time_cart = false;
if ((!isset($_COOKIE["mynoticecart"]))&&($actual_link == $cart_link)) {
$first_time_cart = true;
setcookie("mynoticecart", "mynoticesalecart", time() + 600, $cart_link);
}
if ($first_time_cart) {
function sp_custom_notice() {
$subtotal = WC()->cart->subtotal;
$free_shipping_threshold = 40;
if ($subtotal < $free_shipping_threshold) {
wc_add_notice( ' free shipping for $40 & under', 'notice' );
}
}
add_filter( 'woocommerce_init', 'sp_custom_notice' );
}
I am new to Programming and I am interested to know if there is a better and shorter solution of this code?