Tribe__Tickets_Plus__Commerce__EDD__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/EDD/Cart.php
public function add_ticket_to_cart( $ticket_id, $quantity, array $extra_data = [], $additive = true ) {
$item_position = false;
if ( edd_item_in_cart( $ticket_id ) ) {
$item_position = edd_get_item_position_in_cart( $ticket_id );
}
if ( ! $additive && false !== $item_position ) {
// Remove from the cart.
edd_remove_from_cart( $item_position );
}
if ( 0 < $quantity ) {
// Add item to cart.
$options = [
'quantity' => $quantity,
];
$options = array_merge( $options, $extra_data );
if ( $additive && false !== $item_position ) {
$new_quantity = edd_get_cart_item_quantity( $ticket_id ) + $quantity;
edd_set_cart_item_quantity( $ticket_id, $new_quantity );
} else {
edd_add_to_cart( $ticket_id, $options );
// If quantities are disabled on a site, we need to manually set the cart item quantity to force it.
if ( edd_get_cart_item_quantity( $ticket_id ) !== $quantity ) {
edd_set_cart_item_quantity( $ticket_id, $quantity );
}
}
}
}
Changelog
| Version | Description |
|---|---|
| 4.11.0 | Introduced. |