Last active
August 24, 2019 14:04
-
-
Save ronipl/3a6c989471b3c671d6a256e82a112427 to your computer and use it in GitHub Desktop.
Display related product based on cart item
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% assign heading = 'You may also like' %} | |
<div id="related-product-from-cart-items" class="featured-product-items"> | |
{% assign tag = '' %} | |
{% assign prod_handle = '' %} | |
{% assign item_handle = '' %} | |
{% for item in cart.items %} | |
{% assign item_handle = item_handle | append: item.product.handle | append: ';' %} | |
{% endfor %} | |
{% for item in cart.items %} | |
{{item.item_count}} | |
{% if item.item_count == 1 %} | |
1 item | |
{% elsif item.item_count == 2 %} | |
2 item | |
{% elsif item.count > 2 %} | |
item more than 2 | |
{% endif %} | |
{% for col_item in item.product.collections %} | |
{% for product in col_item.products %} | |
{% for t in product.tags %} | |
{% assign tag = t %} | |
{% endfor %} | |
{%- assign current_variant = product.selected_or_first_available_variant -%} | |
{% if prod_handle contains product.handle or item_handle contains product.handle %} | |
{% else %} | |
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4 featured-product-item {{ product.handle }} {{ product.type | handleize }}" product-handle="{{ product.handle }}"> | |
<div class="product__image"> | |
<img src="{{ product.featured_image | img_url: '600x600' }}" alt="{{ product.title }}"> | |
</div> | |
<div class="product__details"> | |
{% if product.metafields.custom_fields["product_subcategory"] != blank %} | |
<h4>{{ product.metafields.custom_fields["product_subcategory"] }}</h4> | |
{% else %} | |
<h4>{{ tag }}</h4> | |
{% endif %} | |
{% if product.metafields.custom_fields["product_title"] != blank %} | |
<h3>{{ product.metafields.custom_fields["product_title"] }}</h3> | |
{% else %} | |
<h3>{{ product.title }}</h3> | |
{% endif %} | |
<div class="divider dark-red"></div> | |
</div> | |
<a href="#" class="featuredItemBtn btn btn--secondary button red-border" prod-variant="{{ product.selected_or_first_available_variant.id }}">ADD TO CART</a> <!--{{product.url}}--> | |
<input type="hidden" class="feature-product-id" value="{{ product.selected_or_first_available_variant.id }}"> | |
</div> | |
{% endif %} | |
{% endfor %} | |
{% endfor %} | |
{% endfor %} | |
{%- comment -%}<div class="slider-dots"></div>{%- endcomment -%} | |
</div> | |
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> | |
<script type="text/javascript"> | |
function getRelatedProductFromCartItems(){ | |
$("#related-product-from-cart-items").hide(); | |
if($(".featured-product-item").text()){ | |
$("#related-product-from-cart-items").show(); | |
} | |
} | |
getRelatedProductFromCartItems(); | |
var getRandomThreeProductOnCartItems = (function() { | |
var parent = $("#related-product-from-cart-items"); | |
var products = parent.children().slice(); | |
//console.log(products); | |
/* for checking if the same item is exist on array elements*/ | |
var product_exist = {}; | |
var product_array = []; | |
$(products).each(function(){ | |
var product_handle = $(this).attr('product-handle'); //handles | |
if(product_exist[product_handle]){ // checking duplicate handle | |
//console.log(product_exist) | |
$(this).remove(); | |
}else { | |
product_exist[product_handle] = true; | |
product_array.push($(this)); | |
} | |
}) | |
var ctr_item = 3; | |
return function(){ | |
parent.empty(); | |
var product_items = product_array.filter(function( obj ) { | |
console.log($(obj).is('.gift-card', '.sampler')); | |
return $(obj).is('.gift-card', '.sampler') == false; | |
}); | |
var clone = product_items.slice(); | |
var appended_item = []; | |
for (var i = 0; i < ctr_item && i < product_items.length; ++i ) { | |
console.log(product_items); | |
appended_item.push(clone.splice(Math.floor(Math.random() * clone.length), 1)[0]); | |
}; | |
parent.append(appended_item); | |
console.log(appended_item); | |
//$('.sampler').remove(); | |
}; | |
})(); | |
getRandomThreeProductOnCartItems(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment