Shopify: Filtering collections by custom filter - shopify

I'm new with liquid and ruby, but I would like to create a custom filter in a collection, to filter by metafields. I already have:
A dropdown in the collection.liquid, with the values I would like to filter for.
When selecting a filter, it goes to a link like: https://myshop.myshopify.com/collections/my-collection/my-filter . Basically it is like the tags, but with my filter instead
However, since it is a custom filter and not a tag, I get no results. I'm wondering where is the query that displays all the products (or filters) is in the code. I know that it depends on the theme, but I'm using the default theme: launchpad-star.
Not sure if I could do it this way or with a link like: https://myshop.myshopify.com/collections/my-collection?filter_by=my-filter , in which case, I would also need where should the logic go.
I've looked at the forums already and found two closed tickets with no responses: https://ecommerce.shopify.com/c/ecommerce-design/t/using-metafields-to-create-filter-drop-downs-in-collection-liquid-187513 and https://ecommerce.shopify.com/c/ecommerce-design/t/using-metafields-to-create-filter-drop-downs-in-collection-liquid-134401 .
Thanks in advance

Probably not the best solution, but this is what I did to solve the problem:
I changed to the second option of the url, so when a user selects an option in the combobox, it is sent to a URL like: myshop.myshopify.com/collections/my-collection?filter_by=my-filter
In product-grid-item.liquid, I'm getting the metafield value of the product and displaying it as a class, and hide all the products as default. In the collection.liquid I read with javascript the value of the parameter (filter_by) and remove the "hide" class of the products with the value of the filter_by as class, so it gets displayed.
I feel that it is not very clean, but it is working as expected. Problems with this solution:
* Not displaying all the products and then filtering them
* I need to display all the products to avoid pagination, which could be a big problem if I have a lot of products.
If anyone could post a better solution, welcome!.

Related

Algolia vue-instantsearch : disjunction between two distincts facets

Using algolia vue-instantsearch, i’m encountering a special case and i’m having an hard time finding a solution.
Refinement behaviors is that you get the results that matches all your refinement filters.
If i filter on a brand and a price, i’ll get the results that matches both the brand and the price.
I need to add some specific filters that work differently. I would like to be able to say “returns me the results that matches either refinementA, or refinementB, or refinementC.”
The reason is that those refinements are checking fields that are not present on all the products.
If i check a value for refinementA, i want to keep all the results that has no value for field corresponding to refinementA, but remove those that has a value for refinementA that does not match with the one i filtered one.
Im thinking about handling myself some inputs instead of ias-components, and modifying by hand each query that is send to algolia by checking the value of my own inputs when searchFunction is triggered (https://www.algolia.com/doc/api-reference/widgets/instantsearch/js/#widget-param-searchfunction).
I did not found yet how i can trigger a search from an non-vue-instantsearch input event, and i’m not sure how the above solution could affect the internal working of vue-instantsearch.
I’m looking for feedbacks about a more standard way of doing this, or for any advices !
I got the answer by exchanging with a vue-instantsearch maintainer.
vue-instantsearch does not provide any option to do it.
A workaround is to overwrite algoliasearch search method, that is used under the hood by vue-instant-search.
const client = algoliasearch('', '');
const originalSearch = client.search;
client.search = function search(queries) { ... }
More informations in this github issue : https://github.com/algolia/vue-instantsearch/issues/1102

Is there any example or sample code for the find and filter feature in Cytoscape JS

I saw in cytoscape application we have features like find and filter by keywords and degree. I tried a workaround following the original docs. Here you can see the demo webdemo.intolap.com/cytoscape (view-source for the source code or snippet). The filter works well partially. Example, "apple" will display apple and it's connected nodes (1st level) just what I am looking for.
But the problem I am facing is about resetting the graph and filter again with a
different keyword. It seems the filter function does not work after the text box is cleared and then keyed in a different keyword.
I mean when I clear the text box, it resets the graph to original which is correct. I did that using an init() function which reinstates the graph. But then if I search for "Ball" filter does not work. Any help please. Thanks!
actually there is a reasonably good explanation in the official docs here, but to be honest, I too struggled with this feature at first:
Basically, you can filter the specific collection you want to search by just inserting a filter query. So if you want to filter all nodes, you can use this:
cy.nodes(filterQuery);
If you want to filter all elements, just call this:
cy.elements(filterQuery);
If you want to make it easy, you can use this short version (short for cy.filter(...)):
cy.$(filterQuery);
The filter query itself is not that hard, you can do this (assuming that you have a node with the id "first" or an attribute like nodeColor "#2763c4"):
cy.$('[id != "first"]');
cy.$('[id = "first"]');
cy.$('[nodeColor = "#2763c4"]');
cy.$('[weight > 50]');
Additionally, you can specify the target collection within your filter query like this:
cy.$('node[id != "first"]');
Lastly, if you need complex filtering, you can use a function to apply that logic to the filter, for that just do this:
cy.$(function(element, i){
return element.isNode() && element.data('weight') > 50;
});
Sounds like you are trying to cy.filter on a cytoscape instance that no longer exists at that point. That's why it works the first time, but not the second time (after you reinstate the graph, which probably means destroy & create).
You need to make sure you point your filter handlers to the active cytoscape instance.

Show content based on whether the user has certain tag in Mailchimp

I went through the Merge Tags here and here, but couldn't figure out the syntax that would allow me to show content based on whether the user has certain Tag or not.
Help?
My goal in case it helps:
User subscribes, and is queued for a welcome mail one day later. In meantime that user may get tagged (my way of segmenting them), and so, the next day when that user receives the welcome mail, the content needs to be catered based on the tag that user got.
Got a response from their support saying
merge tags do not work with Tags just yet
here's the whole thing:
While we do have conditional merge tags available, I'm afraid we do
not have any that would work with Tags. To be transparent, Tags were
recently added a few months ago, and there are some features in our
application that has not updated to work with Tags just yet.
Because conditional merge tags do not work with tags yet, the best
option would be to create multiple automations and send them out based
on each tags. If you do it that way, you'll be able to target those in
specific tags with specific content
Dug a little deeper from the first link. There is another link Use Conditional Merge Tag Blocks which contained the below code:
Name
IF-ELSE
Definition
Use ELSE to indicate alternative content to display if the *|MERGE|* tag value is false.
Example
*|IF:MERGE|* content to display *|ELSE:|* alternative content to display *|END:IF|*
Name
ELSEIF
Definition
Use ELSEIF to specify a new *|MERGE|* tag to be matched against if the first *|MERGE|* tag value is false.
Example
*|IF:TRANSACTIONS >= 20|* Enjoy this 40% off coupon! *|COUPON40|*
*|ELSEIF:TRANSACTIONS >= 10|* Enjoy this 20% off coupon! *|COUPON20|*
*|ELSE:|* Enjoy this 10% off coupon! *|COUPON10|* *|END:IF|*
More examples with definitions can be found here.
Hope this is the answer you were after.

GetResource, dynamic parent.

my problem is the following. I'm currently making a blog-page with get-page, get-resources, form-it, and wayfinder. This question requires a decent amount of knowledge about Modx and snippits. I've got the page numberin and all working and i've got a template page with all my calls in it (called weblogTemplate). This template has the following wayfinder call in it :
[[!getResources? &parents=`5` &limit=`5` &tpl=`blogPost`]]
[[!getPage?
&elementClass=`modSnippet`
&element=`getResources`
&parents=`4`
&depth=`2`
&limit=`5`
&pageNavOuterTpl=`[[+first]][[+prev]][[+pages]][[+next]][[+last]]`
&pageVarKey=`page`
&pageFirstTpl=`<li class="controlFirst"><a[[+classes]][[+title]] href="[[+href]]">Eerste pagina</a></li>`
&pageLastTpl=`<li class="controlLast"><a[[+classes]][[+title]] href="[[+href]]">Laatste pagina</a></li>`
&pagePrevTpl=`<li class="controlPrev"><a[[+classes]][[+title]] href="[[+href]]"><<</a></li>`
&pageNextTpl=`<li class="controlNext"><a[[+classes]][[+title]] href="[[+href]]">>></a></li>`
&includeTVs=`1`
&includeContent=`1`
&tpl=`blogListPost`
]]
as you can see the parent is set here to id number 5. This is fine for the homepage but any child page connected in the blog page also uses the same template and so would also have the same menu as the parent. You could use a fix to simply create 1 template for a page and keep using a different getResource call but keep in mind that it is a blog im making, new pages keep getting added. The user can't (, and wouldn't even understand to) make a template and edit any code. A solution i thought of would be to make the parent id dynamic, so it adjust to whatever page it is currently on. So for example if it was on the page with id number 12 it would make the parent call set to 12 and so show all the content under id number 12. If anyone has any ideas / thoughts / solutions i would be very grateful to hear them.
(a link about wayfinder that i used.)
The best solution would be to use two templates - one for main and one for the blog pages and use in blog templates:
&parents=`[[*id]]`
The problem with the user solveds by setting default_template in the system settings.
This worked for me:
[[!getPage?
&elementClass=`modSnippet`
&element=`getResources`
&parents=`[[*id]]`
&depth=`0`
&limit=`10`
&pageNavOuterTpl=`[[+first]][[+prev]][[+pages]][[+next]][[+last]]`
&pageVarKey=`page`
&pageFirstTpl=`<li class="controlFirst"><a[[+classes]][[+title]] href="[[+href]]">Eerste pagina</a></li>`
&pageLastTpl=`<li class="controlLast"><a[[+classes]][[+title]] href="[[+href]]">Laatste pagina</a></li>`
&pagePrevTpl=`<li class="controlPrev"><a[[+classes]][[+title]] href="[[+href]]"><<</a></li>`
&pageNextTpl=`<li class="controlNext"><a[[+classes]][[+title]] href="[[+href]]">>></a></li>`
&includeTVs=`1`
&includeContent=`1`
&tpl=`blogListPost`
]]
Thanks to Vasis for the provided help.

Creating tags on posts RAILS

I am making something that's like that autocomplete tags field for the posts on stackoverflow.com
I want to make it so that when you make a blog post, you can tag it with words in a database, similar to SO.
For the posts, it belongs_to_and_has_many tags
For the tags, it belongs_to_and_has_many posts
However it is a problem for me to do it on the same page because #post would be nil.
How can I implement this?
(If someone can give me the code for the stackoverflow ask question page that would be AWESOME)
I think the problem here is that it seems like you are asking two different questions. Please correct me if I'm wrong on this.
The first question is how you would implement the autocomplete feature to allow users to easily select from a pre-populated list of tags. To answer this, you might refer to spncrgr's answer above.
The second question is how to deal with associating these retrieved tags to the current post. For this you can add additional javascript functionality to your autocomplete solution. When a user selects which tag they want from the autocomplete field, you can do like StackOverflow does and add the tag to a list of tags in a single text field. These can be either space or comma delimited. When you submit the form to create the new post, you can parse this field into it's separate tags:
tags = params[:tags].split(' ')
You can then associate these tags to the model in the Post#create action.
This may not help you at all (or you may have already seen it), but here's a link to a Railscasts' episode on auto-complete:
http://railscasts.com/episodes/102-auto-complete-association
I know it helped me when trying to do something similar.
HTH
It looks for me like you want to generate tags automatically.
You could create
class Post
before_save :create_tags
private
def create_tags
# get your tags somehow
self.tags << Tag.new(:text=>"...")
end
end
method in models/post.rb and build them there.
If you want to search among existing tags for your auto-suggest, you should have it match from Tag.all, as that won't be nil, just as you would collect a group of objects in a select drop down. Not sure about the code for auto-complete, but the tags should be in the Tag table.