Last active
April 19, 2023 09:36
-
-
Save divienginesupport/dff5535c79c0bb072f88ff5e8bd2ef2a to your computer and use it in GitHub Desktop.
WooCommerce Snippet - Shortcode for displaying a cart icon with the total amount in the cart next to it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Shortcode for displaying a WooCommerce cart icon with the cart total. | |
* The cart icon is linked to the WooCommerce cart page. | |
* Shortcode tag: [divi_engine_cart_icon] | |
* | |
* Make sure to add this CSS also https://gist.github.com/divienginesupport/ea2d56f435c94ca7607c243b9c8d829e | |
* | |
*/ | |
function divi_engine_cart_icon_shortcode() { | |
// Check if WooCommerce is active | |
if (!class_exists('WooCommerce')) { | |
// If WooCommerce is not active, return an empty string | |
return ''; | |
} | |
// Get the cart object from WooCommerce | |
$cart = WC()->cart; | |
// Get the total value of the items in the cart | |
$cart_total = $cart->get_total(); | |
// Get the URL of the WooCommerce cart page | |
$cart_url = wc_get_cart_url(); | |
// Define the cart icon using the Divi icon font and the correct unicode | |
$cart_icon = '<span class="de-icon"></span>'; | |
// Build the HTML output for displaying the cart icon with the cart total | |
$output = '<div class="my-cart-icon-wrapper">'; // Start wrapper div | |
$output .= '<a href="' . esc_url($cart_url) . '">'; // Add link to cart page | |
$output .= $cart_icon; // Add cart icon | |
$output .= '<span class="my-cart-total">' . $cart_total . '</span>'; // Add cart total | |
$output .= '</a>'; // Close link tag | |
$output .= '</div>'; // Close wrapper div | |
// Return the generated HTML output | |
return $output; | |
} | |
// Register the function as a shortcode with the tag 'divi_engine_cart_icon' | |
add_shortcode('divi_engine_cart_icon', 'divi_engine_cart_icon_shortcode'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment