fonc: travail en cours sur scripts page Produit
This commit is contained in:
parent
08ad871e0c
commit
5e97b6dcbc
30 changed files with 1112 additions and 996 deletions
|
|
@ -22,22 +22,20 @@ use WC_Session_Handler;
|
|||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// TODO: Appliquer le bon calcul pour les montants vs. percentages
|
||||
function get_discount_amount(WC_Coupon $coupon)
|
||||
{
|
||||
if ($coupon->get_discount_type() === 'fixed_cart') {
|
||||
return $coupon->get_amount() * 100;
|
||||
} else {
|
||||
return $coupon->get_amount();
|
||||
}
|
||||
function get_discount_amount(WC_Coupon $coupon) {
|
||||
if ($coupon->get_discount_type() === 'fixed_cart') {
|
||||
return $coupon->get_amount() * 100;
|
||||
} else {
|
||||
return $coupon->get_amount();
|
||||
}
|
||||
}
|
||||
|
||||
function get_discount_duration(WC_Coupon $coupon): string
|
||||
{
|
||||
if ($coupon->get_discount_type() === 'fixed_cart') {
|
||||
return 'once';
|
||||
} else {
|
||||
return 'forever';
|
||||
}
|
||||
function get_discount_duration(WC_Coupon $coupon): string {
|
||||
if ($coupon->get_discount_type() === 'fixed_cart') {
|
||||
return 'once';
|
||||
} else {
|
||||
return 'forever';
|
||||
}
|
||||
}
|
||||
|
||||
// Récupère les informations nécessaires
|
||||
|
|
@ -46,37 +44,37 @@ $session_wc = WC()->session;
|
|||
|
||||
/** @var array<string,string> $urls URLs utilisables pour rediriger l'Utilisateur. */
|
||||
$urls = [
|
||||
'accueil' => get_page_link(get_page_by_path('home')),
|
||||
'succes_commande' => get_page_link(get_page_by_path('successful-order')),
|
||||
'echec_commande' => get_page_link(get_page_by_path('failed-order')),
|
||||
'accueil' => get_page_link(get_page_by_path('home')),
|
||||
'succes_commande' => get_page_link(get_page_by_path('successful-order')),
|
||||
'echec_commande' => get_page_link(get_page_by_path('failed-order')),
|
||||
];
|
||||
|
||||
// Redirige à la page d'accueil si le Panier est vide
|
||||
if (WC()->cart->is_empty()) {
|
||||
header('Location: ' . $urls['accueil']);
|
||||
header('Location: ' . $urls['accueil']);
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérifie que les paramètres d'URLs nécessaires soient présents
|
||||
/** @var string $order_id */
|
||||
$order_id = $_GET['order_id'];
|
||||
if (!$order_id) {
|
||||
$reponse = ['succes' => false, 'status' => 'order_key is missing'];
|
||||
echo json_encode($reponse);
|
||||
http_response_code(400);
|
||||
$reponse = ['succes' => false, 'status' => 'order_key is missing'];
|
||||
echo json_encode($reponse);
|
||||
http_response_code(400);
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var string $order_key */
|
||||
$order_key = $_GET['order_key'];
|
||||
if (!$order_key) {
|
||||
$reponse = ['succes' => false, 'status' => 'order_key is missing'];
|
||||
echo json_encode($reponse);
|
||||
http_response_code(400);
|
||||
$reponse = ['succes' => false, 'status' => 'order_key is missing'];
|
||||
echo json_encode($reponse);
|
||||
http_response_code(400);
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
// Récupère le Panier et l'Email du Client
|
||||
|
|
@ -88,29 +86,29 @@ $email_client = WC()->session->get('customer')['email'];
|
|||
|
||||
/** @var list<Product> $articles */
|
||||
$articles = collect($panier->get_cart())
|
||||
->map(static function ($article_panier) {
|
||||
$titre_produit = match ('variable' === $article_panier['data']?->get_type()) {
|
||||
true => $article_panier['data']?->get_title()
|
||||
. ' ('
|
||||
. explode(': ', (string) $article_panier['data']?->get_attribute_summary())[1]
|
||||
. ')',
|
||||
false => $article_panier['data']?->get_title(),
|
||||
};
|
||||
->map(static function ($article_panier) {
|
||||
$titre_produit = match ('variable' === $article_panier['data']?->get_type()) {
|
||||
true => $article_panier['data']?->get_title()
|
||||
. ' ('
|
||||
. explode(': ', (string) $article_panier['data']?->get_attribute_summary())[1]
|
||||
. ')',
|
||||
false => $article_panier['data']?->get_title(),
|
||||
};
|
||||
|
||||
return [
|
||||
'price_data' => [
|
||||
'currency' => 'EUR',
|
||||
'product_data' => [
|
||||
'name' => $titre_produit,
|
||||
'images' => [wp_get_attachment_image_url($article_panier['data']?->get_image_id())],
|
||||
],
|
||||
'unit_amount' => $article_panier['data']?->get_price() * 100,
|
||||
],
|
||||
'quantity' => $article_panier['quantity'],
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
return [
|
||||
'price_data' => [
|
||||
'currency' => 'EUR',
|
||||
'product_data' => [
|
||||
'name' => $titre_produit,
|
||||
'images' => [wp_get_attachment_image_url($article_panier['data']?->get_image_id())],
|
||||
],
|
||||
'unit_amount' => $article_panier['data']?->get_price() * 100,
|
||||
],
|
||||
'quantity' => $article_panier['quantity'],
|
||||
];
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
// Récupère la Commande et la Méthode de Livraison
|
||||
/** @var WC_Order $commande */
|
||||
|
|
@ -120,7 +118,7 @@ $methode_livraison = ['nom' => $commande->get_shipping_method(), 'cout' => $comm
|
|||
|
||||
// Le nom de la méthode de livraison ne peut être une chaîne vide.
|
||||
if (empty($methode_livraison['nom'])) {
|
||||
$methode_livraison['nom'] = 'Free';
|
||||
$methode_livraison['nom'] = 'Free';
|
||||
}
|
||||
|
||||
// Sélectionne la clé API Stripe
|
||||
|
|
@ -129,39 +127,39 @@ Stripe::setApiKey(Config::get('STRIPE_API_SECRET'));
|
|||
// Met à jour les Codes promos
|
||||
$coupons_stripe = collect(Coupon::all()->data);
|
||||
$coupons_wc = collect(WC()->cart->get_coupons())
|
||||
->map(static fn(WC_Coupon $coupon): array => [
|
||||
'currency' => 'EUR',
|
||||
'duration' => get_discount_duration($coupon),
|
||||
'fixed_cart' === $coupon->get_discount_type() ? 'amount_off' : 'percent_off' => get_discount_amount($coupon),
|
||||
'id' => $coupon->get_code(),
|
||||
'name' => $coupon->get_code(),
|
||||
])
|
||||
->each(static function (array $item) use ($coupons_stripe): void {
|
||||
// Si le code promo n'existe pas, le créer
|
||||
if (!$coupons_stripe->contains('name', $item['name'])) {
|
||||
Coupon::create($item);
|
||||
}
|
||||
});
|
||||
->map(static fn(WC_Coupon $coupon): array => [
|
||||
'currency' => 'EUR',
|
||||
'duration' => get_discount_duration($coupon),
|
||||
'fixed_cart' === $coupon->get_discount_type() ? 'amount_off' : 'percent_off' => get_discount_amount($coupon),
|
||||
'id' => $coupon->get_code(),
|
||||
'name' => $coupon->get_code(),
|
||||
])
|
||||
->each(static function (array $item) use ($coupons_stripe): void {
|
||||
// Si le code promo n'existe pas, le créer
|
||||
if (!$coupons_stripe->contains('name', $item['name'])) {
|
||||
Coupon::create($item);
|
||||
}
|
||||
});
|
||||
$reductions_stripe = $coupons_wc
|
||||
->map(static fn($coupon): array => ['coupon' => $coupon['name']])
|
||||
->values()
|
||||
->toArray();
|
||||
->map(static fn($coupon): array => ['coupon' => $coupon['name']])
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
/** @var Session $session_checkout_stripe */
|
||||
$session_checkout_stripe = Session::create([
|
||||
'cancel_url' => $urls['echec_commande'],
|
||||
'customer_email' => $email_client,
|
||||
'discounts' => $reductions_stripe,
|
||||
'line_items' => $articles,
|
||||
'mode' => 'payment',
|
||||
'success_url' => $urls['succes_commande'] . '?session_id={CHECKOUT_SESSION_ID}',
|
||||
'metadata' => ['order_id' => $order_id, 'order_key' => $order_key],
|
||||
'shipping_options' => [['shipping_rate_data' => [
|
||||
'display_name' => $methode_livraison['nom'],
|
||||
'fixed_amount' => ['amount' => $methode_livraison['cout'], 'currency' => 'EUR'],
|
||||
'tax_behavior' => 'inclusive',
|
||||
'type' => 'fixed_amount',
|
||||
]]],
|
||||
'cancel_url' => $urls['echec_commande'],
|
||||
'customer_email' => $email_client,
|
||||
'discounts' => $reductions_stripe,
|
||||
'line_items' => $articles,
|
||||
'mode' => 'payment',
|
||||
'success_url' => $urls['succes_commande'] . '?session_id={CHECKOUT_SESSION_ID}',
|
||||
'metadata' => ['order_id' => $order_id, 'order_key' => $order_key],
|
||||
'shipping_options' => [['shipping_rate_data' => [
|
||||
'display_name' => $methode_livraison['nom'],
|
||||
'fixed_amount' => ['amount' => $methode_livraison['cout'], 'currency' => 'EUR'],
|
||||
'tax_behavior' => 'inclusive',
|
||||
'type' => 'fixed_amount',
|
||||
]]],
|
||||
], ['idempotency_key' => Uuid::v4()]);
|
||||
// echo json_encode($session_checkout_stripe);
|
||||
header('HTTP/1.1 303 See Other');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue