bogdanbatsenko

West Coast functions php

Jun 16th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1.  
  2.  
  3. // my own custom code
  4.  
  5. add_filter('add_to_cart_custom_fragments', 'woocommerce_header_add_to_cart_custom_fragment');
  6. function woocommerce_header_add_to_cart_custom_fragment( $cart_fragments ) {
  7. global $woocommerce;
  8. ob_start();
  9. ?>
  10. <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
  11. <?php
  12. $cart_fragments['a.cart-contents'] = ob_get_clean();
  13. return $cart_fragments;
  14. }
  15.  
  16.  
  17. //Add WooCommerce Cart Icon to Menu with Cart Item Count
  18. // https://wpbeaches.com/add-woocommerce-cart-icon-to-menu-with-cart-item-count/
  19.  
  20. add_shortcode ('woo_cart_but', 'woo_cart_but' );
  21. /**
  22. * Create Shortcode for WooCommerce Cart Menu Item
  23. */
  24. function woo_cart_but() {
  25. ob_start();
  26.  
  27. $cart_count = WC()->cart->cart_contents_count; // Set variable for cart item count
  28. $cart_url = wc_get_cart_url(); // Set Cart URL
  29.  
  30. ?>
  31. <li><a class="menu-item cart-contents" href="<?php echo $cart_url; ?>" title="My Basket">
  32. <?php
  33. if ( $cart_count > 0 ) {
  34. ?>
  35. <span class="cart-contents-count"><?php echo $cart_count; ?></span>
  36. <?php
  37. }
  38. ?>
  39. </a></li>
  40. <?php
  41.  
  42. return ob_get_clean();
  43. }
  44.  
  45.  
  46.  
  47. add_filter( 'woocommerce_add_to_cart_fragments', 'woo_cart_but_count' );
  48. /**
  49. * Add AJAX Shortcode when cart contents update
  50. */
  51. function woo_cart_but_count( $fragments ) {
  52.  
  53. ob_start();
  54.  
  55. $cart_count = WC()->cart->cart_contents_count;
  56. $cart_url = wc_get_cart_url();
  57.  
  58. ?>
  59. <a class="cart-contents menu-item" href="<?php echo $cart_url; ?>" title="<?php _e( 'View your shopping cart' ); ?>">
  60. <?php
  61. if ( $cart_count > 0 ) {
  62. ?>
  63. <span class="cart-contents-count"><?php echo $cart_count; ?></span>
  64. <?php
  65. }
  66. ?></a>
  67. <?php
  68.  
  69. $fragments['a.cart-contents'] = ob_get_clean();
  70.  
  71. return $fragments;
  72. }
  73.  
  74.  
  75.  
  76.  
  77. add_filter( 'main-nav', 'woo_cart_but_icon', 10, 2 ); // Change menu to suit - example uses 'top-menu'
  78.  
  79. /**
  80. * Add WooCommerce Cart Menu Item Shortcode to particular menu
  81. */
  82. function woo_cart_but_icon ( $items, $args ) {
  83. $items .= '[woo_cart_but]'; // Adding the created Icon via the shortcode already created
  84.  
  85. return $items;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment