Advertisement
oscarviedma

Agregar shortcode y contador ajax al botón del carrito woocommerce

Aug 13th, 2020
1,652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. // Crear shortcode para el item del carrito de WooCommerce
  2. add_shortcode ('ov_boton_carrito', 'ov_boton_carrito' );
  3.  
  4. function ov_boton_carrito() {
  5.     ob_start();
  6.  
  7.         $cart_count = WC()->cart->cart_contents_count; // Variable del recuento del carrito
  8.  
  9.         ?>
  10.         <li class="cart-sc"><a class="cart-contents" title="Mi carrito">
  11.         <?php
  12.         if ( $cart_count > 0 ) {
  13.        ?>
  14.             <span class="cart-contents-count"><?php echo $cart_count; ?></span>
  15.         <?php
  16.         }
  17.         ?>
  18.         </a></li>
  19.         <?php
  20.            
  21.     return ob_get_clean();
  22.  
  23. }
  24.  
  25. // Agregar contador de productos mediante ajax
  26. add_filter( 'woocommerce_add_to_cart_fragments', 'ov_carrito_contador' );
  27. function ov_carrito_contador( $fragments ) {
  28.  
  29.     ob_start();
  30.    
  31.     $cart_count = WC()->cart->cart_contents_count;
  32.    
  33.     ?>
  34.     <a class="cart-contents" title="<?php _e( 'Ver carrito de compras' ); ?>">
  35.     <?php
  36.     if ( $cart_count > 0 ) {
  37.         ?>
  38.         <span class="cart-contents-count"><?php echo $cart_count; ?></span>
  39.         <?php            
  40.     }
  41.         ?></a>
  42.     <?php
  43.  
  44.     $fragments['a.cart-contents'] = ob_get_clean();
  45.      
  46.     return $fragments;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement