Uniformize ApplePay javascript coding style.

Add support for checkout form data to ApplePay.
Add checkout shipping mode option in ApplePay settings.
This commit is contained in:
Pedro Silva 2023-11-09 17:50:57 +00:00
parent 3a39bccc54
commit 2f5f51e518
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
9 changed files with 292 additions and 248 deletions

View file

@ -70,6 +70,8 @@ class CartScriptParamsEndpoint implements EndpointInterface {
wc_maybe_define_constant( 'WOOCOMMERCE_CART', true );
}
$include_shipping = (bool) wc_clean( wp_unslash( $_GET['shipping'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$script_data = $this->smart_button->script_data();
$total = (float) WC()->cart->get_total( 'numeric' );
@ -79,52 +81,23 @@ class CartScriptParamsEndpoint implements EndpointInterface {
$shop_country_code = $base_location['country'] ?? '';
$currency_code = get_woocommerce_currency();
$response = array(
'url_params' => $script_data['url_params'],
'button' => $script_data['button'],
'messages' => $script_data['messages'],
'amount' => WC()->cart->get_total( 'raw' ),
$calculated_packages = WC()->shipping->calculate_shipping(
WC()->cart->get_shipping_packages()
'total' => $total,
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
'currency_code' => $currency_code,
'country_code' => $shop_country_code,
);
$shipping_packages = array();
foreach ( $calculated_packages[0]['rates'] as $rate ) {
$rate_cost = $rate->get_cost();
/**
* The shipping rate.
*
* @var \WC_Shipping_Rate $rate
*/
$shipping_packages[] = array(
'id' => $rate->get_id(),
'label' => $rate->get_label(),
'cost' => (float) $rate_cost,
'cost_str' => ( new Money( (float) $rate_cost, $currency_code ) )->value_str(),
'description' => html_entity_decode(
wp_strip_all_tags(
wc_price( (float) $rate->get_cost(), array( 'currency' => get_woocommerce_currency() ) )
)
),
);
if ( $include_shipping ) {
$response = $this->append_shipping_data( $response, $currency_code );
}
wp_send_json_success(
array(
'url_params' => $script_data['url_params'],
'button' => $script_data['button'],
'messages' => $script_data['messages'],
'amount' => WC()->cart->get_total( 'raw' ),
'total' => $total,
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
'currency_code' => $currency_code,
'country_code' => $shop_country_code,
'chosen_shipping_methods' => WC()->session->get( 'chosen_shipping_methods' ),
'shipping_packages' => $shipping_packages
)
);
wp_send_json_success( $response );
return true;
} catch ( Throwable $error ) {
$this->logger->error( "CartScriptParamsEndpoint execution failed. {$error->getMessage()} {$error->getFile()}:{$error->getLine()}" );
@ -133,4 +106,45 @@ class CartScriptParamsEndpoint implements EndpointInterface {
return false;
}
}
/**
* Appends shipping data to response.
*
* @param array $response The response array.
* @param string $currency_code The currency code.
* @return array
*/
private function append_shipping_data( array $response, string $currency_code ): array {
$calculated_packages = WC()->shipping->calculate_shipping(
WC()->cart->get_shipping_packages()
);
$shipping_packages = array();
foreach ( $calculated_packages[0]['rates'] as $rate ) {
$rate_cost = $rate->get_cost();
/**
* The shipping rate.
*
* @var \WC_Shipping_Rate $rate
*/
$shipping_packages[] = array(
'id' => $rate->get_id(),
'label' => $rate->get_label(),
'cost' => (float) $rate_cost,
'cost_str' => ( new Money( (float) $rate_cost, $currency_code ) )->value_str(),
'description' => html_entity_decode(
wp_strip_all_tags(
wc_price( (float) $rate->get_cost(), array( 'currency' => get_woocommerce_currency() ) )
)
),
);
}
$response['chosen_shipping_methods'] = WC()->session->get( 'chosen_shipping_methods' );
$response['shipping_packages'] = $shipping_packages;
return $response;
}
}