Skip to content

Commit

Permalink
feat: add category browsing functionality to store
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
NagariaHussain committed Nov 10, 2023
1 parent 0cf8f7c commit ff4648d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
9 changes: 5 additions & 4 deletions printrov_merch_store/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
# ----------

# add methods and filters to jinja environment
# jinja = {
# "methods": "printrov_merch_store.utils.jinja_methods",
# "filters": "printrov_merch_store.utils.jinja_filters"
# }
jinja = {
"methods": [
"printrov_merch_store.utils.get_categories_with_count"
],
}

# Installation
# ------------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% from "printrov_merch_store/templates/includes/categories_macro.html" import categories_list_group %}
{% extends "templates/web.html" %}

{% set category = frappe.form_dict.get("category") %}

{% set parents = [{"label": "All Products", "route": "/store"}] %}
{% set parents = [] %}

{% if category %}
{% set filters = {"is_published": 1, "printrove_category": category} %}
Expand All @@ -17,6 +18,9 @@
) %}

{% block page_content %}
<h4 class="text-muted mb-3">Browse By Category</h4>
{{ categories_list_group(active_category=category) }}

<h2 class="mb-6">{{ "All Products" if not category else category }}</h2>

{% if products | len == 0 %}
Expand Down
15 changes: 15 additions & 0 deletions printrov_merch_store/templates/includes/categories_macro.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro categories_list_group(active_category=None) %}
{% set categories_with_count = get_categories_with_count() %}
<div class="list-group">
<a href="/store" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center {{ 'active' if (not active_category) else '' }}" aria-current="true">
All
<span class="badge badge-primary badge-pill">{{ categories_with_count.values()|sum }}</span>
</a>
{% for category, count in categories_with_count.items() %}
<a href="/store?category={{category}}" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center {{ 'active' if category == active_category else '' }}" aria-current="true">
{{ category }}
<span class="badge badge-primary badge-pill">{{ count }}</span>
</a>
{% endfor %}
</div>
{% endmacro %}
15 changes: 15 additions & 0 deletions printrov_merch_store/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections import defaultdict

import frappe
import razorpay
from frappe.integrations.utils import (
Expand Down Expand Up @@ -86,3 +88,16 @@ def get_razorpay_client():
key_secret = razorpay_settings.get_password("key_secret")

return razorpay.Client(auth=(key_id, key_secret))


def get_categories_with_count():
categories_with_count = defaultdict(lambda: 0)
product_categories = frappe.db.get_all(
"Store Product", pluck="printrove_category"
)

# Count the number of products in each category
for category in product_categories:
categories_with_count[category] += 1

return categories_with_count

0 comments on commit ff4648d

Please sign in to comment.