181 lines
5 KiB
PHP
Executable file
181 lines
5 KiB
PHP
Executable file
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Le modèle de la Page Panier (« Cart »).
|
|
*/
|
|
|
|
use function Crell\fp\pipe;
|
|
use Illuminate\Support\Number;
|
|
|
|
use Timber\Timber;
|
|
|
|
// Importe la fonction pour récupérer les informations affichées des Produits dans le Panier
|
|
require_once __DIR__ . '/src/inc/TraitementInformations.php';
|
|
|
|
// Contexte et modèles
|
|
$contexte = Timber::context();
|
|
$modeles = ['panier.twig'];
|
|
|
|
$pays_acceptes = [
|
|
'AD',
|
|
'AL',
|
|
'AM',
|
|
'AR',
|
|
'AT',
|
|
'AU',
|
|
'BA',
|
|
'BE',
|
|
'BG',
|
|
'BR',
|
|
'CA',
|
|
'CH',
|
|
'CL',
|
|
'CR',
|
|
'CU',
|
|
'CY',
|
|
'CZ',
|
|
'DE',
|
|
'DK',
|
|
'DZ',
|
|
'EE',
|
|
'EG',
|
|
'ES',
|
|
'FI',
|
|
'FR',
|
|
'GF',
|
|
'GP',
|
|
'GR',
|
|
'HR',
|
|
'HU',
|
|
'IE',
|
|
'IS',
|
|
'IT',
|
|
'JP',
|
|
'KR',
|
|
'LB',
|
|
'LI',
|
|
'LT',
|
|
'LU',
|
|
'LV',
|
|
'MA',
|
|
'MD',
|
|
'ME',
|
|
'MF',
|
|
'MQ',
|
|
'MT',
|
|
'MX',
|
|
'NC',
|
|
'NL',
|
|
'NO',
|
|
'NZ',
|
|
'PF',
|
|
'PL',
|
|
'PM',
|
|
'PS',
|
|
'PT',
|
|
'RE',
|
|
'RO',
|
|
'SE',
|
|
'SI',
|
|
'SK',
|
|
'SM',
|
|
'TN',
|
|
'TR',
|
|
'TW',
|
|
'US',
|
|
'YT',
|
|
'ZA',
|
|
];
|
|
|
|
// Récupère les informations affichés des Produits du Panier
|
|
$panier = [];
|
|
|
|
/** @var float $sous_total_panier Le sous-total de la Commande dans le Panier. */
|
|
$sous_total_panier = WC()->cart->get_subtotal();
|
|
|
|
/** @var string|null $code_promo Le code promo appliqué au Panier s'il existe. */
|
|
$code_promo = collect(WC()->cart->get_applied_coupons())->first();
|
|
|
|
/** @var int $sous_total_reduction Le total du montant de la Réduction appliquée au Panier */
|
|
$sous_total_reduction = Number::format(WC()->cart->get_totals()['discount_total'], maxPrecision: 2);
|
|
|
|
/** @var float $total_panier Le total de la Commande dans le Panier. */
|
|
$total_panier = Number::format((float) WC()->cart->get_totals()['total'], maxPrecision: 2);
|
|
|
|
foreach (WC()->cart->get_cart() as $cle_panier => $article_panier) {
|
|
$panier[$cle_panier] = [
|
|
'attributs' => $article_panier['data']?->get_type() === 'variation'
|
|
? recupere_et_formate_attributs_produit($article_panier['data']?->get_attributes())
|
|
: [],
|
|
'cle' => $cle_panier,
|
|
'id_produit' => $article_panier['product_id'],
|
|
'id_variation' => $article_panier['variation_id'],
|
|
'image' => pipe($article_panier['data']?->get_image_id(), static fn($id): string => genere_balise_img_multiformats(
|
|
id: (string) $id,
|
|
lazy: true,
|
|
)),
|
|
'prix' => $article_panier['data']?->get_price(),
|
|
'quantite' => $article_panier['quantity'],
|
|
'url' => $article_panier['data']?->get_permalink(),
|
|
'titre' => $article_panier['data']?->get_title(),
|
|
];
|
|
}
|
|
|
|
// Récupère les Adresses de l'Utilisateur
|
|
$email = WC()->customer->get_billing_email();
|
|
$adresse_livraison = WC()->customer->get_shipping();
|
|
$adresse_facturation = WC()->customer->get_billing();
|
|
$adresse_renseignee = $adresse_livraison['city'] !== '';
|
|
$pays_livraison = collect(WC()->countries->get_countries())->only($pays_acceptes)->toArray();
|
|
$total_livraison = Number::format((float) WC()->cart->get_totals()['shipping_total'], precision: 0);
|
|
$methodes_livraison = collect(WC()->session->get('shipping_for_package_0')['rates'])
|
|
->values()
|
|
->map(static fn(WC_Shipping_Rate $methode): array => [
|
|
'id' => $methode->get_method_id(),
|
|
'prix' => Number::format((int) $methode->get_cost(), maxPrecision: 2),
|
|
'selectionnee' => collect(WC()->session->get('chosen_shipping_methods'))->first() === $methode->get_id(),
|
|
'titre' => $methode->get_label(),
|
|
]);
|
|
|
|
$contexte['email'] = $email;
|
|
$contexte['adresse_livraison'] = $adresse_livraison;
|
|
$contexte['adresse_facturation'] = $adresse_facturation;
|
|
$contexte['adresse_renseignee'] = $adresse_renseignee;
|
|
$contexte['sous_total_panier'] = $sous_total_panier;
|
|
$contexte['code_promo'] = $code_promo;
|
|
$contexte['sous_total_reduction'] = $sous_total_reduction;
|
|
$contexte['total_panier'] = $total_panier;
|
|
$contexte['produits_panier'] = $panier;
|
|
$contexte['pays_livraison'] = $pays_livraison;
|
|
$contexte['sous_total_livraison'] = $total_livraison;
|
|
$contexte['methodes_livraison'] = $methodes_livraison;
|
|
|
|
// Charge les scripts et styles de la page
|
|
function charge_scripts_styles_page_panier(): void {
|
|
wp_enqueue_style(
|
|
handle: 'haiku-atelier-2024-styles-page-panier',
|
|
src: get_template_directory_uri() . '/assets/css/pages/page-panier.css',
|
|
deps: [],
|
|
ver: filemtime(get_template_directory() . '/assets/css/pages/page-panier.css'),
|
|
media: 'all',
|
|
);
|
|
wp_enqueue_script_module(
|
|
id: 'haiku-atelier-2024-scripts-page-panier',
|
|
src: get_template_directory_uri() . '/assets/js/scripts-page-panier.js',
|
|
deps: [],
|
|
version: filemtime(get_template_directory() . '/assets/js/scripts-page-panier.js'),
|
|
);
|
|
}
|
|
|
|
add_action('wp_enqueue_scripts', 'charge_scripts_styles_page_panier');
|
|
|
|
// $lal = wp_json_encode($contexte);
|
|
// echo "<script>console.debug({$lal});</script>";
|
|
|
|
// Rendu
|
|
Timber::render(
|
|
filenames: $modeles,
|
|
data: $contexte,
|
|
);
|