/* Add your event IDs in array like this: array( 123, 456 ) */ $products = array( ); $products_multi_button = array( ); /* This option does not currently work with booking slots */ /** * Add multiple variations with quantities at once in add to cart url using notation ":,..." */ function fooevents_add_multiple_products_to_cart( $url = false ) { if ( ! empty( $_GET['add-to-cart'] ) ) { // Make sure WC is installed and add-to-cart query arg exists if ( ! class_exists( 'WC_Form_Handler' ) || empty( $_REQUEST['add-to-cart'] ) ) { return; } // Remove WooCommerce's hook since it doesn't handle multiple products/variations remove_action( 'wp_loaded', array( 'WC_Form_Handler', 'add_to_cart_action' ), 20 ); $product_ids = explode( ',', $_REQUEST['add-to-cart'] ); $count = count( $product_ids ); $number = 0; foreach ( $product_ids as $id_and_quantity ) { if ( ! empty( $_GET['fooevents_bookings_method'] ) ) { $_POST['fooevents_bookings_method'] = $_GET['fooevents_bookings_method']; } else { $_POST['fooevents_bookings_method'] = 'slotdate'; } if ( ! empty( $_GET['bookings_sid'] ) ) { $_POST['fooevents_bookings_slot_val__trans'] = $_GET['bookings_sid']; } if ( ! empty( $_GET['bookings_did'] ) ) { $_POST['fooevents_bookings_date_val__trans'] = $_GET['bookings_did']; } if ( ! empty( $_GET['bookings_slot_date'] ) ) { $_POST['fooevents_bookings_slot_date_val_trans'] = $_GET['bookings_slot_date']; } // Check for quantities defined in notation (:) $id_and_quantity = explode( ':', $id_and_quantity ); $product_id = $id_and_quantity[0]; $_REQUEST['quantity'] = ! empty( $id_and_quantity[1] ) ? absint( $id_and_quantity[1] ) : 1; if ( ++$number === $count ) { // Send back to WooCommerce's add_to_cart_action method for handling. $_REQUEST['add-to-cart'] = $product_id; return WC_Form_Handler::add_to_cart_action( $url ); } $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $product_id ) ); $was_added_to_cart = false; $adding_to_cart = wc_get_product( $product_id ); if ( ! $adding_to_cart ) { continue; } $add_to_cart_handler = apply_filters( 'woocommerce_add_to_cart_handler', $adding_to_cart->get_type(), $adding_to_cart ); // Variable product handling if ( 'variable' === $add_to_cart_handler ) { fooevents_invoke_multiple_adds( 'WC_Form_Handler', 'add_to_cart_handler_variable', $product_id ); // Grouped Products } elseif ( 'grouped' === $add_to_cart_handler ) { fooevents_invoke_multiple_adds( 'WC_Form_Handler', 'add_to_cart_handler_grouped', $product_id ); // Custom Handler } elseif ( has_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler ) ) { do_action( 'woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url ); // Simple Products } else { fooevents_invoke_multiple_adds( 'WC_Form_Handler', 'add_to_cart_handler_simple', $product_id ); } } } } add_action( 'wp_loaded', 'fooevents_add_multiple_products_to_cart', 15 ); function fooevents_invoke_multiple_adds( $class_name, $methodName ) { if ( version_compare( phpversion(), '5.3', '<' ) ) { throw new Exception( 'PHP version does not support ReflectionClass::setAccessible()', __LINE__ ); } $args = func_get_args(); unset( $args[0], $args[1] ); $reflection = new ReflectionClass( $class_name ); $method = $reflection->getMethod( $methodName ); $method->setAccessible( true ); $args = array_merge( array( $reflection ), $args ); return call_user_func_array( array( $method, 'invoke' ), $args ); } /** * Display variations in a table format. */ function woocommerce_variable_add_to_cart() { global $product, $post, $products, $products_multi_button; $woocommerce_events_type = get_post_meta( $post->ID, 'WooCommerceEventsType', true ); // Comma separated list of product ID's that should display variations in table format. Leave empty to display all products ( e.g array() ) if ( in_array( $post->ID, $products ) || empty( $products ) || in_array( $post->ID, $products_multi_button ) ) { // Set default in each input box to 0 instead of 1 add_filter( 'woocommerce_quantity_input_args', 'fooevents_quantity_input_default', 10, 2 ); $variations = find_valid_variations(); // Check if the special 'price_grid' meta is set, if it is, load the default template. if ( get_post_meta( $post->ID, 'price_grid', true ) ) { // Enqueue variation scripts. wp_enqueue_script( 'wc-add-to-cart-variation' ); // Load the template wc_get_template( 'single-product/add-to-cart/variable.php', array( 'available_variations' => $product->get_available_variations(), 'attributes' => $product->get_variation_attributes(), 'selected_attributes' => $product->get_variation_default_attributes(), ) ); return; } // Custom template ?>
$value ) { if ( ! $value['variation_is_visible'] ) { continue; } if ( $variation != $value['variation_id'] ) { if ( in_array( $post->ID, $products_multi_button ) ) { ?>
$val ) { $val = str_replace( array( '-', '_' ), ' ', $val ); printf( '
%s
', $key, ucwords( $val ) ); } ?>
<script> $attr_value ) { ?>

$val ) { $val = str_replace( array( '-', '_' ), ' ', $val ); printf( '
%s
', $key, ucwords( $val ) ); } ?>
<script> $attr_value ) { ?>

ID, $products_multi_button ) ) { ?>

Add to cart

get_children() ) <= apply_filters( 'woocommerce_ajax_variation_threshold', 30, $product ); // Load the template. wc_get_template( 'single-product/add-to-cart/variable.php', array( 'available_variations' => $get_variations ? $product->get_available_variations() : false, 'attributes' => $product->get_variation_attributes(), 'selected_attributes' => $product->get_default_attributes(), ) ); } } function find_valid_variations() { global $product, $post; $variations = $product->get_available_variations(); $attributes = $product->get_attributes(); $new_variants = array(); // Loop through all variations foreach ( $variations as $variation ) { // Peruse the attributes. // 1. If both are explicitly set, this is a valid variation // 2. If one is not set, that means any, and we must 'create' the rest. $valid = true; // so far foreach ( $attributes as $slug => $args ) { if ( array_key_exists( "attribute_$slug", $variation['attributes'] ) && ! empty( $variation['attributes'][ "attribute_$slug" ] ) ) { // Exists } else { // Not exists, create $valid = false; // it contains 'anys' // loop through all options for the 'ANY' attribute, and add each foreach ( explode( '|', $attributes[ $slug ]['value'] ) as $attribute ) { $attribute = trim( $attribute ); $new_variant = $variation; $new_variant['attributes'][ "attribute_$slug" ] = $attribute; $new_variants[] = $new_variant; } } } // This contains ALL set attributes, and is itself a 'valid' variation. if ( $valid ) { $new_variants[] = $variation; } } return $new_variants; } function fooevents_quantity_input_default( $args, $product ) { $args['input_value'] = 0; return $args; } /** * Adds booking slot dropdowns only to single product page if the event is "Bookable" */ function fooevents_add_booking_slots() { global $post, $products; $wc_product = wc_get_product( $post->ID ); $woocommerce_events_type = get_post_meta( $post->ID, 'WooCommerceEventsType', true ); if ( is_plugin_active( 'fooevents_bookings/fooevents-bookings.php' ) || is_plugin_active_for_network( 'fooevents_bookings/fooevents-bookings.php' ) ) { if ( is_product() && $woocommerce_events_type == 'bookings' && ( in_array( $post->ID, $products ) || empty( $products ) ) && $wc_product->is_type( 'variable' ) ) { $fooevents_bookings = new Fooevents_Bookings(); add_action( 'fooevents_output_booking_details', array( $fooevents_bookings, 'output_booking_fields_product' ) ); } } } add_action( 'wp', 'fooevents_add_booking_slots' );