Tribe__Tickets_Plus__Commerce__WooCommerce__Cart::add_ticket_to_cart( int $ticket_id, int $quantity, array $extra_data = array(), boolean $additive = true )
Handles the process of adding a ticket product to the cart.
Contents
If the cart contains a line item for the product, this will replace the previous quantity. If the quantity is zero and the cart contains a line item for the product, this will remove it.
Parameters
- $ticket_id
-
(int) (Required) Ticket ID.
- $quantity
-
(int) (Required) Ticket quantity.
- $extra_data
-
(array) (Optional) Extra data to send to the cart item.
Default value: array()
- $additive
-
(boolean) (Optional) Whether to add or replace tickets.
Default value: true
Source
File: src/Tribe/Commerce/WooCommerce/Cart.php
public function add_ticket_to_cart( $ticket_id, $quantity, array $extra_data = [], $additive = true ) {
/** @var WooCommerce $woocommerce */
global $woocommerce;
if ( 0 < $quantity ) {
/**
* Allow hooking into WooCommerce Add to Cart validation.
*
* Note: This is a WooCommerce filter that is not abstracted for API usage so we have to run it manually.
*
* @param bool $passed_validation Whether the item can be added to the cart.
* @param int $ticket_id Ticket ID.
* @param int $quantity Ticket quantity.
*/
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $ticket_id, $quantity );
if ( ! $passed_validation ) {
return;
}
}
try {
if ( ! $additive ) {
$cart_item_keys = $this->get_cart_item_keys_for_ticket_in_cart( $ticket_id );
if ( ! empty( $cart_item_keys ) ) {
// Remove cart items.
array_map( [ $woocommerce->cart, 'remove_cart_item' ], $cart_item_keys );
}
}
// Set quantity in cart.
if ( 0 < $quantity ) {
$woocommerce->cart->add_to_cart( $ticket_id, $quantity, 0, [], $extra_data );
}
} catch ( \Exception $exception ) {
// Item not added to / removed from cart.
}
}
Changelog
| Version | Description |
|---|---|
| 4.11.0 | Introduced. |