grouped.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Grouped product add to cart
  4. *
  5. * This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/grouped.php.
  6. *
  7. * HOWEVER, on occasion WooCommerce will need to update template files and you
  8. * (the theme developer) will need to copy the new files to your theme to
  9. * maintain compatibility. We try to do this as little as possible, but it does
  10. * happen. When this occurs the version of the template file will be bumped and
  11. * the readme will list any important changes.
  12. *
  13. * @see https://docs.woocommerce.com/document/template-structure/
  14. * @package WooCommerce/Templates
  15. * @version 3.4.0
  16. */
  17. defined( 'ABSPATH' ) || exit;
  18. global $product, $post;
  19. do_action( 'woocommerce_before_add_to_cart_form' ); ?>
  20. <form class="cart grouped_form" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
  21. <table cellspacing="0" class="woocommerce-grouped-product-list group_table">
  22. <tbody>
  23. <?php
  24. $quantites_required = false;
  25. $previous_post = $post;
  26. $grouped_product_columns = apply_filters( 'woocommerce_grouped_product_columns', array(
  27. 'quantity',
  28. 'label',
  29. 'price',
  30. ), $product );
  31. foreach ( $grouped_products as $grouped_product_child ) {
  32. $post_object = get_post( $grouped_product_child->get_id() );
  33. $quantites_required = $quantites_required || ( $grouped_product_child->is_purchasable() && ! $grouped_product_child->has_options() );
  34. $post = $post_object; // WPCS: override ok.
  35. setup_postdata( $post );
  36. echo '<tr id="product-' . esc_attr( $grouped_product_child->get_id() ) . '" class="woocommerce-grouped-product-list-item ' . esc_attr( implode( ' ', wc_get_product_class( '', $grouped_product_child->get_id() ) ) ) . '">';
  37. // Output columns for each product.
  38. foreach ( $grouped_product_columns as $column_id ) {
  39. do_action( 'woocommerce_grouped_product_list_before_' . $column_id, $grouped_product_child );
  40. switch ( $column_id ) {
  41. case 'quantity':
  42. ob_start();
  43. if ( ! $grouped_product_child->is_purchasable() || $grouped_product_child->has_options() || ! $grouped_product_child->is_in_stock() ) {
  44. woocommerce_template_loop_add_to_cart();
  45. } elseif ( $grouped_product_child->is_sold_individually() ) {
  46. echo '<input type="checkbox" name="' . esc_attr( 'quantity[' . $grouped_product_child->get_id() . ']' ) . '" value="1" class="wc-grouped-product-add-to-cart-checkbox" />';
  47. } else {
  48. do_action( 'woocommerce_before_add_to_cart_quantity' );
  49. woocommerce_quantity_input( array(
  50. 'input_name' => 'quantity[' . $grouped_product_child->get_id() . ']',
  51. 'input_value' => isset( $_POST['quantity'][ $grouped_product_child->get_id() ] ) ? wc_stock_amount( wc_clean( wp_unslash( $_POST['quantity'][ $grouped_product_child->get_id() ] ) ) ) : 0, // WPCS: CSRF ok, input var okay, sanitization ok.
  52. 'min_value' => apply_filters( 'woocommerce_quantity_input_min', 0, $grouped_product_child ),
  53. 'max_value' => apply_filters( 'woocommerce_quantity_input_max', $grouped_product_child->get_max_purchase_quantity(), $grouped_product_child ),
  54. ) );
  55. do_action( 'woocommerce_after_add_to_cart_quantity' );
  56. }
  57. $value = ob_get_clean();
  58. break;
  59. case 'label':
  60. $value = '<label for="product-' . esc_attr( $grouped_product_child->get_id() ) . '">';
  61. $value .= $grouped_product_child->is_visible() ? '<a href="' . esc_url( apply_filters( 'woocommerce_grouped_product_list_link', $grouped_product_child->get_permalink(), $grouped_product_child->get_id() ) ) . '">' . $grouped_product_child->get_name() . '</a>' : $grouped_product_child->get_name();
  62. $value .= '</label>';
  63. break;
  64. case 'price':
  65. $value = $grouped_product_child->get_price_html() . wc_get_stock_html( $grouped_product_child );
  66. break;
  67. default:
  68. $value = '';
  69. break;
  70. }
  71. echo '<td class="woocommerce-grouped-product-list-item__' . esc_attr( $column_id ) . '">' . apply_filters( 'woocommerce_grouped_product_list_column_' . $column_id, $value, $grouped_product_child ) . '</td>'; // WPCS: XSS ok.
  72. do_action( 'woocommerce_grouped_product_list_after_' . $column_id, $grouped_product_child );
  73. }
  74. echo '</tr>';
  75. }
  76. $post = $previous_post; // WPCS: override ok.
  77. setup_postdata( $post );
  78. ?>
  79. </tbody>
  80. </table>
  81. <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" />
  82. <?php if ( $quantites_required ) : ?>
  83. <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
  84. <button type="submit" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
  85. <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
  86. <?php endif; ?>
  87. </form>
  88. <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>