2016-02-27 39 views
1

我是Shopify的新手,我一直在玩这个星期左右,但在尝试解决我的问题时没有取得太大的成功。Shopify Product Page隐藏变形图片

我目前使用的是布鲁克林模板,它们代表产品页面的方式并不能提供最佳的用户体验。默认情况下代表图像的方式是将所有产品变体图像始终显示给客户,即使他没有实际纠正该变体(即产品的特定颜色)也是如此。它还以垂直方式显示所有产品图像,这使得用户的导航非常令人沮丧,因为如果每种产品型号都有不止两张照片的话。

我在网上发现了一个教程(http://littlesnippets.ca/blogs/tutorials/15665261-grouping-images-with-variants),它解决了我的问题的一部分,用于隐藏未由客户选择的变体的图片,并在用户点击后显示它们(可以看到我想在这里的一个例子http://group-variant-images.myshopify.com/collections/frontpage/products/anson-chair)。这或多或少是它现在看起来像我的网站(https://themes.shopify.com/themes/brooklyn/styles/brooklyn/preview)。

问题是,该教程正在应用于一个网站,虽然它是相似的,但它并未使用Ii所做的确切主题/功能。 我未能将这些更改应用于我的主题,有人能帮我一个忙吗?

这是我此刻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> 
+0

如果您有一个指向您当前商店的链接,并且可以更好地解释您要实现的目标,可能会有所帮助。您是否试图在用户调整选项时动态显示和隐藏变体图像,或者您希望它们在页面加载时根本不显示? – ohiodoug

+0

让我们说,我的产品页面看起来非常像这些https://themes.shopify.com/themes/brooklyn/styles/brooklyn/preview。在这里你可以看到产品有几种颜色(海军蓝,青苔和肉豆蔻)。我想要的是找到一种方法来为每种颜色提供多张照片,一旦用户选择了一种颜色,它将仅显示该颜色的产品图片,因此其他颜色的产品图片将会是“隐藏”,直到用户为产品选择另一种颜色。所以,如你所说,我希望图像显示和动态隐藏:) –

+0

让我确保我理解这个问题。每个变体都有多个图像?或者产品本身有多个图像,并且在页面上选择选项时,产品页面上的图像会切换到变体图像? – ohiodoug

回答

2

Shopify的管理界面只允许每一个变型产品形象。出于这个原因,做你正在做的事情并不像你期望的那么容易。通过Shopify API,您可以将metafields添加到productsproduct variants,在这些metafields中,您可以存储任何想要的信息 - 包括指向其他​​图像的链接。下面是变体的文档的链接为metafieldshttps://docs.shopify.com/api/reference/product_variant

由于管理界面并没有真正给你直接修改metafields的能力,你有两个选择:

  1. 支出将自己的管理工具连接到API并自行修改它的时间和精力(或金钱)。
  2. 从Shopify应用商店购买一个已经为您做到这一点的应用。下面是其中一些应用程序应该引导你在正确的方向的一个链接:https://apps.shopify.com/search/query?utf8=%E2%9C%93&q=variant

无论哪种方式,你需要拿出某种属性命名约定的,所以你知道你处理图像,因为metafields接受任何你想放入它们的感觉。

一旦您能够为产品变体定义图像,那么您需要更新liquid/javascript中的逻辑来完成所需的功能。你可以这样做几种不同的方式,不同的人会根据搜索引擎有不同的意见,但我的阻力最小的路径的建议是做这样的事情:

{% 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 %} 

你会想加入一些逻辑根据变体显示和隐藏div。请注意0​​

此方法的原因而不是使用api来填充js对象的原因是您的链接已经在DOM中,并且您可以在页面加载时创建javascript处理程序。如果您等待填充图像和链接,直到用户选择变体,那么您将不得不处理动态创建的节点的事件处理。

希望这一切都能指引您朝着良好的方向发展。如果您有任何疑问或需要帮助,请随时通过我的个人资料与我联系。

附加说明:好的软件开发人员会注意到上面代码中的O(n^2)时间复杂度。 Shopify不会在页面加载时执行后端代码,而是在主题上传或修改时执行并缓存。正因为如此,糟糕的O(n^2)时间复杂度不会影响您的页面加载性能。

作为一个有趣的事实,这就是Shopify无法为current_time或random等事物创建Liquid标签的原因。他们的缓存机制即使在Shark Tank上展示也不会造成网站崩溃,这也依赖于他们的液体标签,过滤器和块不会返回可变结果。因此,他们可以缓存生成的HTML并直接从其缓存服务器提供服务......因此像random或current_time这样的标签只能有机会运行一次。

+0

非常感谢您的回答,我会尽力解决,看看我能不能拿出一些体面的东西! –