2017-05-29 100 views
-2

在我的情況下Shopify產品有5個變體:Monday, Tuesday, Wednesday, Thursday, Friday。 我想在48小時內限制變體。例如:Shopify:更改變體的可用性

星期一 - Wednesday, Thursday, Friday

週三 - Monday, Tuesday, Friday

週五 - Monday, Tuesday, Wednesday, Thursday

我想知道如果我可以使用標準功能。通過Shopify的管理面板或內部結構。

+0

是的,你可以做到這一點使用JavaScript – miglio

+0

的對你是否在乎誰的產品添加到人的回答變化的複雜性他們的購物車在星期一,但不要退房,直到星期五... –

+0

@DaveB購物車的會話將在每一個新的一天清潔 – Lola

回答

1

您可以使用Liquid過濾出不想顯示的變體,以防止購物者能夠將這些產品添加到購物車。

在你的產品模板,找到行{% for variant in product.variants %}

這一行之前,添加{% assign day_of_week = 'now' | date: '%A' %}存儲當前星期幾,在液體可變

這一行後,添加代碼以過濾掉(你不想看到的變種)。對於您關心的每週的每一天,您需要一個'when'語句,然後再對照該變體的某些屬性(我使用變體標題)進行檢查。如果這是您不想顯示的變體,請使用「continue」語句跳過該變體並繼續下一個變體。

例子:

{% assign day_of_week = 'now' | date: "%A" %} 

{% for variant in product.variants %} 
<!-- Addition below to filter out products that customer shouldn't see --> 
    {% case day_of_week %} 
    {% when 'Monday' %} 
     {% if variant.title contains 'Monday' or variant.title contains 'Tuesday' %}{% continue %}{% endif %} 
    {% when 'Tuesday' %} 
     {% if variant.title contains 'Tuesday' or variant.title contains 'Wednesday' %}{% continue %}{% endif %} 

<!-- Repeat for each day of the week --> 
    {% endcase %} 
    <!-- Regular code from your variant loop --> 
+0

非常感謝你,那就是我需要的 – Lola

0

從我的其他獨立的回答,您還可以過濾使用JavaScript的變種。我通常更喜歡通過Liquid預過濾變體 - 依賴於Javascript可能會導致頁面加載閃爍,因爲變體在代碼運行之前會短暫出現。

假設jQuery是已經在頁面上,我們可以爲寫:

<script> 
    jQuery(document).ready(function(){ 
    var variants_list = {{ product.variants | json }}; 
    var date = new Date(Date.now()).toDateString(); 

    for(var i=0; i < variants_list.length; i++){ 
     var variant = variants_list[i]; 
     if(variant.title == 'Monday' && (date.indexOf('Mon') > -1 || date.indexOf('Tue') > -1){ 
     jQuery('[value="' + variant.id + '"]').remove(); 
     //Note: If you theme uses swatches or anything like that, you may need to remove additional elements from the document as well. 
     } 
     if(variant.title == 'Tuesday' && (date.indexOf('Tue') > -1 || date.indexOf('Wed') > -1){ 
     jQuery('[value="' + variant.id + '"]').remove(); 
     } 
    } 
    //Lather-rinse-repeat for each day of the week that you care about 

} 
</script>