Elementor: Disable the Menu Cart Widget Default Function and Link to the Cart Page

Smart Web Creative - Elementor: Disable the Menu Cart Widget Default Function and Link to the Cart Page

If you have ever used the Elementor widget, “Menu Cart”, you are probably familiar with its features of showing the cart subtotal and item count. What if you wanted to take users to the cart page instead? Normally you would have to create a button with just an icon that links them there. But, by doing this, you lose out on having the dynamic features of the menu cart widget like the subtotal and item count.

What if there was a way to disable the menu cart widget default function and link to the cart page? This can be accomplished with some custom code.

Adding the Code

Go to your theme’s functions.php file and add the following code:

/**
* Redirect Menu Cart Widget to Cart Page
*/
function redirect_menu_cart_to_cart_page() {
// Check if the current page is the cart page
if (is_cart()) {
return;
}

// Get the URL of the cart page
$cart_page_url = wc_get_cart_url(); // WooCommerce cart URL

// Modify the URL if you're using a custom cart page URL
// $cart_page_url = site_url('/your-custom-cart-page'); // Example for custom cart page URL

?>
<script>
document.addEventListener('DOMContentLoaded', function() {
var menuCartLink = document.querySelector('#elementor-menu-cart__toggle_button');
if (menuCartLink) {
menuCartLink.addEventListener('click', function(event) {
event.preventDefault();
window.location.href = '<?php echo esc_url($cart_page_url); ?>';
event.stopPropagation(); // Prevent event propagation
});
}
});
</script>
<?php
}
add_action('wp_footer', 'redirect_menu_cart_to_cart_page');

Now when a user clicks on the menu cart widget, it will take them to your cart page. By adding this function, it enables the user to see their cart subtotal and item count, plus it takes them to the cart page.

Other Helpful Articles

Smart Web Creative - Quick Resources: Date and Time in PHP

Quick Resources: Date and Time in PHP

If you want to display the date and time in PHP, it can be somewhat confusing what letters to use to bring in those elements. Below is a list of all the PHP date and time options that you can use on your site: To utilize these options in PHP,

Read More »

Setting Up WooCommerce Products

Having a WooCommerce store is a great way to sell things online. However, when setting up WooCommerce products, it’s important to note what you will need for adding products to your store. The following are items that are needed for each product: Product Title Product Description and/or Product Short Description

Read More »

What Do You Need Done?:

(Please check all that apply)

Tell Us About You

Almost Done!

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info

Let's Get Some Info