Enums
7 months ago
Messages
7 months ago
AgenticCheckoutSession.php
7 months ago
CheckoutSessions.php
4 months ago
CheckoutSessionsComplete.php
4 months ago
CheckoutSessionsUpdate.php
4 months ago
Error.php
7 months ago
AgenticCheckoutSession.php
96 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Automattic\WooCommerce\StoreApi\Routes\V1\Agentic; |
| 4 | |
| 5 | use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Messages\Messages; |
| 6 | use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Enums\SessionKey; |
| 7 | use Automattic\WooCommerce\StoreApi\Utilities\CartTokenUtils; |
| 8 | use WC_Cart; |
| 9 | |
| 10 | /** |
| 11 | * AgenticCheckoutSession class. |
| 12 | * |
| 13 | * Wrapper for all things, associated with an agentic checkout session. |
| 14 | * This class manages the cart and error handling for agentic checkout processes. |
| 15 | */ |
| 16 | final class AgenticCheckoutSession { |
| 17 | /** |
| 18 | * The WooCommerce cart instance. |
| 19 | * |
| 20 | * @var WC_Cart |
| 21 | */ |
| 22 | private $cart; |
| 23 | |
| 24 | /** |
| 25 | * Error messages handler for the checkout session. |
| 26 | * |
| 27 | * @var Messages |
| 28 | */ |
| 29 | private $messages; |
| 30 | |
| 31 | /** |
| 32 | * The checkout session ID. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | private $id; |
| 37 | |
| 38 | /** |
| 39 | * Constructor. |
| 40 | * |
| 41 | * @param WC_Cart $cart The WooCommerce cart instance. |
| 42 | */ |
| 43 | public function __construct( WC_Cart $cart ) { |
| 44 | $this->cart = $cart; |
| 45 | $this->messages = new Messages(); |
| 46 | $this->id = $this->get_or_set_checkout_session_id(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the cart instance. |
| 51 | * |
| 52 | * @return WC_Cart The WooCommerce cart instance. |
| 53 | */ |
| 54 | public function get_cart(): WC_Cart { |
| 55 | return $this->cart; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gets the messages collection. |
| 60 | * |
| 61 | * @return Messages The messages handler instance. |
| 62 | */ |
| 63 | public function get_messages(): Messages { |
| 64 | return $this->messages; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the checkout session ID. |
| 69 | * |
| 70 | * @return string The checkout session ID. |
| 71 | */ |
| 72 | public function get_id(): string { |
| 73 | return $this->id; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the checkout session ID. If it does not exist, generate a cart token for it and save to the current session. |
| 78 | * |
| 79 | * @return string Checkout Session ID stored in the current session. |
| 80 | */ |
| 81 | private function get_or_set_checkout_session_id(): string { |
| 82 | $wc_session = WC()->session; |
| 83 | if ( null === $wc_session ) { |
| 84 | return ''; |
| 85 | } |
| 86 | |
| 87 | $session_id = $wc_session->get( SessionKey::AGENTIC_CHECKOUT_SESSION_ID ); |
| 88 | if ( null === $session_id ) { |
| 89 | $session_id = CartTokenUtils::get_cart_token( (string) $wc_session->get_customer_id() ); |
| 90 | $wc_session->set( SessionKey::AGENTIC_CHECKOUT_SESSION_ID, $session_id ); |
| 91 | } |
| 92 | |
| 93 | return $session_id; |
| 94 | } |
| 95 | } |
| 96 |