How to remove empty categories? - plugins

I have a two scenarios to solve :)
Let's say I built plugin (with builder) where I have products and categories.
Now on page with listed categories I have all of them, but I want to not display empty categories.
How to remove empty categories form listing?
Second scenario. Some of products have options and some none.
Products and options have relation table.
On single product page I have some like:
product description
options for this product
(that work well)
But in case of product doesn't have any options I want to have:
product description
text "this product have no options" (or not display any text at all)
So far I have tried something like:
{{ if option in record.options == true }}
display options
{{ else }}
"this product have no options"
{{ endif }}
But this doesn't work at all.
Is there a way to check for existing options for product?
Thanks for your time.

For Question number 1
Here you can put a condition on $query->has('products', '>', 0) but seems you are using the builder plugin so you can simply put a condition on the category product count.
{% for category in categories %}
{% if category.products|length > 0 %}
{{category.name}}. // here you will get only category which has products.
{% endif %}
{% endfor %}
For Question number 2
You can do something like this
{% if record.options|length > 0 %}
show them
{% else %}
no options
{% endif %}
if any doubt please comment.

Related

Filtering by tag shows all pages instead of tagged pages

With reference to the Wagtial recipe on tags, I have set up tags in my courses app. However, tag filtering does not seem to be working. The tags themselves seem to be working: I can select tags in the admin and show them on the page. I have a course page tag model that seems to be working properly:
class CoursePageTag(TaggedItemBase):
content_object = ParentalKey('CoursePage',
on_delete=models.CASCADE,
related_name='course_tagged_items')
I have filtering setup in the CourseListingPage model (with template = "courses/course_listing_page.html") like this:
# Get all courses
course_pages = (
CoursePage.objects.live().public().order_by("-first_published_at")
)
# Filter by tag
tag = request.GET.get('tag')
if tag:
course_pages = course_pages.filter(tags__name=tag)
context['course_pages'] = course_pages
return context
And in the template I have this:
{% for tag in page.tags.all %}
{{ tag }}
{% endfor %}
When I click a tag on a course page with the above template code, I do arrive at the parent course listing page, and the URL shows what seems to be the correct filtering format:
courses/course-listing/?tag=test
But I do not see a filtered list of pages. Instead I see all course pages. I suspect there is something very simple that I am doing wrong here.
Answering my own question: {% for course in course_pages %} is required in the template at the location of the listing of the filtered pages -- in my case, below the filtering code. So, I now have the filtering code, then the actual listing of the pages, which begins with:
{% for course in course_pages %}
{% with course=course.specific %}
... HTML/template code for page details (e.g. course.title, course.sub_title) ...
{% endfor %}
{% endwith %}
I didn't initially that realize that the filtering code only does the filtering; it doesn't actually show the results of the filtering. For a beginner like me, when the docs say that the code will filter the listing to only those pages with a matching tag name before passing this to the template for rendering, it's possible to misread this to mean that the template will show the filtered results without further steps. But that's not the case. The extra contextual code is required -- at least it was for me.

Adding attributes to Shopify's {% form %} liquid tag

Is it possible to add attributes to Shopify's {% form %} liquid tag? I want to give all my forms id's so that I don't have to rely on parent elements to target their styles.
Yes you can add attributes.
Example:
{% form 'contact', id: "test" %}

How to order Wagtail tags

I tried to create tags for my Posts as described in doc (tutorial My first website). But i need to show these tags in specific order. Is there some simple way or i just need to create own class with Orderable?
I have assumed you have gotten to this point in the docs - Tagging Posts and want to present the view of your BlogPage with the tags in a special order (eg. alphabetical). Orderable is if you want to do more complex admin interaction with InlinePanels and ordering of related models, where you are asking the page editor to order related items themselves.
The tutorial has this code for your template blog_page.html:
{% if page.tags.all.count %}
<div class="tags">
<h3>Tags</h3>
{% for tag in page.tags.all %}
<button type="button">{{ tag }}</button>
{% endfor %}
</div>
{% endif %}
To work with a custom ordering of your tags, you will need to set up a way to send your ordered tags to the template context. The easiest way to do this is to have a method added to your BlogPage model, we will call this get_ordered_tags.
class BlogPage(Page):
date = models.DateField("Post date")
# other fields...
def get_ordered_tags(self):
"""Specific ordered list of tags."""
# ordered_tags = self.tags.order_by('?') # order randomly
ordered_tags = self.tags.order_by('name') # order by tag name
return ordered_tags
Further up the page you would have seen the docs on Overriding Context, we will do something similar to add ordered_tags to our Page's context.
We can then easily make one minor change to our template, just replace:
{% for tag in page.tags.all %}
With:
{% for tag in page.get_ordered_tags %}
So instead of getting the tags in their default order, we are getting them in our specific order.

How to exceed 255 characters limit of product tag in Shopify?

I am looking for a way to exceed the 255 characters limit on product tag in Shopify admin. Please suggest any way of doing so.
In general this does not sound like a good idea. If you are trying to embed extra information per item you might want to look at putting that in a snippet file and then formatting your tag like __extra File1 and then a snippet like the following in your product template:
{% for tag in product.tags %}
{%if tag contains '__extra' %}
{% assign snip = tag | remove_first : '__extra ' %}
{% include snip %}
{% endif %}
{% endfor %}
This would allow you to share large chunks of information per product while not blowing up your tags.
If you adopt this approach then you'll also want to go through your theme and make sure you filter out tags beginning with '__'. e.g.
{% for tag in collection.all_tags %}
{% assign tag_pref = tag | slice:0,2 %}
{% unless tag_pref == '__' %}
... do your tag related layout
{% endunless %}
{% endfor %}

Shopify Liquid - if product.template contains

I am trying to retrieve the template for a specific product while NOT on the product template page. I am on the collection page and am trying to adjust the Quick shop feature to only show the add to cart for products no associated with a custom template. For example, I've assigned a 'consultation' template to a group of products. However because I am no longer on the product template page, the Quick shop doesn't recognize the consultation product. Any ideas on how I can make this work other than using tags, types and collections?
Thank you!!
On collection page , products for that collection are called in a loop, something like
{% for product in collection.products %}
...
{% assign prodtemplate = product.template_suffix %}
{% if prodtemplate contains 'consultation' %}
{% else %}
//Add to Cart code
{% endif %}
...
{% endfor %}