简体   繁体   English

Shopify产品页面以隐藏变体图像

[英]Shopify Product Page to hide variant images

I am sort of new with Shopify and I´ve been playing around with it for the ast week or so without much success when trying to solve my issue. 我对Shopify有点陌生,并且在过去一周内一直在尝试使用它,而在尝试解决我的问题时没有取得太大的成功。

I am currently using the Brooklyn template and the way they represent the Product Page doesn´t precisely give the best user experience. 我目前正在使用布鲁克林模板,并且它们表示产品页面的方式不能精确地提供最佳的用户体验。 The way it´s representing the images by default is by having all the product variants images showed to the customer all the time even if he hasn´t actually picket that variant (ie a specific colour for the product). 默认情况下,表示图像的方式是通过将所有产品变型图像始终显示给客户,即使客户实际上没有挑剔该变型(即产品的特定颜色)。 It also displays all the product images in a vertical way which makes navigation for the user quite frustrating if you have more than a couple of pictures per product variant. 它还会垂直显示所有产品图像,如果每个产品变型中有多张图片,则使用户的导航非常麻烦。

I´ve found online a tutorial ( http://littlesnippets.ca/blogs/tutorials/15665261-grouping-images-with-variants )that addresses the part of my issue to hide the pictures of the variants that are not selected by the customer, and show them once the user click on it.(you can see an example of what I want here http://group-variant-images.myshopify.com/collections/frontpage/products/anson-chair ). 我在网上找到了一个教程( http://littlesnippets.ca/blogs/tutorials/15665261-grouping-images-with-variants ),该教程解决了我的问题部分,即隐藏了未被选择的变体的图片。客户,并在用户点击后向他们展示。(您可以在此处查看我想要的示例http://group-variant-images.myshopify.com/collections/frontpage/products/anson-chair )。 This is more or less how it looks my site now ( https://themes.shopify.com/themes/brooklyn/styles/brooklyn/preview ). 这或多或少是现在我的网站的外观( https://themes.shopify.com/themes/brooklyn/styles/brooklyn/preview )。

The problem is that that tutorial is being applied to a website that is not using the exact theme/functionality that Ii do, although it´s similar. 问题在于,该教程正在应用到一个网站,尽管它与Ii的主题/功能并不完全相同。 I´ve failed to apply those changes to my theme, can anybody give me a hand? 我未能将这些更改应用于我的主题,有人可以帮我吗?

This is my product.liquid piece of code at the moment: 这是我目前的product.liquid代码片段:

<!-- /templates/product.liquid -->

<div itemscope itemtype="http://schema.org/Product">

<meta itemprop="url" content="{{ shop.url }}{{ product.url }}">
<meta itemprop="image" content="{{ product.featured_image.src | img_url: 'grande' }}">

{% assign current_variant = product.selected_or_first_available_variant %}

<div class="grid product-single">
<div class="grid__item large--seven-twelfths medium--seven-twelfths text-center">
  <div class="product-single__photos">
    {% assign featured_image = current_variant.featured_image | default: product.featured_image %}

    {% comment %}
      Display current variant image, or default first
    {% endcomment %}
    <div class="product-single__photo-wrapper">
      <img class="product-single__photo" id="ProductPhotoImg" src="{{ featured_image | img_url: 'grande' }}" {% if settings.product_zoom_enable %}data-mfp-src="{{ featured_image | img_url: '1024x1024' }}"{% endif %} alt="{{ featured_image.alt | escape }}" data-image-id="{{ featured_image.id }}">
    </div>

    {% comment %}
      Display rest of product images, not repeating the featured one
    {% endcomment %}
    {% for image in product.images %}
      {% unless image contains featured_image %}
        <div class="product-single__photo-wrapper">
          <img class="product-single__photo" src="{{ image.src | img_url: 'grande' }}" {% if settings.product_zoom_enable %}data-mfp-src="{{ image.src | img_url: '1024x1024' }}"{% endif %} alt="{{ image.alt | escape }}" data-image-id="{{ image.id }}">
        </div>
      {% endunless %}
    {% endfor %}
  </div>
</div>

<div class="grid__item product-single__meta--wrapper medium--five-twelfths large--five-twelfths">
  <div class="product-single__meta">
    {% if settings.product_vendor_enable %}
      <h2 class="product-single__vendor" itemprop="brand">{{ product.vendor }}</h2>
    {% endif %}

    <h1 class="product-single__title" itemprop="name">{{ product.title }}</h1>

    <div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
      {% comment %}
        Optionally show the 'compare at' or original price of the product.
      {% endcomment %}

      {% if product.compare_at_price_max > product.price %}
        <span class="product-single__price--wrapper">
          <span class="visually-hidden">{{ 'products.general.regular_price' | t }}</span>
          <span id="ComparePrice" class="product-single__price--compare-at">
            {% if current_variant.compare_at_price > current_variant.price %}
              {{ current_variant.compare_at_price | money }}
            {% endif %}
          </span>
          <span class="visually-hidden">{{ 'products.general.sale_price' | t }}</span>
        </span>
      {% endif %}

      <span id="ProductPrice" class="product-single__price{% if product.compare_at_price > product.price %} on-sale{% endif %}" itemprop="price">
        {{ current_variant.price | money }}
      </span>

      <hr class="hr--small">

      <meta itemprop="priceCurrency" content="{{ shop.currency }}">
      <link itemprop="availability" href="http://schema.org/{% if product.available %}InStock{% else %}OutOfStock{% endif %}">

      <form action="/cart/add" method="post" enctype="multipart/form-data" class="product-single__form" id="AddToCartForm">
        <select name="id" id="ProductSelect" class="product-single__variants">
          {% for variant in product.variants %}
            {% if variant.available %}
              <option {% if variant == product.selected_or_first_available_variant %} selected="selected" {% endif %} data-sku="{{ variant.sku }}" value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money_with_currency }}</option>
            {% else %}
              <option disabled="disabled">
                {{ variant.title }} - {{ 'products.product.sold_out' | t }}
              </option>
            {% endif %}
          {% endfor %}
        </select>

        {% comment %}
        <div class="product-single__quantity">
          <label for="Quantity" class="product-single__quantity-label js-quantity-selector">{{ 'products.product.quantity' | t }}</label>
          <input type="number" hidden="hidden" id="Quantity" name="quantity" value="1" min="1" class="js-quantity-selector">
        </div>
        {% endcomment %}

        <div class="product-single__add-to-cart">
          <button type="submit" name="add" id="AddToCart" class="btn">
            <span id="AddToCartText">{{ 'products.product.add_to_cart' | t }}</span>
          </button>
        </div>
      </form>

    </div>

    <div class="product-single__description rte" itemprop="description">
      {{ product.description }}
    </div>

    {% if settings.social_sharing_products %}
      {% include 'social-sharing' %}
    {% endif %}
  </div>
</div>
</div>
{% if collection %}
<hr class="hr--clear">
<div class="text-center">
  <a href="{{ collection.url }}" class="return-link">&larr; {{ 'products.general.collection_return' | t: collection: collection.title }}</a>
</div>
{% endif %}

</div>

{% comment %}

*IMPORTANT:*
This theme uses a customized version of `option_selection.js` to support    using radio inputs for color and size variants. The custom version is in `variant_selection.js`.

If you wish to enable the default dropdowns for size and color
you can change the liquid asset tag below from:

{{ 'variant_selection.js' | asset_url | script_tag }}

to

{{ 'option_selection.js' | shopify_asset_url | script_tag }}

 If you use the default `option_selection.js` the labels for the dropdowns will  appear outside the dropdown.

You will also need to change `.radio-wrapper` to `.selector-wrapper` below.

{% endcomment %}
{{ 'variant_selection.js' | asset_url | script_tag }}
<script>
var selectCallback = function(variant, selector) {
timber.productPage({
  money_format: "{{ shop.money_format }}",
  variant: variant,
  selector: selector,
  translations: {
    addToCart : {{ 'products.product.add_to_cart' | t | json }},
    soldOut : {{ 'products.product.sold_out' | t | json }},
    unavailable : {{ 'products.product.unavailable' | t | json }}
  }
});
};

jQuery(function($) {
new Shopify.OptionSelectors('ProductSelect', {
  product: {{ product | json }},
  onVariantSelected: selectCallback,
  enableHistoryState: true
});

// Add label if only one product option and it isn't 'Title'. Could be 'Size'.
{% if product.options.size == 1 and product.options.first != 'Title' %}
  $('.radio-wrapper:eq(0)').prepend('<label for="ProductSelect-option-0"    class="single-option-radio__label">{{ product.options.first | escape }} </label>');
{% endif %}

// Hide drop-down selectors if we only have 1 variant and its title contains 'Default'.
{% if product.variants.size == 1 and product.variants.first.title contains 'Default' %}
  $('.selector-wrapper').hide();
{% endif %}
 });
</script>

Shopify's admin interface only allows for one product image per variant. Shopify的管理界面每个变体只允许一个产品图像。 For this reason, to do what you are trying to do isn't as easy as you are hoping. 因此,做您想做的事并不像您希望的那么容易。 Through the Shopify API you can add metafields to products and product variants and in these metafields you can store whatever information you want -- including links to additional images. 通过Shopify API,您可以将元metafields添加到productsproduct variants并且可以在这些元metafields中存储所需的任何信息-包括指向其他​​图像的链接。 Here is a link to the documentation for the metafields on variants: https://docs.shopify.com/api/reference/product_variant 这是有关变型元metafields的文档的链接: https : metafields

Since the admin interface doesn't really give you the ability to modify the metafields directly, you have a couple of choices: 由于管理界面并没有真正让您直接修改元字段的能力,因此您有两种选择:

  1. Spend the time and energy (or money) to hook up your own management tool to the API and modify it yourself. 花时间和精力(或金钱)将您自己的管理工具连接到API并自行进行修改。
  2. Purchase an app from the Shopify app store that already does this for you. 从Shopify应用商店购买已经为您完成此操作的应用。 Here is a link to some of these apps that should guide you in the right direction: https://apps.shopify.com/search/query?utf8=%E2%9C%93&q=variant 以下是这些应用程序的链接,这些应用程序可以指导您正确使用: https : //apps.shopify.com/search/query?utf8=%E2%9C%93&q=variant

Either way, you'll need to come up with some sort of property name convention so you know you're dealing with images because the metafields accept whatever you feel like dropping into them. 无论哪种方式,您都需要提出某种属性名称约定,以便知道您正在处理图像,因为元metafields接受您想要放入图像中的任何内容。

Once you are able to define the images for your product variants, then you'll need to update the logic in your liquid/javascript to accomplish the desired functionality. 一旦能够为您的产品变型定义图像,那么您将需要更新液体/ javascript中的逻辑以完成所需的功能。 You can do this a few different ways, and different people will have different opinions based on SEO but my suggestion for the path of least resistance is to do something like this: 您可以通过几种不同的方式来执行此操作,并且基于SEO,不同的人会有不同的意见,但是我对阻力最小的建议是执行以下操作:

{% for v in product.variants %}
  <div id="variant_{{ v.id }}">
    {% for m in v.metafields %}
      {% if m.key contains "WHATEVER_CONVENTION_YOU_USED_TO_DENOTE_IMAGES" %}
        {% comment %}
          OUTPUT THE IMAGE TAG - PROBABLY WRAPPED IN AN ANCHOR
        {% endcomment %}
      {% endif %}
    {% endfor %}
  </div>
{% endfor %}

You'll want to incorporate some logic to show and hide the div based on the variant. 您需要合并一些逻辑以根据变体显示和隐藏div。 Note the <div id="variant_{{ v.id }}"> 请注意<div id="variant_{{ v.id }}">

The reason for this approach rather than using the api to populate js objects is that your links are already in the DOM and you can create javascript handlers when the page loads. 采用这种方法而不是使用api填充js对象的原因是您的链接已经在DOM中,并且您可以在页面加载时创建javascript处理程序。 If you wait to populate your images and links until the user selects the variant, then you'll have to deal with the event handling of dynamically created nodes. 如果您等待填充图像和链接,直到用户选择了变体,那么您就必须处理动态创建的节点的事件处理。

Hope all this guides you in a good direction. 希望所有这些都能引导您朝着正确的方向发展。 If you have questions or need help feel free to reach out to me via my profile. 如果您有任何疑问或需要帮助,请随时通过我的个人资料与我联系。

Additional Note : Good software developers will note the O(n^2) time complexity in the code above. 附加说明 :优秀的软件开发人员将在上面的代码中注明O(n ^ 2)的时间复杂度。 Shopify doesn't execute your backend code when the page loads, rather it is executed and cached when your theme is uploaded or modified. 当页面加载时,Shopify不会执行您的后端代码,而是在上载或修改主题时执行并缓存它。 Because of this, the poor O(n^2) time complexity won't hurt your page load performance. 因此,糟糕的O(n ^ 2)时间复杂度不会损害您的页面加载性能。

As a side fun fact, this is the reason Shopify cannot create Liquid tags for things like current_time or random. 作为附带的事实,这是Shopify无法为current_time或random之类的事物创建Liquid标签的原因。 Their caching mechanism which keeps sites from crashing even when being presented on Shark Tank also relies on the fact that their liquid tags, filters, and blocks don't return variable results. 它们的缓存机制即使在出现在Shark Tank上时也可以防止站点崩溃,还取决于它们的液体标签,过滤器和块不返回可变结果的事实。 So, they can cache the resulting HTML and serve it directly from their caching servers...thus tags like random or current_time would only ever have an opportunity to run once. 因此,他们可以缓存生成的HTML并直接从其缓存服务器提供它……因此,诸如random或current_time之类的标签只能运行一次。

you can try variant-image-automator paid app. 您可以尝试使用variant-image-automator付费应用。 this hides unnecessary images from the product page. 这会在产品页面上隐藏不必要的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM