From 2a978b3197f3046e80f85a6a573f469bf31a5205 Mon Sep 17 00:00:00 2001 From: GP_DV Date: Tue, 15 Apr 2025 21:55:53 +0300 Subject: [PATCH] Feat: template for WC product category and term archives --- wp-content/themes/cosmopet/composer.json | 8 +- wp-content/themes/cosmopet/composer.lock | 59 +++++- wp-content/themes/cosmopet/functions.php | 175 +++++++++++++++++- .../layout/assets/css/gp-style-core.css | 19 ++ .../layout/assets/css/gp-style-full.css | 9 +- .../layout/assets/css/gp-style-normalize.css | 6 - .../themes/cosmopet/templates/footer.twig | 2 +- .../templates/front-page/front-page.twig | 1 + .../themes/cosmopet/templates/header.twig | 4 +- .../themes/cosmopet/templates/layout.twig | 4 +- .../cosmopet/woocommerce/archive-product.php | 100 ++++++---- .../archive-product/archive-product.twig | 70 ++++++- 12 files changed, 397 insertions(+), 60 deletions(-) diff --git a/wp-content/themes/cosmopet/composer.json b/wp-content/themes/cosmopet/composer.json index f8f8769..386905d 100644 --- a/wp-content/themes/cosmopet/composer.json +++ b/wp-content/themes/cosmopet/composer.json @@ -1,5 +1,11 @@ { "require": { - "timber/timber": "^2.1" + "timber/timber": "^2.1", + "mindkomm/timber-integration-woocommerce": "^1.0" + }, + "config": { + "allow-plugins": { + "composer/installers": true + } } } diff --git a/wp-content/themes/cosmopet/composer.lock b/wp-content/themes/cosmopet/composer.lock index 00f78e9..a51eb8d 100644 --- a/wp-content/themes/cosmopet/composer.lock +++ b/wp-content/themes/cosmopet/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6da1f9205429be4e1609181ef22acad2", + "content-hash": "361ab752c076e0fc5ea94a3756fab8a1", "packages": [ { "name": "composer/installers", @@ -152,6 +152,55 @@ ], "time": "2024-06-24T20:46:46+00:00" }, + { + "name": "mindkomm/timber-integration-woocommerce", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/mindkomm/timber-integration-woocommerce.git", + "reference": "bddcccba2fe7d58d53e81fc8f5d293fdc6e25b57" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mindkomm/timber-integration-woocommerce/zipball/bddcccba2fe7d58d53e81fc8f5d293fdc6e25b57", + "reference": "bddcccba2fe7d58d53e81fc8f5d293fdc6e25b57", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0", + "timber/timber": "^2.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Timber\\Integrations\\WooCommerce\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Lukas Gächter", + "email": "lukas.gaechter@mind.ch", + "homepage": "https://www.mind.ch" + } + ], + "description": "WooCommerce integration for Timber.", + "keywords": [ + "integration", + "timber", + "twig", + "woocommerce" + ], + "support": { + "docs": "https://github.com/mindkomm/timber-integration-woocommerce#documentation", + "issues": "https://github.com/mindkomm/timber-integration-woocommerce/issues", + "source": "https://github.com/mindkomm/timber-integration-woocommerce" + }, + "time": "2024-05-08T09:54:00+00:00" + }, { "name": "symfony/deprecation-contracts", "version": "v3.0.2", @@ -716,10 +765,10 @@ "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": false, "prefer-lowest": false, - "platform": [], - "platform-dev": [], - "plugin-api-version": "2.1.0" + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.6.0" } diff --git a/wp-content/themes/cosmopet/functions.php b/wp-content/themes/cosmopet/functions.php index e38e470..416a96e 100644 --- a/wp-content/themes/cosmopet/functions.php +++ b/wp-content/themes/cosmopet/functions.php @@ -1,6 +1,6 @@ wc_product = wc_get_product($this->ID); + } + + public function price() { + return $this->wc_product->get_price(); + } + + public function get_price_html() { + return $this->wc_product->get_price_html(); + } + + public function get_attr() { + return $this->wc_product->get_attribute('pa_compound'); + } + + public function get_test() { + return 'test'; + } + +}; + +add_filter('timber/post/classmap', function ($classmap) { + $custom_classmap = [ + 'product' => WooProduct::class, + ]; + return array_merge($classmap, $custom_classmap); +}); + +//Ajax подгрузка товаров в архиве +add_action( 'wp_ajax_nopriv_get_products', 'get_products' ); +add_action( 'wp_ajax_get_products', 'get_products' ); + +function get_products() { + global $post; + + if (function_exists('WC')) { + WC(); + } + + // Потом Timber + + $context = Timber::context(); + $context['get_page'] = empty($_POST['get_page']) ? 1 : $_POST['get_page']; + $context['criteria_for_new_product'] = date('Y-m-d', strtotime('-30 days')); + $context['get_category'] = isset($_POST['get_category']) ? $_POST['get_category'] : NULL; + $context['get_category_type'] = isset($_POST['get_category_type']) ? $_POST['get_category_type'] : NULL; + + // Задаем количество постов для подзагрузки Ajax + $posts_per_page = 1; + + $args = array( + 'post_type' => 'product', + 'post_status' => 'publish', + 'posts_per_page' => $posts_per_page, + 'paged' => $context['get_page'], + 'has_password' => FALSE + ); + + $count_args = array( + 'post_type' => 'product', + 'post_status' => 'publish', + 'posts_per_page' => -1, + 'has_password' => FALSE + ); + + if ($context['get_category'] != NULL) { + $categories = [ + 'tax_query' => array( + array( + 'taxonomy' => $context['get_category_type'], + 'field' => 'term_id', + 'terms' => array($context['get_category']), + 'operator' => 'IN' + ), + array( + 'taxonomy' => 'product_visibility', + 'field' => 'slug', + 'terms' => 'exclude-from-catalog', + 'operator' => 'NOT IN' + ) + ) + ]; + $args = array_merge($args, $categories); + $count_args = array_merge($count_args, $categories); + } + + $products = new WP_Query($args); + $products = new Timber\PostQuery($products, 'Timber\Integrations\WooCommerce\Product'); + + $context['posts'] = $products; + + $context['count'] = count(Timber::get_posts($count_args)); + + if ($context['count'] <= $context['get_page'] * 1) $context['ended'] = true; + + Timber::render( 'woocommerce/archive-product/archive-product-ajaxload.twig', $context ); + + die(); +} + + + + + + +function get_product_info ($id, $type) { + $product = wc_get_product( $id ); + if ($type == 'price') { + return $product->get_price(); + } elseif ($type == 'weight') { + return $product->get_weight() . ' кг'; + } +} + +function get_add_to_cart_button ($id) { + $product = wc_get_product( $id ); + return ''. pll__('Добавить в корзину') .''; +} + +function get_collection_siblings ($term) { + $args = array( + 'posts_per_page' => -1, + 'post_type' => 'product', + 'order' => 'ASC', + 'order_by' => 'name', + 'tax_query' => [ + [ + 'taxonomy' => 'pa_collection', + 'terms' => $term, + 'field' => 'id', + ] + ], + ); + + $siblings = get_posts($args); + + $sibl_arr = []; + + foreach( $siblings as $sibling ) { + $sibl_arr [] = $sibling; + } + + return $sibl_arr; +} \ No newline at end of file diff --git a/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-core.css b/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-core.css index 5767d4d..8cb67ba 100644 --- a/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-core.css +++ b/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-core.css @@ -56,6 +56,20 @@ /* Fonts */ --font-craftwork: 'Craftwork Grotest', sans-serif; --font-abel: 'Abel', serif; + + --text-dark: #2b2b3b; + --text-red: #fa0505; + --text-green: #2ED15D; + --text-0: #000; + --background-white: #fff; + --background-black: #121212; + --background-grey: #f2f2f2; + --background-green: #2ED15D; + --background-green-white: #f4fff0; + --background-9: #999; + --gradient-blue: radial-gradient(346.57% 244.17% at 149.73% -58.39%, rgb(15, 88, 129) 0%, rgb(30, 164, 156) 51.21689438819885%, rgb(118, 206, 117) 80.70731163024902%, rgb(236, 243, 159) 91.14583134651184%); + --gradient-turquoise: radial-gradient(346.57% 244.17% at 149.73% -58.39%, rgb(117, 196, 240) 0%, rgb(126, 231, 225) 51.21689438819885%, rgb(181, 228, 180) 80.70731163024902%, rgb(237, 244, 164) 91.14583134651184%); + --gradient-red: linear-gradient(22deg, #f44242 0%, #569ef0 100%); } body { @@ -189,4 +203,9 @@ textarea{ height: 29px; padding: 7px; } +} + +.wrapper { + margin: 0 auto; + max-width: 1600px; } \ No newline at end of file diff --git a/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-full.css b/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-full.css index c8dddf3..e04ef1d 100644 --- a/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-full.css +++ b/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-full.css @@ -74,11 +74,6 @@ address { font-style: normal; } -.wrapper { - width: 100%; - overflow: hidden; - min-height: 100svh; -} ul, ol, @@ -144,6 +139,10 @@ body { font-weight: 500; } +body.bg-white { + background: var(--main_white); +} + .container { max-width: 1232px; margin: 0 auto; diff --git a/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-normalize.css b/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-normalize.css index ce8476b..7274e0c 100644 --- a/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-normalize.css +++ b/wp-content/themes/cosmopet/modules/layout/assets/css/gp-style-normalize.css @@ -44,12 +44,6 @@ address { font-style: normal; } -.wrapper { - width: 100%; - overflow: hidden; - min-height: 100svh; -} - ul, ol, dl { diff --git a/wp-content/themes/cosmopet/templates/footer.twig b/wp-content/themes/cosmopet/templates/footer.twig index e1895a3..d7c908e 100644 --- a/wp-content/themes/cosmopet/templates/footer.twig +++ b/wp-content/themes/cosmopet/templates/footer.twig @@ -1,6 +1,6 @@ {% set current_path = template_path ~ '/modules/footer' %}