<?php
/**
* Event Tickets Plus - Add other WooCommerce Product to Cart when WooCommerce Ticket is added to cart
*
* @link https://wordpress.org/plugins/woocommerce-product-dependencies/ May be a better and easier solution !!! !!! !!! (But I haven't tried it.)
*
* !!! Before using, edit the variables in this function according to your needs!!!
* Could maybe also do the inverse (if ticket product is removed from cart, also remove the chained product).
*
* From https://gist.github.com/cliffordp/30481ca323012e298418095476d49337
* For https://theeventscalendar.com/support/forums/topic/adding-product-to-community-tickets-through-woo-commerce/#post-1295497
*
* @link https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/
* @link https://woocommerce.com/products/chained-products/
*/
add_action( 'woocommerce_add_to_cart', 'cliff_woo_ticket_add_chained_product_to_cart', 10, 6 );
function cliff_woo_ticket_add_chained_product_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
//
// !!! CHANGE THESE THREE VARIABLES BEFORE USING!!!
//
$woo_ticket_product_id = 1234; // If this ticket gets added to cart...
$woo_product_id_to_add = 2747; // ...then add this product to cart as well...
$quantity_to_add_per_ticket = 1; // ... in this quantity PER TICKET being added to cart.
//
// !!! STOP EDITING HERE !!!
//
// bail if the product being added to cart is not the targeted ticket
if ( $woo_ticket_product_id !== $product_id ) {
return false;
}
$quantity_to_add_to_cart = $quantity * $quantity_to_add_per_ticket;
WC()->cart->add_to_cart( $woo_product_id_to_add, $quantity_to_add_to_cart );
}