Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/StoreApi/Utilitie...
File: AgenticCheckoutUtils.php
<?php
[0] Fix | Delete
declare(strict_types=1);
[1] Fix | Delete
namespace Automattic\WooCommerce\StoreApi\Utilities;
[2] Fix | Delete
[3] Fix | Delete
use Automattic\WooCommerce\StoreApi\Exceptions\RouteException;
[4] Fix | Delete
use Automattic\WooCommerce\Internal\Agentic\Enums\Specs\CheckoutSessionStatus;
[5] Fix | Delete
use Automattic\WooCommerce\Internal\Agentic\Enums\Specs\ErrorCode;
[6] Fix | Delete
use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Enums\SessionKey;
[7] Fix | Delete
use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Errors\Error;
[8] Fix | Delete
use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Error as AgenticError;
[9] Fix | Delete
use Automattic\WooCommerce\Internal\Features\FeaturesController;
[10] Fix | Delete
use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\AgenticCheckoutSession;
[11] Fix | Delete
use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Messages\MessageError;
[12] Fix | Delete
use Automattic\WooCommerce\StoreApi\Routes\V1\Agentic\Messages\Messages;
[13] Fix | Delete
[14] Fix | Delete
/**
[15] Fix | Delete
* AgenticCheckoutUtils class.
[16] Fix | Delete
*
[17] Fix | Delete
* Utility class for shared Agentic Checkout API functionality.
[18] Fix | Delete
*/
[19] Fix | Delete
class AgenticCheckoutUtils {
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Get the shared parameters schema for checkout session requests.
[23] Fix | Delete
*
[24] Fix | Delete
* @return array Parameters array.
[25] Fix | Delete
*/
[26] Fix | Delete
public static function get_shared_params() {
[27] Fix | Delete
return [
[28] Fix | Delete
'items' => [
[29] Fix | Delete
'description' => __( 'Line items to add to the cart.', 'woocommerce' ),
[30] Fix | Delete
'type' => 'array',
[31] Fix | Delete
'items' => [
[32] Fix | Delete
'type' => 'object',
[33] Fix | Delete
'properties' => [
[34] Fix | Delete
'id' => [
[35] Fix | Delete
'description' => __( 'Product ID.', 'woocommerce' ),
[36] Fix | Delete
'type' => 'string',
[37] Fix | Delete
],
[38] Fix | Delete
'quantity' => [
[39] Fix | Delete
'description' => __( 'Quantity.', 'woocommerce' ),
[40] Fix | Delete
'type' => 'integer',
[41] Fix | Delete
'minimum' => 1,
[42] Fix | Delete
],
[43] Fix | Delete
],
[44] Fix | Delete
'required' => [ 'id', 'quantity' ],
[45] Fix | Delete
],
[46] Fix | Delete
],
[47] Fix | Delete
'buyer' => [
[48] Fix | Delete
'description' => __( 'Buyer information.', 'woocommerce' ),
[49] Fix | Delete
'type' => 'object',
[50] Fix | Delete
'properties' => [
[51] Fix | Delete
'first_name' => [
[52] Fix | Delete
'description' => __( 'First name.', 'woocommerce' ),
[53] Fix | Delete
'type' => 'string',
[54] Fix | Delete
],
[55] Fix | Delete
'last_name' => [
[56] Fix | Delete
'description' => __( 'Last name.', 'woocommerce' ),
[57] Fix | Delete
'type' => 'string',
[58] Fix | Delete
],
[59] Fix | Delete
'email' => [
[60] Fix | Delete
'description' => __( 'Email address.', 'woocommerce' ),
[61] Fix | Delete
'type' => 'string',
[62] Fix | Delete
'format' => 'email',
[63] Fix | Delete
],
[64] Fix | Delete
'phone_number' => [
[65] Fix | Delete
'description' => __( 'Phone number.', 'woocommerce' ),
[66] Fix | Delete
'type' => 'string',
[67] Fix | Delete
],
[68] Fix | Delete
],
[69] Fix | Delete
],
[70] Fix | Delete
'fulfillment_address' => [
[71] Fix | Delete
'description' => __( 'Fulfillment/shipping address.', 'woocommerce' ),
[72] Fix | Delete
'type' => 'object',
[73] Fix | Delete
'properties' => [
[74] Fix | Delete
'name' => [
[75] Fix | Delete
'description' => __( 'Full name.', 'woocommerce' ),
[76] Fix | Delete
'type' => 'string',
[77] Fix | Delete
],
[78] Fix | Delete
'line_one' => [
[79] Fix | Delete
'description' => __( 'Address line 1.', 'woocommerce' ),
[80] Fix | Delete
'type' => 'string',
[81] Fix | Delete
],
[82] Fix | Delete
'line_two' => [
[83] Fix | Delete
'description' => __( 'Address line 2.', 'woocommerce' ),
[84] Fix | Delete
'type' => 'string',
[85] Fix | Delete
],
[86] Fix | Delete
'city' => [
[87] Fix | Delete
'description' => __( 'City.', 'woocommerce' ),
[88] Fix | Delete
'type' => 'string',
[89] Fix | Delete
],
[90] Fix | Delete
'state' => [
[91] Fix | Delete
'description' => __( 'State/province.', 'woocommerce' ),
[92] Fix | Delete
'type' => 'string',
[93] Fix | Delete
],
[94] Fix | Delete
'country' => [
[95] Fix | Delete
'description' => __( 'Country code (ISO 3166-1 alpha-2).', 'woocommerce' ),
[96] Fix | Delete
'type' => 'string',
[97] Fix | Delete
],
[98] Fix | Delete
'postal_code' => [
[99] Fix | Delete
'description' => __( 'Postal/ZIP code.', 'woocommerce' ),
[100] Fix | Delete
'type' => 'string',
[101] Fix | Delete
],
[102] Fix | Delete
],
[103] Fix | Delete
'required' => [ 'line_one', 'city', 'country', 'postal_code' ],
[104] Fix | Delete
],
[105] Fix | Delete
];
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
/**
[109] Fix | Delete
* Add items to cart from request.
[110] Fix | Delete
*
[111] Fix | Delete
* @param array $items Items array from request.
[112] Fix | Delete
* @param CartController $cart_controller Cart controller instance.
[113] Fix | Delete
* @param Messages $messages Error messages instance.
[114] Fix | Delete
* @return Error|null Returns error response on failure, null on success.
[115] Fix | Delete
*/
[116] Fix | Delete
public static function add_items_to_cart( $items, $cart_controller, $messages ) {
[117] Fix | Delete
foreach ( $items as $item_index => $item ) {
[118] Fix | Delete
if ( ! ctype_digit( $item['id'] ) ) {
[119] Fix | Delete
return AgenticError::invalid_request(
[120] Fix | Delete
'invalid_product_id',
[121] Fix | Delete
__( 'Product ID must be numeric.', 'woocommerce' ),
[122] Fix | Delete
'$.items[' . $item_index . '].id'
[123] Fix | Delete
);
[124] Fix | Delete
}
[125] Fix | Delete
[126] Fix | Delete
$product_id = (int) $item['id'];
[127] Fix | Delete
$quantity = (int) $item['quantity'];
[128] Fix | Delete
[129] Fix | Delete
try {
[130] Fix | Delete
$cart_controller->add_to_cart(
[131] Fix | Delete
[
[132] Fix | Delete
'id' => $product_id,
[133] Fix | Delete
'quantity' => $quantity,
[134] Fix | Delete
]
[135] Fix | Delete
);
[136] Fix | Delete
} catch ( RouteException $exception ) {
[137] Fix | Delete
$message = wp_specialchars_decode( $exception->getMessage(), ENT_QUOTES );
[138] Fix | Delete
$param = '$.items[' . $item_index . ']';
[139] Fix | Delete
$message_error = null;
[140] Fix | Delete
[141] Fix | Delete
// Map WooCommerce error codes to Agentic Commerce Protocol error codes.
[142] Fix | Delete
switch ( $exception->getErrorCode() ) {
[143] Fix | Delete
case 'woocommerce_rest_product_out_of_stock':
[144] Fix | Delete
case 'woocommerce_rest_product_partially_out_of_stock':
[145] Fix | Delete
$message_error = MessageError::out_of_stock( $message, $param );
[146] Fix | Delete
break;
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
if ( null !== $message_error ) {
[150] Fix | Delete
$messages->add( $message_error );
[151] Fix | Delete
} else {
[152] Fix | Delete
// The error code is generally applicable only to MessageErrors, but we can use it here as well.
[153] Fix | Delete
return AgenticError::invalid_request( ErrorCode::INVALID, $message, $param );
[154] Fix | Delete
}
[155] Fix | Delete
}
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
return null;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Set buyer data on customer.
[163] Fix | Delete
*
[164] Fix | Delete
* @param array $buyer Buyer data.
[165] Fix | Delete
* @param \WC_Customer $customer Customer instance.
[166] Fix | Delete
*/
[167] Fix | Delete
public static function set_buyer_data( $buyer, $customer ) {
[168] Fix | Delete
if ( isset( $buyer['first_name'] ) ) {
[169] Fix | Delete
$first_name = wc_clean( wp_unslash( $buyer['first_name'] ) );
[170] Fix | Delete
$customer->set_billing_first_name( $first_name );
[171] Fix | Delete
$customer->set_shipping_first_name( $first_name );
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
if ( isset( $buyer['last_name'] ) ) {
[175] Fix | Delete
$last_name = wc_clean( wp_unslash( $buyer['last_name'] ) );
[176] Fix | Delete
$customer->set_billing_last_name( $last_name );
[177] Fix | Delete
$customer->set_shipping_last_name( $last_name );
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
if ( isset( $buyer['email'] ) ) {
[181] Fix | Delete
$email = sanitize_email( wp_unslash( $buyer['email'] ) );
[182] Fix | Delete
if ( is_email( $email ) ) {
[183] Fix | Delete
$customer->set_billing_email( $email );
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
if ( isset( $buyer['phone_number'] ) ) {
[188] Fix | Delete
$phone = wc_clean( wp_unslash( $buyer['phone_number'] ) );
[189] Fix | Delete
$customer->set_billing_phone( $phone );
[190] Fix | Delete
}
[191] Fix | Delete
[192] Fix | Delete
$customer->save();
[193] Fix | Delete
}
[194] Fix | Delete
[195] Fix | Delete
/**
[196] Fix | Delete
* Set fulfillment address on customer.
[197] Fix | Delete
*
[198] Fix | Delete
* @param array $address Address data.
[199] Fix | Delete
* @param \WC_Customer $customer Customer instance.
[200] Fix | Delete
*/
[201] Fix | Delete
public static function set_fulfillment_address( $address, $customer ) {
[202] Fix | Delete
// Only parse and set name if provided and non-empty.
[203] Fix | Delete
if ( ! empty( $address['name'] ) ) {
[204] Fix | Delete
$name = wc_clean( wp_unslash( $address['name'] ) );
[205] Fix | Delete
$name_parts = explode( ' ', $name, 2 );
[206] Fix | Delete
$first_name = $name_parts[0];
[207] Fix | Delete
$last_name = isset( $name_parts[1] ) ? $name_parts[1] : '';
[208] Fix | Delete
[209] Fix | Delete
// Set shipping names.
[210] Fix | Delete
$customer->set_shipping_first_name( $first_name );
[211] Fix | Delete
$customer->set_shipping_last_name( $last_name );
[212] Fix | Delete
} else {
[213] Fix | Delete
// Preserve existing shipping names.
[214] Fix | Delete
$first_name = $customer->get_shipping_first_name();
[215] Fix | Delete
$last_name = $customer->get_shipping_last_name();
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
// Sanitize all address fields.
[219] Fix | Delete
$line_one = wc_clean( wp_unslash( $address['line_one'] ?? '' ) );
[220] Fix | Delete
$line_two = wc_clean( wp_unslash( $address['line_two'] ?? '' ) );
[221] Fix | Delete
$city = wc_clean( wp_unslash( $address['city'] ?? '' ) );
[222] Fix | Delete
$state = wc_clean( wp_unslash( $address['state'] ?? '' ) );
[223] Fix | Delete
$postal_code = wc_clean( wp_unslash( $address['postal_code'] ?? '' ) );
[224] Fix | Delete
$country = wc_clean( wp_unslash( $address['country'] ?? '' ) );
[225] Fix | Delete
[226] Fix | Delete
// Set shipping address fields.
[227] Fix | Delete
$customer->set_shipping_address_1( $line_one );
[228] Fix | Delete
$customer->set_shipping_address_2( $line_two );
[229] Fix | Delete
$customer->set_shipping_city( $city );
[230] Fix | Delete
$customer->set_shipping_state( $state );
[231] Fix | Delete
$customer->set_shipping_postcode( $postal_code );
[232] Fix | Delete
$customer->set_shipping_country( $country );
[233] Fix | Delete
[234] Fix | Delete
// Also set as billing address if not already set.
[235] Fix | Delete
if ( ! $customer->get_billing_address_1() ) {
[236] Fix | Delete
// For billing, only set names if provided or use existing billing names.
[237] Fix | Delete
if ( ! empty( $address['name'] ) ) {
[238] Fix | Delete
$customer->set_billing_first_name( $first_name );
[239] Fix | Delete
$customer->set_billing_last_name( $last_name );
[240] Fix | Delete
}
[241] Fix | Delete
$customer->set_billing_address_1( $line_one );
[242] Fix | Delete
$customer->set_billing_address_2( $line_two );
[243] Fix | Delete
$customer->set_billing_city( $city );
[244] Fix | Delete
$customer->set_billing_state( $state );
[245] Fix | Delete
$customer->set_billing_postcode( $postal_code );
[246] Fix | Delete
$customer->set_billing_country( $country );
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
$customer->save();
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
/**
[253] Fix | Delete
* Clear fulfillment address from customer.
[254] Fix | Delete
*
[255] Fix | Delete
* @param \WC_Customer $customer Customer instance.
[256] Fix | Delete
*/
[257] Fix | Delete
public static function clear_fulfillment_address( $customer ) {
[258] Fix | Delete
// Clear shipping address.
[259] Fix | Delete
$customer->set_shipping_first_name( '' );
[260] Fix | Delete
$customer->set_shipping_last_name( '' );
[261] Fix | Delete
$customer->set_shipping_address_1( '' );
[262] Fix | Delete
$customer->set_shipping_address_2( '' );
[263] Fix | Delete
$customer->set_shipping_city( '' );
[264] Fix | Delete
$customer->set_shipping_state( '' );
[265] Fix | Delete
$customer->set_shipping_postcode( '' );
[266] Fix | Delete
$customer->set_shipping_country( '' );
[267] Fix | Delete
[268] Fix | Delete
$customer->save();
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
/**
[272] Fix | Delete
* Set billing address on customer.
[273] Fix | Delete
*
[274] Fix | Delete
* @param array $address Address data.
[275] Fix | Delete
* @param \WC_Customer $customer Customer instance.
[276] Fix | Delete
*/
[277] Fix | Delete
public static function set_billing_address( $address, $customer ) {
[278] Fix | Delete
// Only parse and set name if provided and non-empty.
[279] Fix | Delete
if ( ! empty( $address['name'] ) ) {
[280] Fix | Delete
$name = wc_clean( wp_unslash( $address['name'] ) );
[281] Fix | Delete
$name_parts = explode( ' ', $name, 2 );
[282] Fix | Delete
$first_name = $name_parts[0];
[283] Fix | Delete
$last_name = isset( $name_parts[1] ) ? $name_parts[1] : '';
[284] Fix | Delete
[285] Fix | Delete
// Set billing names.
[286] Fix | Delete
$customer->set_billing_first_name( $first_name );
[287] Fix | Delete
$customer->set_billing_last_name( $last_name );
[288] Fix | Delete
}
[289] Fix | Delete
[290] Fix | Delete
// Sanitize all address fields.
[291] Fix | Delete
$line_one = wc_clean( wp_unslash( $address['line_one'] ?? '' ) );
[292] Fix | Delete
$line_two = wc_clean( wp_unslash( $address['line_two'] ?? '' ) );
[293] Fix | Delete
$city = wc_clean( wp_unslash( $address['city'] ?? '' ) );
[294] Fix | Delete
$state = wc_clean( wp_unslash( $address['state'] ?? '' ) );
[295] Fix | Delete
$postal_code = wc_clean( wp_unslash( $address['postal_code'] ?? '' ) );
[296] Fix | Delete
$country = wc_clean( wp_unslash( $address['country'] ?? '' ) );
[297] Fix | Delete
[298] Fix | Delete
// Set billing address fields.
[299] Fix | Delete
$customer->set_billing_address_1( $line_one );
[300] Fix | Delete
$customer->set_billing_address_2( $line_two );
[301] Fix | Delete
$customer->set_billing_city( $city );
[302] Fix | Delete
$customer->set_billing_state( $state );
[303] Fix | Delete
$customer->set_billing_postcode( $postal_code );
[304] Fix | Delete
$customer->set_billing_country( $country );
[305] Fix | Delete
[306] Fix | Delete
$customer->save();
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
/**
[310] Fix | Delete
* Add Agentic Commerce Protocol headers to response.
[311] Fix | Delete
*
[312] Fix | Delete
* @param \WP_REST_Response $response Response object.
[313] Fix | Delete
* @param \WP_REST_Request $request Request object.
[314] Fix | Delete
* @return \WP_REST_Response Response with headers.
[315] Fix | Delete
*/
[316] Fix | Delete
public static function add_protocol_headers( \WP_REST_Response $response, \WP_REST_Request $request ) {
[317] Fix | Delete
// Echo Idempotency-Key header if provided.
[318] Fix | Delete
$idempotency_key = $request->get_header( 'Idempotency-Key' );
[319] Fix | Delete
if ( $idempotency_key ) {
[320] Fix | Delete
$response->header( 'Idempotency-Key', $idempotency_key );
[321] Fix | Delete
}
[322] Fix | Delete
[323] Fix | Delete
// Echo Request-Id header if provided.
[324] Fix | Delete
$request_id = $request->get_header( 'Request-Id' );
[325] Fix | Delete
if ( $request_id ) {
[326] Fix | Delete
$response->header( 'Request-Id', $request_id );
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
return $response;
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
/**
[333] Fix | Delete
* Validate that the request is signed with Jetpack blog token.
[334] Fix | Delete
*
[335] Fix | Delete
* @since 10.6.0
[336] Fix | Delete
*
[337] Fix | Delete
* @return true|\WP_Error True if valid, WP_Error otherwise.
[338] Fix | Delete
*/
[339] Fix | Delete
public static function validate_jetpack_request() {
[340] Fix | Delete
if ( class_exists( 'Automattic\Jetpack\Connection\Rest_Authentication' ) ) {
[341] Fix | Delete
if ( \Automattic\Jetpack\Connection\Rest_Authentication::is_signed_with_blog_token() ) {
[342] Fix | Delete
return true;
[343] Fix | Delete
}
[344] Fix | Delete
}
[345] Fix | Delete
[346] Fix | Delete
return new \WP_Error(
[347] Fix | Delete
'rest_forbidden',
[348] Fix | Delete
__( 'This endpoint requires Jetpack blog token authentication.', 'woocommerce' ),
[349] Fix | Delete
array( 'status' => 401 )
[350] Fix | Delete
);
[351] Fix | Delete
}
[352] Fix | Delete
[353] Fix | Delete
/**
[354] Fix | Delete
* Validates a session.
[355] Fix | Delete
*
[356] Fix | Delete
* @param AgenticCheckoutSession $checkout_session Checkout session object.
[357] Fix | Delete
* @return void
[358] Fix | Delete
*/
[359] Fix | Delete
public static function validate( AgenticCheckoutSession $checkout_session ): void {
[360] Fix | Delete
$messages = $checkout_session->get_messages();
[361] Fix | Delete
[362] Fix | Delete
// Check if ready for payment.
[363] Fix | Delete
$needs_shipping = $checkout_session->get_cart()->needs_shipping();
[364] Fix | Delete
$has_address = WC()->customer && WC()->customer->get_shipping_address_1();
[365] Fix | Delete
[366] Fix | Delete
// Add info message if shipping is needed.
[367] Fix | Delete
if ( $needs_shipping && ! $has_address ) {
[368] Fix | Delete
$messages->add(
[369] Fix | Delete
MessageError::missing(
[370] Fix | Delete
__( 'Shipping address required.', 'woocommerce' ),
[371] Fix | Delete
'$.fulfillment_address'
[372] Fix | Delete
)
[373] Fix | Delete
);
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
// Check if valid shipping method is selected (not just empty strings).
[377] Fix | Delete
$chosen_methods = WC()->session ? WC()->session->get( SessionKey::CHOSEN_SHIPPING_METHODS ) : null;
[378] Fix | Delete
$has_shipping = ! empty( $chosen_methods ) && ! empty( array_filter( $chosen_methods ) );
[379] Fix | Delete
[380] Fix | Delete
if ( $needs_shipping && ! $has_shipping ) {
[381] Fix | Delete
$messages->add(
[382] Fix | Delete
MessageError::missing(
[383] Fix | Delete
__( 'No shipping method selected.', 'woocommerce' ),
[384] Fix | Delete
'$.fulfillment_option_id'
[385] Fix | Delete
)
[386] Fix | Delete
);
[387] Fix | Delete
}
[388] Fix | Delete
}
[389] Fix | Delete
[390] Fix | Delete
/**
[391] Fix | Delete
* Calculate the status of the checkout session.
[392] Fix | Delete
*
[393] Fix | Delete
* @param AgenticCheckoutSession $checkout_session Checkout session object.
[394] Fix | Delete
*
[395] Fix | Delete
* @return string Status value.
[396] Fix | Delete
*/
[397] Fix | Delete
public static function calculate_status( AgenticCheckoutSession $checkout_session ): string {
[398] Fix | Delete
$wc_session = WC()->session;
[399] Fix | Delete
if ( null === $wc_session ) {
[400] Fix | Delete
return CheckoutSessionStatus::CANCELED;
[401] Fix | Delete
}
[402] Fix | Delete
[403] Fix | Delete
if ( $wc_session->get( SessionKey::AGENTIC_CHECKOUT_COMPLETED_ORDER_ID ) ) {
[404] Fix | Delete
return CheckoutSessionStatus::COMPLETED;
[405] Fix | Delete
}
[406] Fix | Delete
[407] Fix | Delete
if ( $wc_session->get( SessionKey::AGENTIC_CHECKOUT_PAYMENT_IN_PROGRESS ) ) {
[408] Fix | Delete
return CheckoutSessionStatus::IN_PROGRESS;
[409] Fix | Delete
}
[410] Fix | Delete
[411] Fix | Delete
// Check for validation errors.
[412] Fix | Delete
if (
[413] Fix | Delete
$checkout_session->get_messages()->has_errors()
[414] Fix | Delete
// Once we switch to using the CartController everywhere, there should be no notices and need for this.
[415] Fix | Delete
|| ! empty( wc_get_notices( 'error' ) )
[416] Fix | Delete
) {
[417] Fix | Delete
return CheckoutSessionStatus::NOT_READY_FOR_PAYMENT;
[418] Fix | Delete
}
[419] Fix | Delete
[420] Fix | Delete
return CheckoutSessionStatus::READY_FOR_PAYMENT;
[421] Fix | Delete
}
[422] Fix | Delete
[423] Fix | Delete
/**
[424] Fix | Delete
* Get the agentic commerce payment gateway from available gateways.
[425] Fix | Delete
*
[426] Fix | Delete
* Finds the first gateway that supports agentic commerce and has the required methods.
[427] Fix | Delete
*
[428] Fix | Delete
* @param array $available_gateways Array of available payment gateways.
[429] Fix | Delete
* @return \WC_Payment_Gateway|null The agentic commerce gateway or null if not found.
[430] Fix | Delete
*/
[431] Fix | Delete
public static function get_agentic_commerce_gateway( $available_gateways ) {
[432] Fix | Delete
if ( empty( $available_gateways ) ) {
[433] Fix | Delete
return null;
[434] Fix | Delete
}
[435] Fix | Delete
[436] Fix | Delete
foreach ( $available_gateways as $gateway ) {
[437] Fix | Delete
if ( $gateway->supports( \Automattic\WooCommerce\Enums\PaymentGatewayFeature::AGENTIC_COMMERCE )
[438] Fix | Delete
&& method_exists( $gateway, 'get_agentic_commerce_provider' )
[439] Fix | Delete
&& method_exists( $gateway, 'get_agentic_commerce_payment_methods' )
[440] Fix | Delete
) {
[441] Fix | Delete
return $gateway;
[442] Fix | Delete
}
[443] Fix | Delete
}
[444] Fix | Delete
[445] Fix | Delete
return null;
[446] Fix | Delete
}
[447] Fix | Delete
[448] Fix | Delete
/**
[449] Fix | Delete
* Whether the current request is within Agentic Commerce session.
[450] Fix | Delete
*
[451] Fix | Delete
* @return bool
[452] Fix | Delete
*/
[453] Fix | Delete
public static function is_agentic_commerce_session(): bool {
[454] Fix | Delete
$wc_session = WC()->session;
[455] Fix | Delete
if ( null === $wc_session ) {
[456] Fix | Delete
return false;
[457] Fix | Delete
}
[458] Fix | Delete
[459] Fix | Delete
return ! empty( $wc_session->get( SessionKey::AGENTIC_CHECKOUT_SESSION_ID ) );
[460] Fix | Delete
}
[461] Fix | Delete
}
[462] Fix | Delete
[463] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function