Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Convert this into a globally usable shortcode WordPress

  • SOLVED

Hi!

I have found this small piece of handy code to tell the customer to buy more in order to get free shipping in Woocommerce.

// If your free shipping threshold is 450 as per Woocommerce settings...
// ...show a cart notice if order subtotal is less than 450

function cart_notice() {
$maximum = 450;
$current = WC()->cart->subtotal;
if ( $current < $maximum ) {
echo '<div class="woocommerce-message">Få din ordre tilsendt gratis hvis du bestiller for ' . ($maximum - $current) . ' kr. mere!</div>';
}
}
add_action( 'woocommerce_before_cart', 'cart_notice' );


However, this specific code only outputs the message on the cart page, I would like to be able to use this information anywhere as a shortcode.

Can one of your wp-brainiacs make a shortcode out of this code so that I can use it anywehere?

Also, would it be possible to apply a CSS class and then remove it again depending on whether the customer have added anything to their cart, so that I can hide this until the customer actually starts to add stuff to his cart?

Something like

<div class="free-shipping-calculator hide">...</div>

When the customer haven't put anything in their cart, and then remove the "hide" class once the customer does?

Answers (3)

2015-06-02

Jayaram Y answers:

Hi,

Add this in your functions.php file



// If your free shipping threshold is 450 as per Woocommerce settings...
// ...show a cart notice if order subtotal is less than 450

function cart_notice() {

$maximum = 450;
$current = WC()->cart->subtotal;

if ( $current < $maximum ) {

echo '<div class="woocommerce-message">Få din ordre tilsendt gratis hvis du bestiller for ' . ($maximum - $current) . ' kr. mere!</div>';

}
}
add_action( 'woocommerce_before_cart', 'cart_notice' );
add_shortcode( 'cart_notice_sc', 'cart_notice' );



ans use [cart_notice_sc] as shortcode where ever you want..

Here is the link i tested.

http://dev.igenero.in/shopping/?page_id=119

Hope this helps!


cruiseback comments:

Same problem as with previous posted solution, content goes into the top of the page.


Jayaram Y comments:

can you post the website url/screenshot here.. to check why its going to top of the screen.. I use the same shortcode in my page and is working fine as shown in the example link


cruiseback comments:

Sure