Tribe__Tickets__Commerce__PayPal__Gateway::add_to_cart()

Handles adding tickets to cart.

Contents


Source

File: src/Tribe/Commerce/PayPal/Gateway.php

	public function add_to_cart() {
		global $post;

		/**
		 * Action before adding to cart
		 *
		 * @since 4.9
		 *
		 * @param array $post_data
		 */
		do_action( 'tribe_tickets_commerce_paypal_gateway_pre_add_to_cart', $_POST );

		// bail if this isn't a Tribe Commerce PayPal ticket
		if (
			empty( $_POST['product_id'] )
			|| empty( $_POST['provider'] )
			|| 'Tribe__Tickets__Commerce__PayPal__Main' !== $_POST['provider']
		) {
			return;
		}

		$cart_url      = $this->get_cart_url( '_cart' );
		$post_url      = get_permalink( $post );
		$currency_code = trim( tribe_get_option( 'ticket-commerce-currency-code' ) );
		$product_ids   = $_POST['product_id'];

		$notify_url = tribe_get_option( 'ticket-paypal-notify-url', home_url() );

		/**
		 * Filters the notify URL.
		 *
		 * The `notify_url` argument is an IPN only argument specifying the URL PayPal should
		 * use to POST the payment information.
		 *
		 * @since 4.7
		 *
		 * @see  Tribe__Tickets__Commerce__PayPal__Handler__IPN::check_response()
		 * @link https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/
		 *
		 * @param string $notify_url
		 * @param WP_Post $post The post tickets are associated with
		 * @param array $product_ids An array of ticket post IDs that are being added to the cart
		 */
		$notify_url = apply_filters( 'tribe_tickets_commerce_paypal_notify_url', $notify_url, $post, $product_ids );

		$custom_args = array( 'user_id' => get_current_user_id(), 'tribe_handler' => 'tpp', 'pid' => $post->ID );

		$invoice_number = $this->set_invoice_number();

		$custom_args['invoice'] = $invoice_number;

		/**
		 * Filters the custom arguments that will be sent ot PayPal.
		 *
		 * @since 4.7
		 *
		 * @param array   $custom_args
		 * @param WP_Post $post        The post tickets are associated with
		 * @param array   $product_ids An array of ticket post IDs that are being added to the cart
		 */
		$custom_args = apply_filters( 'tribe_tickets_commerce_paypal_custom_args', $custom_args, $post, $product_ids );

		$custom      = Tribe__Tickets__Commerce__PayPal__Custom_Argument::encode( $custom_args );

		$args = array(
			'cmd'           => '_cart',
			'add'           => 1,
			'business'      => urlencode( trim( tribe_get_option( 'ticket-paypal-email' ) ) ),
			'bn'            => 'ModernTribe_SP',
			'notify_url'    => urlencode( trim( $notify_url ) ),
			'shopping_url'  => urlencode( $post_url ),
			'return'        => $this->get_success_page_url(),
			'currency_code' => $currency_code ? $currency_code : 'USD',
			'custom'        => $custom,
			/*
			 * We're not sending an invoice anymore.
			 * It would mess up the cart cookies and we ended up not using it.
			 */
		);

		/** @var Tribe__Tickets__Commerce__PayPal__Cart__Interface $cart */
		$cart = tribe( 'tickets.commerce.paypal.cart' );
		$cart->set_id( $invoice_number );

		foreach ( $product_ids as $ticket_id ) {
			$ticket   = tribe( 'tickets.commerce.paypal' )->get_ticket( $post->ID, $ticket_id );
			$quantity = absint( $_POST[ "quantity_{$ticket_id}" ] );

			// skip if the ticket in no longer in stock or is not sellable
			if (
				! $ticket->is_in_stock()
				|| ! $ticket->date_in_range()
			) {
				continue;
			}

			$inventory    = $ticket->inventory();
			$is_unlimited = $inventory === - 1;

			// if the requested amount is greater than remaining, use remaining instead
			if ( ! $is_unlimited && $quantity > $inventory ) {
				$quantity = $inventory;
			}

			// if the ticket doesn't have a quantity, skip it
			if ( empty( $quantity ) ) {
				continue;
			}

			$args['quantity']    = $quantity;
			$args['amount']      = $ticket->price;
			$args['item_number'] = "{$post->ID}:{$ticket->ID}";
			$args['item_name']   = urlencode( wp_kses_decode_entities( $this->get_product_name( $ticket, $post ) ) );

			$cart->add_item( $ticket->ID, $quantity );

			// we can only submit one product at a time. Bail if we get to here because we have a product
			// with a requested quantity
			break;
		}

		// If there isn't a quantity at all, then there's nothing to purchase. Redirect with an error
		if ( empty( $args['quantity'] ) || ! is_numeric( $args['quantity'] ) || (int) $args['quantity'] < 1 ) {
			/**
			 * @see Tribe__Tickets__Commerce__PayPal__Errors::error_code_to_message for error codes
			 */
			wp_safe_redirect( add_query_arg( array( 'tpp_error' => 103 ), $post_url ) );
			die;
		}

		$cart->save();

		/**
		 * Filters the arguments passed to PayPal while adding items to the cart
		 *
		 * @since 4.7
		 *
		 * @param array   $args
		 * @param array   $data POST data from buy now submission
		 * @param WP_Post $post Post object that has tickets attached to it
		 */
		$args = apply_filters( 'tribe_tickets_commerce_paypal_add_to_cart_args', $args, $_POST, $post );

		$cart_url = add_query_arg( $args, $cart_url );

		/**
		 * To allow the Invoice cookie to apply we have to redirect to a page on the same domain
		 * first.
		 * The redirection is handled in the `Tribe__Tickets__Redirections::maybe_redirect` class
		 * on the `wp_loaded` action.
		 *
		 * @see Tribe__Tickets__Redirections::maybe_redirect
		 */
		$url = add_query_arg(
			array( 'tribe_tickets_redirect_to' => rawurlencode( $cart_url ) ),
			home_url()
		);

		/**
		 * Filters the add to cart redirect
		 *
		 * @since 4.9
		 *
		 * @param string $url
		 * @param string $cart_url
		 * @param array $post_data
		 */
		$url = apply_filters( 'tribe_tickets_commerce_paypal_gateway_add_to_cart_redirect', $url, $cart_url, $_POST );

		wp_redirect( $url );
		die;
	}

Top ↑

Changelog

Changelog
Version Description
4.7 Introduced.