Conditionally displaying a notice about free shipping in Woocommerce
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.