Sylius商店 – 如何在页眉弹出购物车中呈现产品图像?

huangapple go评论96阅读模式
英文:

Sylius shop - how to render product image in popup cart in header?

问题

在模板 themes/xxx/templates/bundles/SyliusShopBundle/Cart/Widget/_popup.html.twig 中呈现购物车中的商品。有变量 item.product、item.quantity。

是否有一种方法可以呈现产品图片,类似于 item.image?

我倾倒了变量 {{dump(item.product)}} 并看到了图片集合。

英文:

in template themes/xxx/templates/bundles/SyliusShopBundle/Cart/Widget/_popup.html.twig
are rendered items in shopping cart.
There are variables item.product, item.quantity

Is there some way how to render product image, something like item.image?

I dumped variable {{dump(item.product)}}
and see images collection

答案1

得分: 0

First you need to retrieve from the product one image you want to display you can do it e.g. by image.type in twig to filter the image by type you can use product.imagesByType('type') then you can use imagine_filter to retrieve the image path in appropriate resolutions e.g. product.imagesByType('thumbnail').first.path|imagine_filter(filter|default('sylius_shop_product_tiny_thumbnail')).

You can find all image types defined in sylius in https://docs.sylius.com/en/latest/cookbook/images/images.html

An example how to do it you will find in sylius code, for example here -> https://github.com/Sylius/Sylius/blob/f0ea6668bde5163d7e5a5b0f1ae28301ce6a9b14/src/Sylius/Bundle/ShopBundle/Resources/views/Product/_mainImage.html.twig

{% if product.imagesByType('thumbnail') is not empty %}
    {% set path = product.imagesByType('thumbnail').first.path|imagine_filter(filter|default('sylius_shop_product_thumbnail')) %}
{% elseif product.images.first %}
    {% set path = product.images.first.path|imagine_filter(filter|default('sylius_shop_product_thumbnail')) %}
{% else %}
    {% set path = asset('assets/shop/img/200x200.png') %}
{% endif %}

<img src="{{ path }}" {{ sylius_test_html_attribute('main-image') }} alt="{{ product.name }}" class="ui bordered image" />
英文:

First you need to retrieve from the product one image you want to display you can do it e.g. by image.type in twig to filter the image by type you can use product.imagesByType(&#39;type&#39;) then you can use imagine_filter to retrieve the image path in appropriate resolutions e.g. product.imagesByType(&#39;thumbnail&#39;).first.path|imagine_filter(filter|default(&#39;sylius_shop_product_tiny_thumbnail&#39;)).

You can find all image types defined in sylius in https://docs.sylius.com/en/latest/cookbook/images/images.html

An example how to do it you will find in sylius code, for example here -> https://github.com/Sylius/Sylius/blob/f0ea6668bde5163d7e5a5b0f1ae28301ce6a9b14/src/Sylius/Bundle/ShopBundle/Resources/views/Product/_mainImage.html.twig

{% if product.imagesByType(&#39;thumbnail&#39;) is not empty %}
    {% set path = product.imagesByType(&#39;thumbnail&#39;).first.path|imagine_filter(filter|default(&#39;sylius_shop_product_thumbnail&#39;)) %}
{% elseif product.images.first %}
    {% set path = product.images.first.path|imagine_filter(filter|default(&#39;sylius_shop_product_thumbnail&#39;)) %}
{% else %}
    {% set path = asset(&#39;assets/shop/img/200x200.png&#39;) %}
{% endif %}

&lt;img src=&quot;{{ path }}&quot; {{ sylius_test_html_attribute(&#39;main-image&#39;) }} alt=&quot;{{ product.name }}&quot; class=&quot;ui bordered image&quot; /&gt;

huangapple
  • 本文由 发表于 2023年8月5日 00:32:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定