Andrei | рефакторинг - заменил в карточке товара все вызовы функций на методы класса CosmopetProduct

web_10
parent f062cbf11f
commit a9f298cbc9
  1. 6
      wp-content/themes/cosmopet/modules/shop/CosmopetProduct.php
  2. 13
      wp-content/themes/cosmopet/modules/static-pages/new-front-page/template-new-front-page.php
  3. 4
      wp-content/themes/cosmopet/views/_blocks/shop/archive-product-ajaxload.twig
  4. 4
      wp-content/themes/cosmopet/views/_pages/new-front-page/main-food.twig
  5. 4
      wp-content/themes/cosmopet/views/_pages/new-front-page/main-treats.twig
  6. 2
      wp-content/themes/cosmopet/views/blocks/modules/shop/products-section.twig
  7. 0
      wp-content/themes/cosmopet/views/blocks/modules/shop/reviews-section.twig
  8. 84
      wp-content/themes/cosmopet/views/shop/product-card.twig
  9. 11
      wp-content/themes/cosmopet/views/woocommerce/archive-product.twig
  10. 56
      wp-content/themes/cosmopet/views/woocommerce/product-card.twig
  11. 15
      wp-content/themes/cosmopet/views/woocommerce/single-product.twig

@ -984,14 +984,12 @@ class CosmopetProduct extends TimberProduct {
}
return $attributes;
}
// TO_DO: переименовать в getPriceWithCurrency
public function getPrice(){
public function getPriceWithCurrency(){
$price = $this->product->get_price() . ' ' . get_woocommerce_currency_symbol();
return $price;
}
public function getRegularPrice(){
public function getRegularPriceWithCurrency(){
$regular_price = $this->product->get_regular_price() . ' ' . get_woocommerce_currency_symbol();
if ($regular_price != $this->getPrice()){
return $regular_price;

@ -115,9 +115,16 @@ if (function_exists('get_field')) {
$context['q_5'] = get_field('q_5', 383);
//
$context['food_list'] = get_field('featured_food');
$context['treats_list'] = get_field('featured_treats');
$array_food = array();
foreach (get_field('featured_food') as $item) {
$array_food[] = Timber::get_post($item['product']);
}
$array_treats = array();
foreach (get_field('featured_treats') as $item) {
$array_treats[] = Timber::get_post($item['product']);
}
$context['food_list'] = $array_food;
$context['treats_list'] = $array_treats;
}
try {

@ -1,8 +1,6 @@
{% for post in posts %}
{% set class = post.getTasteClass %}
{% set section = class ~ ' product-archive-card' %}
{% include 'shop/product-card.twig' with {_product : fn('wc_get_product', post.id), section: section} %}
{% include 'woocommerce/product-card.twig' with {item : post, addClass: 'product-archive-card'} %}
{% endfor %}

@ -2,8 +2,8 @@
<div class="new-container">
<h2 class="main-food_title">сухой гипоаллергенный корм</h2>
<div class="main-food_wrapper">
{% include 'shop/products-section_block.twig' with {product_list: food_list, section: 'food'} %}
{% include 'shop/reviews-section_block.twig' with { section: 'food', rev_list: reviews_food_query } only %}
{% include 'blocks/modules/shop/products-section.twig' with {product_list: food_list, section: 'food'} %}
{% include 'blocks/modules/shop/reviews-section.twig' with { section: 'food', rev_list: reviews_food_query } only %}
</div>
</div>
</div>

@ -13,8 +13,8 @@
<path d="M285.428 26.1179L285.37 26.0584C284.322 22.9684 282.721 20.6807 280.654 19.3139C278.588 17.9472 275.881 17.2341 272.591 17.2341C269.302 17.2341 266.595 17.6204 264.82 18.4226C263.044 19.2248 261.589 21.1263 260.57 24.1866C260.046 25.6722 259.696 27.5737 259.405 29.8615H258.357L259.988 18.1552H250.818V51.7292H259.988V38.5373C259.988 36.0712 260.337 33.9617 261.006 32.179C261.676 30.3963 262.753 29.0296 264.15 28.0788C265.547 27.1578 267.294 26.6527 269.36 26.6527C272.679 26.6527 274.949 27.5737 276.259 29.4455C277.511 31.3174 278.18 34.3777 278.18 38.567V51.7589H287V38.567C287 33.3972 286.476 29.2673 285.399 26.1179H285.428Z" fill="white" />
</svg></h2>
<div class="main-food_wrapper">
{% include 'shop/products-section_block.twig' with {product_list: treats_list, section: 'treats'} %}
{% include 'shop/reviews-section_block.twig' with { section: 'treats', rev_list: reviews_treats_query } only %}
{% include 'blocks/modules/shop/products-section.twig' with {product_list: treats_list} %}
{% include 'blocks/modules/shop/reviews-section.twig' with { section: 'treats', rev_list: reviews_treats_query } only %}
</div>
</div>
</div>

@ -1,6 +1,6 @@
<div class="main-food_products">
{% for product in product_list %}
{% include 'shop/product-card.twig' with {_product : fn('wc_get_product', product.product)} %}
{% include 'woocommerce/product-card.twig' with {item : product, addClass: ''} %}
{% endfor %}
{% if section == 'food' %}
<a href="/shop/" class="main-about_banner-btn"><span class="main-about_banner-btn-text">Вся продукция</span><span class="main-about_banner-btn-arrow"></span></a>

@ -1,84 +0,0 @@
{# TO_DO:
1) в контроллер вынести получение всех переменных, а не тут выхывать функции fn()
2) переименовать в файл /shop/product-card.twig
3) _product, wc_product, s_product -- не понятно почему множество переменных с одинаковым смыслом =>
надо сделать единый контенкст для всех контроллеров и там создать все нужные переменные
#}
{% set thumbnail = _product.get_image_id() ? function('wp_get_attachment_image_url', _product.get_image_id(), 'medium') : function('wc_placeholder_img_src') %}
{% set images = _product.get_gallery_image_ids() %}
{% set compound = fn('wc_get_product_terms', _product.get_id(), 'pa_compound') %}
<div class="main-food_products-card {{section}} {% for item in compound %}{% set term = get_term(item) %}{{ fn('map_attr_slugs_to_class', term.slug) }} {% endfor %}">
<a href="{{_product.get_permalink()}}" class="main-food_products-card-slider">
<div class="swiper-wrapper">
<div class="swiper-slide">
<div class="main-food_products-card-img">
<img src="{{ thumbnail }}" alt="{{_product.get_name()}}">
</div>
</div>
{% if images %}
{% for image in images %}
<div class="swiper-slide">
<div class="main-food_products-card-img">
<img src="{{ function('wp_get_attachment_image_url', image, 'medium') }}" alt="{{ _product.get_title() }}">
</div>
</div>
{% endfor %}
{% endif %}
<div class="swiper-dots"></div>
</div>
</a>
<div class="">
{% set collection = fn('wc_get_product_terms', _product.id, 'pa_collection') %}
{% set cur_weight = function('get_product_info', _product.id, 'weight') %}
<a href="{{_product.get_permalink()}}" class="main-food_products-card-title">
{{_product.get_name()}}
</a>
{% if collection %}
<div class="main-food_products-card__weight">
{% for option in collection %}
{% set term = get_term(option) %}
{% set siblings = function('get_collection_siblings' , term.id) %}
{% for sibling in siblings %}
{% set weight = function('get_product_info', sibling.ID, 'weight') %}
{% set s_product = TimberPost(sibling.ID) %}
{% set wc_product = fn('wc_get_product', sibling.ID) %}
{% set s_in_stock = s_product.meta('_stock_status') == 'instock' %}
{% set s_thumbnail = wc_product.get_image_id() ? function('wp_get_attachment_image_url', wc_product.get_image_id(), 'medium') : function('wc_placeholder_img_src') %}
{% set s_images = wc_product.get_gallery_image_ids() %}
{% set s_title = wc_product.get_title() %}
{% set all_images = [s_thumbnail]|merge(s_images|map(image => function('wp_get_attachment_image_url', image, 'medium'))) %}
{% if s_in_stock %}
{% set class = '' %}
{% if weight == cur_weight %}
{% set class = 'active' %}
{% endif %}
{% if weight %}
<button class="main-food_products-card__weight-item {{ class }}" data-image-set='{{ all_images|json_encode() }}' data-product_id="{{sibling.ID}}" data-product_price="{{ function('get_product_info', sibling.ID, 'price') }} {{ fn('get_woocommerce_currency_symbol') }}" data-title="{{s_title}}">
{{ weight }}
</button>
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
</div>{% endif %}
<div class="main-food_products-card-bottom">
<div class="main-food_products-card-bottom-price">{{ _product.get_price() }} {{ fn('get_woocommerce_currency_symbol') }}</div>
<a href="{{ _product.add_to_cart_url }}" class="main-food_products-cardbottom-btn ajax_add_to_cart add_to_cart_button" data-product_id="{{ _product.id }}"
data-product_sku="{{ _product.sku|e('html_attr') }}">{{ fn('pll_e', 'В корзину') }}</a>
</div>
</div>
</div>

@ -55,15 +55,8 @@
{% if posts|length > 0 %}
{% for post in posts %}
{% set class = post.getTasteClass %}
{% set section = class ~ ' product-archive-card' %}
<!-- TO_DO (избавиться от wc_get_product) -->
{% include 'shop/product-card.twig' with {_product : fn('wc_get_product', post.id), section: section} %}
{% include 'woocommerce/product-card.twig' with {item : post, addClass: 'product-archive-card'} %}
{% endfor %}
{% else %}
<div class="not-found">

@ -0,0 +1,56 @@
{# TO_DO:
1) в контроллер вынести получение всех переменных, а не тут выхывать функции fn()
2) переименовать в файл /shop/product-card.twig
3) _product, wc_product, s_product -- не понятно почему множество переменных с одинаковым смыслом =>
надо сделать единый контенкст для всех контроллеров и там создать все нужные переменные
#}
<div class="main-food_products-card {{addClass}} {{ item.getTasteClass() }}">
<a href="{{item.link}}" class="main-food_products-card-slider">
<div class="swiper-wrapper">
{% set image_list = item.getImageGallery('medium') %}
{% if image_list %}
{% for image in image_list %}
<div class="swiper-slide">
<div class="main-food_products-card-img">
<img src="{{ image.url }}" alt="{{ image.alt }}">
</div>
</div>
{% endfor %}
{% endif %}
<div class="swiper-dots"></div>
</div>
</a>
<div class="">
<a href="{{item.link}}" class="main-food_products-card-title">
{{item.title}}
</a>
{% set size_siblings = post.getSizeSiblings %}
{% if size_siblings %}
<div class="main-food_products-card__weight">
{% for sibling in size_siblings %}
{% set class = '' %}
{% if sibling.get_weight == item.get_weight %}
{% set class = 'active' %}
{% endif %}
{% if sibling.get_weight %}
<button class="main-food_products-card__weight-item {{ class }}" data-image-set='{{ sibling.getImageGallery('medium')|json_encode() }}' data-product_id="{{sibling.id}}" data-product_price="{{sibling.getPrice}}" data-title="{{sibling.title}}">
{{ sibling.get_weight }}
</button>
{% endif %}
{% endfor %}
</div>{% endif %}
<div class="main-food_products-card-bottom">
<div class="main-food_products-card-bottom-price">{{item.getPriceWithCurrency}}</div>
<a href="{{ item.product.add_to_cart_url }}" class="main-food_products-cardbottom-btn ajax_add_to_cart add_to_cart_button" data-product_id="{{ item.id }}"
data-product_sku="{{ item.sku|e('html_attr') }}">{{ fn('pll_e', 'В корзину') }}</a>
</div>
</div>
</div>

@ -229,11 +229,11 @@
<div class="product-price">
<div class="product-price-main">
{{post.getPrice}}
{{post.getPriceWithCurrency}}
</div>
{% if post.getRegularPrice %}
<div class="product-price-disc">
<div class="product-price-old">{{post.getRegularPrice}}</div>
<div class="product-price-old">{{post.getRegularPriceWithCurrency}}</div>
<div class="product-price-percent">
{{ post.getPriceDifferenceInPercent }}
</div>
@ -299,15 +299,8 @@
<div class="product-similar-slider wrapper">
<div class="swiper-wrapper">
{% for related_product in post.getRelatedProducts %}
{% set class = related_product.getTasteClass %}
{% set section = class ~ ' swiper-slide' %}
<!-- TO_DO (избавиться от wc_get_product) -->
{% include 'shop/product-card.twig' with {_product : fn('wc_get_product', related_product.id), section: section} %}
{% endfor %}
{% include 'woocommerce/product-card.twig' with {item : related_product, addClass: 'swiper-slide'} %}
{% endfor %}
</div>
</div>
<div class="product-block-arrows--m">

Loading…
Cancel
Save