Advertisement
YordanSoares

WooCommerce + WoodMart: Botón para vaciar el carrito en la barra lateral y en la página del carrito

Feb 4th, 2024
1,077
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | Software | 0 0
  1. <?php
  2. /**
  3.  * WooCommerce + WoodMart:
  4.  * Añade un botón para vaciar el carrito desde la barra lateral y en la página de detalles del carrito
  5.  */
  6. add_action( 'init', 'woocommerce_clear_cart_url' );
  7. function woocommerce_clear_cart_url() {
  8.     global $woocommerce;
  9.     if ( isset( $_GET['empty-cart'] ) && $_GET['empty-cart'] == 'yes' ) {
  10.         $woocommerce->cart->empty_cart();
  11.     }
  12. }
  13. add_action( 'woocommerce_cart_coupon', 'woocommerce_empty_cart_button' );
  14. add_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_empty_cart_button' );
  15. function woocommerce_empty_cart_button() {
  16.     $cart_url = wc_get_cart_url();
  17.     echo '<a href="'.$cart_url.'?empty-cart=yes" class="button empty_cart" title="' . esc_attr( 'Vaciar Carrito', 'woocommerce' ) . '">' . esc_html( 'Vaciar Carrito', 'woocommerce' ) . '</a>';
  18. }
  19. add_action( 'wp_footer', 'custom_scripts', 10, 1 );
  20. function custom_scripts(){
  21.     ?>
  22.     <script type="text/javascript">
  23.         (function($){
  24.             $(document).ready(function(){
  25.                 $(document).on('click','.empty_cart',function(e){
  26.                     e.preventDefault();
  27.                     if(confirm('¿Estás seguro de que deseas vaciar el carrito?')){
  28.                         var url = $(this).attr('href');
  29.                         window.location = url;
  30.                     }
  31.                 });
  32.             });
  33.         })(jQuery);
  34.     </script>
  35.     <?php
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement