Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. <?php
  2. /**
  3. * Custom Loop Add to Cart.
  4. *
  5. * Template with quantity and ajax.
  6. */
  7.  
  8. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
  9.  
  10. global $product;
  11. ?>
  12.  
  13. <?php if ( ! $product->is_in_stock() ) : ?>
  14.  
  15. <a href="<?php echo apply_filters( 'out_of_stock_add_to_cart_url', get_permalink( $product->id ) ); ?>" class="button"><?php echo apply_filters( 'out_of_stock_add_to_cart_text', __( 'Read More', 'woocommerce' ) ); ?></a>
  16.  
  17. <?php else : ?>
  18.  
  19. <?php
  20. $link = array(
  21. 'url' => '',
  22. 'label' => '',
  23. 'class' => ''
  24. );
  25.  
  26. switch ( $product->product_type ) {
  27. case "variable" :
  28. $link['url'] = apply_filters( 'variable_add_to_cart_url', get_permalink( $product->id ) );
  29. $link['label'] = apply_filters( 'variable_add_to_cart_text', __( 'Select options', 'woocommerce' ) );
  30. break;
  31. case "grouped" :
  32. $link['url'] = apply_filters( 'grouped_add_to_cart_url', get_permalink( $product->id ) );
  33. $link['label'] = apply_filters( 'grouped_add_to_cart_text', __( 'View options', 'woocommerce' ) );
  34. break;
  35. case "external" :
  36. $link['url'] = apply_filters( 'external_add_to_cart_url', get_permalink( $product->id ) );
  37. $link['label'] = apply_filters( 'external_add_to_cart_text', __( 'Read More', 'woocommerce' ) );
  38. break;
  39. default :
  40. if ( $product->is_purchasable() ) {
  41. $link['url'] = apply_filters( 'add_to_cart_url', esc_url( $product->add_to_cart_url() ) );
  42. $link['label'] = apply_filters( 'add_to_cart_text', __( 'Add to cart', 'woocommerce' ) );
  43. $link['class'] = apply_filters( 'add_to_cart_class', 'add_to_cart_button' );
  44. } else {
  45. $link['url'] = apply_filters( 'not_purchasable_url', get_permalink( $product->id ) );
  46. $link['label'] = apply_filters( 'not_purchasable_text', __( 'Read More', 'woocommerce' ) );
  47. }
  48. break;
  49. }
  50.  
  51. // If there is a simple product.
  52. if ( $product->product_type == 'simple' ) {
  53. ?>
  54. <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype="multipart/form-data">
  55. <?php
  56. // Displays the quantity box.
  57. woocommerce_quantity_input();
  58.  
  59. // Display the submit button.
  60. echo sprintf( '<button type="submit" data-product_id="%s" data-product_sku="%s" data-quantity="1" class="%s button product_type_simple">%s</button>', esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_html( $link['label'] ) );
  61. ?>
  62. </form>
  63. <?php
  64. } else {
  65. echo apply_filters( 'woocommerce_loop_add_to_cart_link', sprintf('<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="%s button product_type_%s">%s</a>', esc_url( $link['url'] ), esc_attr( $product->id ), esc_attr( $product->get_sku() ), esc_attr( $link['class'] ), esc_attr( $product->product_type ), esc_html( $link['label'] ) ), $product, $link );
  66. }
  67.  
  68. ?>
  69.  
  70. <?php endif; ?>
  71.  
  72. function cs_wc_loop_add_to_cart_scripts() {
  73. if ( is_shop() || is_product_category() || is_product_tag() || is_product() || is_front_page() || is_home() ) : ?>
  74.  
  75. <script>
  76. jQuery(document).ready(function($) {
  77. $(document).on( 'change', '.quantity .qty', function() {
  78. $(this).parent('.quantity').next('.add_to_cart_button').attr('data-quantity', $(this).val());
  79. });
  80. });
  81. </script>
  82.  
  83. <?php endif;
  84. }
  85.  
  86. add_action( 'wp_footer', 'cs_wc_loop_add_to_cart_scripts' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement