Conditionally displaying a notice about free shipping in Woocommerce I wrote PHP code to display a Woocommerce notice - "free shipping for $40 &amp; 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']) &amp;&amp; $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$shop_link = (isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] === 'on' ? "https" : "http") ."://$_SERVER[HTTP_HOST]/shop/";

$cart_link = (isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] === 'on' ? "https" : "http") ."://$_SERVER[HTTP_HOST]/cart/";

$first_time_shop = false;

if ((!isset($_COOKIE["mynoticeshop"]))&amp;&amp;($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()-&gt;cart-&gt;subtotal;
        $free_shipping_threshold = 40;
        if ($subtotal &lt; $free_shipping_threshold) {
        wc_add_notice( 'free shipping for $40 &amp; under', 'notice' );
    }
}

add_filter( 'woocommerce_init', 'sp_custom_notice' );
}

$first_time_cart = false;
if ((!isset($_COOKIE["mynoticecart"]))&amp;&amp;($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()-&gt;cart-&gt;subtotal;
        $free_shipping_threshold = 40;
        if ($subtotal &lt; $free_shipping_threshold) {
        wc_add_notice( ' free shipping for $40 &amp; 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?
Don't Repeat Yourself
The D.R.Y. principle is a fundamental principle in programming. Anytime logic is repeated more than once, it should be abstracted. For example, the first three variable assignments contain the same ternary operation:

(isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] === 'on' ? &quot;https&quot; : &quot;http&quot;)


That could be put into a function:
function GetProtocol() {
    return isset($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] === 'on' ? &quot;https&quot; : &quot;http&quot;;
}

That way if any of that logic needed to be updated, it could be changed in once place instead of three. The HTTP_HOST could also be included in that function - in that case, a name of GetProtocolAndDomain would be appropriate.
Like Peter's answer on SO mentions- the function sp_custom_notice() can be declared once, since the only difference between the two definitions appears to be the last line - where the first argument passed is 'free shipping for $40 &amp; under' in the first case and the first argument is ' free shipping for $40 &amp; under' in the second case. There isn't really a need for a space in the second case?
Peter also claims the first three variables are not necessary:

Firstly, you don't need the 3 $*_link variables as WooCommerce has 2 conditionals - is_shop() and is_cart() to check if you're on a shop page or a cart page. Also, the , $shop_link &amp; , $cart_link in the setcookies aren't required, so you can safely get rid of them.1

This makes sense, since you can have the cookies available for all pages on the domain instead of just a single page. In the event that is_shop() and is_cart() weren't available, you could compare the last 5 characters of $_SERVER[REQUEST_URI] with cart/ or shop/ using substr() with -5 as the start parameter.
Threshold constant
It appears that the value for $free_shipping_threshold doesn't change. Because of this, it can be made a constant at the top of the file:
const FREE_SHIPPING_CONSTANT = 40;

Then that constant can be used in the sp_custom_notice function, and if that value needed to be updated, you could just look at the top of the file for the line to change.
Erroneous Line
I see the following line in your post:

$first_time_shop = true;&lt;br&gt;


Did you manually add the HTML break tag (i.e. &lt;br&gt;) when posting here? I would expect that if that was actually in your code then the PHP interpreter would thrown an error.