88 lines
2.6 KiB
PHP
Executable file
88 lines
2.6 KiB
PHP
Executable file
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Le modèle de la Page d'un Produit.
|
|
*/
|
|
|
|
namespace HaikuAtelier;
|
|
|
|
use Exception;
|
|
use HaikuAtelier\Data\Product;
|
|
use HaikuAtelier\WP\Resource;
|
|
use HaikuAtelier\WP\Term;
|
|
use Illuminate\Support\Arr;
|
|
use Psl\Option\Option;
|
|
use stdClass;
|
|
use Timber\Timber;
|
|
use WC_Product;
|
|
|
|
use function add_action;
|
|
use function assert;
|
|
use function collect;
|
|
use function is_array;
|
|
use function wc_get_product;
|
|
use function wp_json_encode;
|
|
|
|
$context = Timber::context();
|
|
$templates = ['produit.twig'];
|
|
|
|
$raw_product = wc_get_product();
|
|
|
|
// Le Produit DOIT exister.
|
|
if ($raw_product === null || $raw_product === false) {
|
|
throw new Exception("Le Produit n'existe pas.");
|
|
}
|
|
|
|
// Assemble les données d'intérêt pour la page au sein d'une Classe.
|
|
$product = Product::from_wc_product($raw_product);
|
|
|
|
/** @var int $maximum_price Le prix de la Variation la plus chère */
|
|
$maximum_price = collect($product->variations)->max('price');
|
|
|
|
/** @var list<Product> Les Produits de la même collection que celui affiché dans la Page. */
|
|
$same_collection_products = Product::get_same_collection_products($product->collection)($product->id)
|
|
|> function (/** @var list<WC_Product>|stdClass */ mixed $products): array {
|
|
assert(is_array($products), 'Les Produits de la même collection doivent être un tableau.');
|
|
|
|
return $products;
|
|
}
|
|
|> (static fn(/** @var list<WC_Product> */ array $products): array => Arr::map(
|
|
$products,
|
|
Product::from_wc_product(...),
|
|
));
|
|
|
|
$context['product'] = $product;
|
|
$context['product_json'] = wp_json_encode($product);
|
|
$context['maximum_price'] = $maximum_price;
|
|
$context['same_collection_products'] = $same_collection_products;
|
|
$product_tags = $raw_product->get_tag_ids()
|
|
|> (static fn($tags_ids) => Arr::map($tags_ids, static fn($id) => Term::get_term_by_id(
|
|
id: $id,
|
|
taxonomy: 'product_tag',
|
|
)))
|
|
|> (static fn(/** @var list<Option<WC_Term>> */ $tags) => Arr::reject($tags, static fn($tag) => $tag->isNone()))
|
|
|> (static fn(/** @var list<Option<WC_Term>> */ $tags) => Arr::map($tags, static fn($tag) => $tag->unwrap()));
|
|
$tags = get_terms(['taxonomy' => 'product_tag', 'hide_empty' => true]);
|
|
|
|
// echo '<pre>';
|
|
// print_r($product_tags);
|
|
// print_r($tags);
|
|
// echo '</pre>';
|
|
|
|
// exit();
|
|
|
|
add_action('wp_enqueue_scripts', function (): void {
|
|
Resource::enqueue_script_module_file(
|
|
id: 'haiku-atelier-2024-scripts-page-produit',
|
|
path: '/assets/js/scripts-page-produit.js',
|
|
);
|
|
Resource::enqueue_script_module_file(
|
|
id: 'haiku-atelier-2024-scripts-menu-categories',
|
|
path: '/assets/js/scripts-menu-categories.js',
|
|
);
|
|
});
|
|
|
|
// Rendu
|
|
Timber::render(filenames: $templates, data: $context);
|