empty-mini-cart-button.php
· 2.1 KiB · PHP
原始文件
// for QRh
function add_empty_cart_message_sitewide() {
// Check if WooCommerce is active before proceeding
if ( class_exists('WooCommerce') ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var cartContainer = $('.elementor-menu-cart__main'); // Ensure this targets your specific menu cart container
// This function updates the message based on cart contents
function updateEmptyCartMessage() {
var cartCount = <?php echo WC()->cart->get_cart_contents_count(); ?>;
if (cartCount === 0) {
if (cartContainer.find('.empty-cart-message').length === 0) { // Check if the message is not already there
cartContainer.append('<div class="empty-cart-message">No hay productos en su carrito. <a href="/tienda" class="shop-link">Haga clic aquí para ir a la tienda.</a></div>');
}
} else {
cartContainer.find('.empty-cart-message').remove(); // Remove the message if the cart is not empty
}
}
// Initial call to set the right message
updateEmptyCartMessage();
// Re-check when AJAX requests complete in case the cart is updated dynamically
$(document).ajaxComplete(function() {
updateEmptyCartMessage();
});
});
</script>
<style>
.empty-cart-message {
padding: 20px;
text-align: center;
font-size: 16px;
}
.shop-link {
font-family: 'Roboto', Sans-serif;
font-size: 16px;
line-height: 24px;
color: #FFFFFF;
background-color: #21210E;
text-decoration: none;
padding: 10px 20px;
display: inline-block;
border-radius: 4px;
transition: background-color 0.3s ease;
}
.shop-link:hover {
background-color: #E50913;
}
</style>
<?php
}
}
add_action('wp_footer', 'add_empty_cart_message_sitewide');
1 | // for QRh |
2 | |
3 | function add_empty_cart_message_sitewide() { |
4 | // Check if WooCommerce is active before proceeding |
5 | if ( class_exists('WooCommerce') ) { |
6 | ?> |
7 | <script type="text/javascript"> |
8 | jQuery(document).ready(function($) { |
9 | var cartContainer = $('.elementor-menu-cart__main'); // Ensure this targets your specific menu cart container |
10 | |
11 | // This function updates the message based on cart contents |
12 | function updateEmptyCartMessage() { |
13 | var cartCount = <?php echo WC()->cart->get_cart_contents_count(); ?>; |
14 | |
15 | if (cartCount === 0) { |
16 | if (cartContainer.find('.empty-cart-message').length === 0) { // Check if the message is not already there |
17 | cartContainer.append('<div class="empty-cart-message">No hay productos en su carrito. <a href="/tienda" class="shop-link">Haga clic aquí para ir a la tienda.</a></div>'); |
18 | } |
19 | } else { |
20 | cartContainer.find('.empty-cart-message').remove(); // Remove the message if the cart is not empty |
21 | } |
22 | } |
23 | |
24 | // Initial call to set the right message |
25 | updateEmptyCartMessage(); |
26 | |
27 | // Re-check when AJAX requests complete in case the cart is updated dynamically |
28 | $(document).ajaxComplete(function() { |
29 | updateEmptyCartMessage(); |
30 | }); |
31 | }); |
32 | </script> |
33 | <style> |
34 | .empty-cart-message { |
35 | padding: 20px; |
36 | text-align: center; |
37 | font-size: 16px; |
38 | } |
39 | .shop-link { |
40 | font-family: 'Roboto', Sans-serif; |
41 | font-size: 16px; |
42 | line-height: 24px; |
43 | color: #FFFFFF; |
44 | background-color: #21210E; |
45 | text-decoration: none; |
46 | padding: 10px 20px; |
47 | display: inline-block; |
48 | border-radius: 4px; |
49 | transition: background-color 0.3s ease; |
50 | } |
51 | .shop-link:hover { |
52 | background-color: #E50913; |
53 | } |
54 | </style> |
55 | <?php |
56 | } |
57 | } |
58 | add_action('wp_footer', 'add_empty_cart_message_sitewide'); |
59 |