Merge branch 'trunk' into PCP-1393-update-to-vault-v-3

This commit is contained in:
Emili Castells Guasch 2023-11-21 09:44:34 +01:00
commit d6ad402219
24 changed files with 529 additions and 223 deletions

View file

@ -973,6 +973,7 @@ document.querySelector("#payment").before(document.querySelector("#ppcp-messages
'subscription_plan_id' => $this->subscription_helper->paypal_subscription_id(),
'variable_paypal_subscription_variations' => $this->subscription_helper->variable_paypal_subscription_variations(),
'subscription_product_allowed' => $this->subscription_helper->checkout_subscription_product_allowed(),
'locations_with_subscription_product' => $this->subscription_helper->locations_with_subscription_product(),
'enforce_vault' => $this->has_subscriptions(),
'can_save_vault_token' => $this->can_save_vault_token(),
'is_free_trial_cart' => $is_free_trial_cart,

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,20 +81,23 @@ class CartScriptParamsEndpoint implements EndpointInterface {
$shop_country_code = $base_location['country'] ?? '';
$currency_code = get_woocommerce_currency();
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' ),
$response = 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,
)
'total' => $total,
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
'currency_code' => $currency_code,
'country_code' => $shop_country_code,
);
if ( $include_shipping ) {
$response = $this->append_shipping_data( $response, $currency_code );
}
wp_send_json_success( $response );
return true;
} catch ( Throwable $error ) {
$this->logger->error( "CartScriptParamsEndpoint execution failed. {$error->getMessage()} {$error->getFile()}:{$error->getLine()}" );
@ -101,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;
}
}